Solvedreact select textbox input clears after selecting a value from the options using redux and onInputChange returns empty even if returning the value.

Using redux and react-select the input text is cleared in the following two scenarios:

  • After typing two or three characters the input text gets cleared
  • Pressing enter in one of the list options

I am using react and redux with an immutable state . I am not sure if that could affect but mentioning it just in case. When I click on the option list it does update the state and the textbox correctly.

At the beginning I though it could be related to async calls but I do not think that could affect as I am leaving redux deal with it and in the component I display whatever the properties have. Debugging my issue, I can see that the state is being updated but the input text for some reason does not display what the state has. I am happy to contribute with a PR if needed, I just need some sort of direction on where could the error be happening.

It seems that something apart from my intention is calling the interceptor onInputChange . If I set a log it comes as empty despite the fact that I am always returning the value sent. Moreover, when pressing enter in the list of options it updates the state but does not display the text in the select textbox.

class Selector extends Component {

componentWillMount()
 {
   this.onChange = this.onChange.bind(this);
   this.onInputChange = this.onInputChange.bind(this);
   this.dispatchSuggestions = this.dispatchSuggestions.bind(this);
 }
 onChange(selectedOption){
    let newValue='';
    if(selectedOption !== null){
      newValue=selectedOption.name;
    }
    this.props.actions.updateTextSuccess(newValue.toLowerCase(),
                                    this.props.questionId, this.props.partId)
}
async dispatchSuggestions(newTextValue)
{
  //First update the text so the api can take the first characters that we are looking for in the options
  let updatingText= await this.props.actions.updateTextSuccess(newTextValue,
                                       this.props.questionId,
                                       this.props.partId);
//dispatch the options 
  let updatingSuggestions=await this.props.actions.loadSuggestions(
                this.props.parts.get('byPartId'),
                this.props.questionId,
                this.props.partId);

return updatingText;
}
 async onInputChange (newTextValue)
 {
     //when the user types the third character dispatch an action to load the options and return the 
    //newTextValue
     if(newTextValue.length===3 && newTextValue.trim() !=="")
     {
       return await this.dispatchSuggestions(newTextValue)
     }
    return newTextValue;
 }
  render () {
    let suggestions=[];
    if(this.props.part.get('suggestions').size === undefined){
      suggestions=this.props.part.get('suggestions');
    }
    else {
      suggestions=this.props.part.get('suggestions').toJS(); //toJS because my state is immutable
    }
    return (
      <Select style={{width:'180px'}}
        options={suggestions}
        labelKey='name'
        valueKey='name'
        value={this.props.part.toJS().text}
        onChange={this.onChange}
        onInputChange={this.onInputChange}
      />
    )
  }
}
13 Answers

✔️Accepted Answer

I had to make the following changes to get our search working as expected:

<Select.Async
	placeholder="Placeholder"
	loadOptions={this.state.debouncedFetchOptions}
	value={this.state.selection.value}
+	onSelectResetsInput={false}
+	onBlurResetsInput={false}
	onChange={this._onChange}
/>

Other Answers:

Well, after a couple of hours diving into this issue, I found the reason. We moved from 1.1.0 to 1.2.1 and then everything started to fails, primary on async calls + redux.

If you see this commit has introduced a fix to clear the input after refresh inputValue props.
936dd6e and especially this commit ae26bf8 which force by default to clear the input text after each new props are received because onSelectResetsInput is TRUE by default (

onSelectResetsInput: true,
)

Changes are visible here as well https://github.com/JedWatson/react-select/blame/c9d027387a77b3695fdfbb3d4de25ffcea9505ee/src/Select.js#L144

My solution was force onSelectResetsInput to false, and then all works properly.

import ReactSelect from "react-select";

render() {
    return <ReactSelect {...props} onSelectResetsInput={false}/>;
}

Disclaimer: Setting of onSelectResetsInput will avoid clear the text obviously. So I'm not sure whether is intended or a bug

-- Update --
https://codesandbox.io/s/o4y2p39o29

Related Issues:

187
react select onChange returns the selected value, not the complete event
@ashnur I'm going to address your comment for the benefit of anyone else who is seeking to understan...
116
react select React Select [v2] focus outline color
@Iulia-Soimaru you can style the borderColor via props like: Hi i want to change the focus outline c...
97
react select How to force reload of options?
This works for me: Example working: https://github.com/helmarjunior/react-hooks-select-async ...
64
react select v3 upgrade guide
Would be awesome if you could update typings for this too Currently don't believe I can import like ...
42
react select [V2] Warning: Prop id did not match.
I think it's instanceId prop not id prop? Getting this error in console Warning: Prop id did not mat...
33
react select How to set value on version 2?
Was thinking the same took me a bit From what I can tell you have to set the value to the object of ...
25
react select How to keep value in textbox search after choosing it
@DaveOdden (and others) For V2- I was able to enable editable text by setting the inputValue prop on...
25
react select Warnings: componentWillReceiveProps in strict mode, Legacy context API, findDOMNode is deprecated
For anyone else going insane over this you can disable those warnings (and other actually helpful on...
23
react select Add isOpen Prop
isOpen or alwaysOpen is really important property Sometimes we need it We all can make a fork to add...
15
react select Select doesn't render isomorphically
Use instanceId: <Select instanceId=unique name ...> Hello If I render this server side first and the...
11
react select Uncaught TypeError: Object(...) is not a function
Sorry I meant to follow up here when we resolved this over email The issue was with the Webpack conf...
10
react select textbox input clears after selecting a value from the options using redux and onInputChange returns empty even if returning the value.
I had to make the following changes to get our search working as expected: Using redux and react-sel...
9
react select Select in IOS doesn't work
Thanks for your reply I fixed it - in my case 'fastclick' lib causes that issue ...
9
react select Cursor lagging input
I also experience some sort of this bug in IE11 When you type relatively fast the input jumps back a...
8
react select Critical accessibility issues: aria roles and aria-label
For anyone else who stumbles upon this thread and is looking for a temporary fix.. ...
6
react select Styles are not applied to react-select inside a shadow-DOM component
@JedWatson we add additional check and now it works Can you update your package? ...
5
react select Multi labelKey or custom labelKey
Maybe labelKey can receive a function: That would be awesome! Hello ...
5
react select Feature Request: Allow for sorting options in filterOptions prop
Meanwhile i had to do something like this: Hope it helps someone In v1 overriding the standard filte...
5
react select How to specify labelkey and valuekey in version 2.1.1
You forgot to actually ask your question (which should really go to StackOverflow in this scenario) ...