JedWatson / JedWatson/react-select

Placeholder not visible for empty object as value - Typescript

Open
#5,126 0 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

Found this issue in older versions, but using javascript. Basically, when i have a empty object as value, its placeholder does not show up, even if i set the value to null if its empty.

Wrapped component:

function verifyIfIsEmpty(value: any) {
  if (Array.isArray(value)) {
    return value.length === 0;
  }
  return Object.values(value).length === 0;
}
<InputAsyncSelectFormik__Wrapper>
      <AsyncSelect
        cacheOptions
        defaultOptions
        theme={(theme) => ({
          ...theme,
          colors: {
            ...theme.colors,
            neutral20: error ? 'red' : theme.colors.neutral20,
            neutral30: error ? 'red' : theme.colors.neutral30,
            primary: error ? 'red' : theme.colors.primary,
          },
        })}
        value={value && !verifyIfIsEmpty(value) ? value : null}
        styles={SelectStyles}
        {...props}
      />
      {name && (
        <InputAsyncSelectFormik__ErrorMessage name={name} component='p' />
      )}
</InputAsyncSelectFormik__Wrapper>

Using it:

<InputAsyncSelectFormik
          loadOptions={getData}
          onChange={(value: any) => {
            setFieldValue('useCase', value);
          }}
          getOptionLabel={(option: IUseCase) => {
            return option.name;
          }}
          isDisabled={isEditing}
          value={values.useCase}
          name='useCase'
          placeholder={messages['common.useCase']}
          isOptionSelected={() => {
            return false;
          }}
/>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file, test, or entry point is named. First reproduce the empty-object value case in the TypeScript React select component, then locate the placeholder and value handling and add a regression test; done means the placeholder is visible for an empty object, including when its value is treated as null.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.