Async multi-select removing values on search
See original GitHub issueHi everyone,
I’m having a weird issue when searching on an async multi-select. When I visit my page, I have five users already loaded in the value (Tammy, Sherri, Tonia, Jessica, Jamie). When I start to search on that input to add more users, some of these values disappear depending on what I’m searching for.
i.e. I search for “j” now I can only see “Jessica” and “Jamie” as values. But, this doesn’t change my state, I still see all five until I actually select something from this search, and then they are gone from the state aside from “Jessica,” “Jamie” and my new selection. I’m currently using 1.0.0-rc.3 but I tried switching to re.9 with no resolution.
Here is my code:
<Select.Async
loadOptions={this.getUsers}
multi
disabled={this.state.sending}
value={this.state.userIds}
onChange={users => this.setState({ userIds: users })}
placeholder="Search for Users" />
userSearch = (search, types, callback) => (
Fetch.ajax({
... get the data
}).done(({ users }) => {
const options = this.formatUsers(users)
callback(null, { options })
})
)
getUsers = (search, callback) => {
this.userSearch(search, ['Admin'], callback)
}
formatUsers = users => (
_.map(users, user => (
{ label: user.name, value: user.id }
))
)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
Top Results From Across the Web
React select remove selected option on search - Stack Overflow
import React from "react"; import Select from "react-select/async"; const CheckboxDropdown = ({ options, defaultValue, loadOptions, ...
Read more >Advanced - React Select
While React-Select assumes a standard way of filtering the menu on search, ... 'remove-value': (Multiple) Removing a selected option with the remove button ......
Read more >React-Select-Plus Example - HubSpot
Multiselect Single Value. This example implements custom label and value properties, async options and opens the github profiles in a new window when...
Read more >chakra-react-select - npm
Once installed, you can import the base select package, the async ... isMulti options={[ { label: "I can't be removed", value: "fixed", ...
Read more >React Select with options outside - Prepsheets
If isMulti is true then we use the controlShouldRenderValue prop to hide the selected values and loop over the values prop and map...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello!
I also had the same issue today and I’m using react-select 1.1.0. Let me try to explain what the problem is. After looking at the source code, I found out that
getValueArray
andexpandValue
function inSelect
component might be the culprit.getValueArray
is one of the function that is responsible to render the selected values on the screen.When we pass in an array of a string into the value property (
this.state.userIds
in your case) of theSelect
element, it will go to this code insidegetValueArray
:It will call
expandValue
function for each element of the value and if we look at theexpandValue
function, because of the type of value isstring
, it will go to this code:And this is where the problem is. It will compare each selected value with the options that are loaded asynchronously and return undefined if the selected value is not present in the options. So in your case, it will return only Jessica and Jamie and the rest of the items will be undefined:
Then all the undefined values will be filtered out and the component will render the remaining two.
I’m not sure how to fix this cleanly and the temporary solution that I had is to not pass in an array of a string into the value props but instead pass in an array of an object that has the same data structure as the options. In your case, I would do:
Hello -
In an effort to sustain the
react-select
project going forward, we’re closing old issues / pull requests.We understand this might be inconvenient but in the best interest of supporting the broader community we have to direct our limited efforts to maintain the latest version.
If you aren’t using the latest version of
react-select
please consider upgrading to see if it resolves any issues you’re having.If you feel this issue / pull request is still relevant and you’d like us to review it, please leave a comment and we’ll do our best to get back to you.