JedWatson / JedWatson/react-select

Select ignores onInputChange handler when it returns inputValue as an empty string

Open
#4,964 2 comments 0 reactions 1 assignee View on GitHub

@Rall3n is already working on this.

Since Dec 22, 2021.

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

Description

I'm attempting to enforce a maximum character count and keep users from entering certain characters.
Here is my Select:

<AsyncCreatableSelect
  isMulti
  isSearchable
  defaultOptions
  cacheOptions
  defaultValue={optionsDefaultValue}
  onChange={value => setUpdatedOptionsList(value)}
  onInputChange={handleOnInputChange}
  theme={theme}
  styles={isSavingOptions ? disabledStyles : styles}
  isDisabled={isSavingOptions}
  components={{ ClearIndicator: null, LoadingIndicator: null }}
/>

and here is the handler in question:

const handleOnInputChange = inputValue =>{
  if(inputValue.length > 3){
     inputValue = inputValue.substring(0, 3);
  }
  else{
     const forbiddenCharacters = /[^a-zA-Z0-9.\-_ ]/g;
     if(inputValue.match(forbiddenCharacters)){
       inputValue = inputValue.replace(forbiddenCharacters, '');
     }
  }
  return inputValue;
}

Typing in :

  1. "dog" shows "dog"
  2. "dogs" shows "dog"
  3. "do#" shows "do"
  4. "#" shows "#"
  5. "#f" shows "f"
  6. "#$" shows "#$"

In scenarios 4 and 6, inputValue is being returned as an empty string. I even had a console log in there at one point to make sure that's what it was doing but, instead of showing that, the Select continues to show the forbidden character (unless the user types an approved character after, in which case it removes the forbidden one as shown in scenario 5) even though the inputValue is empty. I also tried hard coding it to return inputValue as an empty string (also tried null), and it just showed whatever I typed instead of nothing. Any time the handler returns an empty string, the Select seems to just...refuse to acknowledge it.

Thank you!

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.