-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input field not updating when option is selected #828
Comments
I'm finding a similar issue but only when I've declared an onChange function.. |
Are you passing the value back into the component? In v1, react-select is now "controlled" so the actual value has to be passed back in as a prop. https://github.com/JedWatson/react-select/blob/master/CHANGES.md Also, if you are doing async then you often need to pass in a whole object as |
That explains it. Thought it must be something like this! Since it sounds like I'm not the only one who has been bitten by this, it might help if this was shown in the examples in the docs. Thanks @jljorgenson18 |
still trying to figure this out ... could you give an example on "the actual value has to be passed back in as a prop"? it seems that setState doesn't work anymore, but how do you pass back the value/option? |
That depends on how you are storing your value. If you were storing your value in state, you would add |
Thanks. But this only seems to work if the selected value is part of the initial options array (with using async, this is not necessarily the case). |
In v1.0.0, you can pass the value back in as a "full option." In this case, you are not simply passing in the value, but rather the value combined with the label. For my async select components, I use a getValue method. Here is a snippet as to how I have it setup with a few properties removed for brevity ...
getValue: function() {
// To work around react-select's weird input api
const {val} = this.props.targetObj;
if(val) {
return ({
label: val,
value: val
});
}
},
render: function() {
const {targetName, targetObj} = this.props;
return (
<div>
<label htmlFor={ targetName }>
{ targetObj.label }
</label>
<Async onChange={ this.onSelectChange }
name={ targetName }
value={ this.getValue() }
loadOptions={ getOptions }
searchable={ true }
clearable={ true } />
<div className="formErrorFeedback">
{ targetObj.errorMessage }
</div>
</div>
);
... |
As @jljorgenson18 and others said, loadOptions should return option objects array: [
{ label: "Option 1", value: 1 },
{ label: "Option 2", value: 2 },
] And to properly display selected item, set value prop of Select.Async to option object: { label: "Option 1", value: 1 } |
@sehmaschine I'm facing this problem too. The value passed as a prop has to be an instance of an option passed via the loadOptions-func. Therefore it's unfortunatly not possible to pass It would be useful if the passed value would be compared to the passed options by see https://github.com/JedWatson/react-select/blob/master/src/Select.js#L679 for reference |
Also is there a reason the event is not passed in the onChange? Seems like setting the value with e.target,value would be another possibility that is currently disallowed. |
Thanks @jljorgenson18 what you suggested is fixed this issue. but it is causing a new issue, now the dropdown list is hiding every time I select a option. I have to type/search again to see dropdown list (In multi select). |
Thank you so much @jljorgenson18! This fixed a similar issue I was having with Here's my code, for anyone who encounters this:
|
@JedWatson this is also an issue when using a |
To expand on what @dspoonia7 said (I'm also seeing this behavior), even selecting the dropdown arrow does not reveal any options. If you type a character and delete it, then they'll get populated. Updated: |
@geezhawk : I was also caught out by this. A couple of reasons why the input value might not have been updating: The Changes doc states:
So, if you are using setState to set a value that is not in the props passed to the Select component, then the value needs to look like:
You cannot only do If you are passing a value that is in the props array being passed to the Select component, then you can pass just the value in a handler function:
Other threads have indicated that passing "" will reset the Select form. However, I have found that unless |
This really needs to be updated in the docs. |
@geezhawk i have same trouble :'( |
I agree! I'm glad I found this topic. I also found some working code by user Nazarlitvin here: Although where it says "return(I" I changed "I" to "Select", which is what I called my react-select variable. Good luck to everyone else running into this issue. |
@jacobwicks thanks for the helpful responses! |
Thanks for helping out everyone, I'm cleaning up the docs now so hopefully this is all resolved. |
@JedWatson It would be very handy if https://github.com/JedWatson/react-select/tree/v1.x#usage mentioned this workaround for |
It seems like the component still does not select the value if passing a string that matches on the value property in the options property. Where in the documentation is this explained better? |
The case I'm running into is that the value is actually getting selected when I pass a string, but the label is just not displaying in the input field. If I click the dropdown I can see that the appropriate options is highlighted, just can't get it to display in the input field. Is this the same problem or new problem? |
Forgive me if this is a stupid question, but I'm having trouble with v1.0.0 of react-select not updating the input field with the selected value after an option is clicked. When I use versions <1.0.0 it automatically updated the input field when I clicked an option. But with v1.0.0 the input field remains empty. Do I need to manually set the value? If so, how? Any help would be greatly appreciated :).
And thanks for putting all the painstaking work into making this awesome tool 👍
The text was updated successfully, but these errors were encountered: