react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Refresh the items in Select.Async

Open nizamsp opened this issue 10 years ago • 35 comments

I have the following Select.Async and a corresponding loadOptions function to load items.

<Select.Async loadOptions={loadOptions} 
                           value={this.state.data.my_value} 
                           onChange={this.logChange} 
                           allowCreate={true} 
                           backspaceRemoves={true} 
                           newOptionCreator={this.optionCreator} 
                        /> 

On a separate UI action, I want to reload this Select.Async items. I am looking for a function to refresh this Select options or invalidate them.

Any help is much appreciated.

nizamsp avatar Feb 07 '16 09:02 nizamsp

I have solved this by using load external option. https://github.com/JedWatson/react-select#async-options-loaded-externally

Thanks for the great library!

nizamsp avatar Feb 07 '16 11:02 nizamsp

I still need to solve this issue. I cannot use the load-externally option, as I need to refresh/add options using the onChange() hook

burgalon avatar Feb 15 '16 07:02 burgalon

@burgalon : You can always add options using the this.state.data right? I load things dynamically into my select but I don't use this Select.Async

 <Select options={this.state.data.all_options}
                            value={this.state.data.selected_option}
                            onChange={this.logChange}
                            allowCreate={true}
                            backspaceRemoves={true}
 />

nizamsp avatar Feb 15 '16 07:02 nizamsp

@nizamsp I'm currently using <Select.Async> as I need to load more options on each keystroke.

<Select> onChange is fired only after selecting an option.

The problem is that <Select.Async> does not accept options

burgalon avatar Feb 15 '16 15:02 burgalon

@burgalon What about onInputChange ?

joual avatar Feb 21 '16 13:02 joual

@joual Thanks. Wasn't aware of onInputChange. That actually works great! I guess we can close it

burgalon avatar Feb 29 '16 15:02 burgalon

@burgalon I'm in the same situation and I'm wondering, did you stick using Select.Async & loadOptions or did you completely skip this an used basic Select with onInputChange & options props ?

LoicUV avatar Apr 15 '16 08:04 LoicUV

@LoicUV yep completely skipped Select.Async and using regular Select with onInputChange

burgalon avatar Apr 18 '16 05:04 burgalon

@burgalon i'm using the following code <Select name= "assignee" inputText="somestring" multi={true} options={options} onInputChange={this._getUsers}/>

_getUsers: function(input){ UserActions.search(input); var optionsDirty = UserStore.getUsers(); options = []; optionsDirty.map(function(option){ options.push({value:option.id, label:option.firstName+' '+option.lastName}); }); }

the problem with this is the text input value get reseted everytime because of DOM update, is there any solution to solve this ?

EdyHartono avatar May 04 '16 03:05 EdyHartono

@EdyHartono the text value is not supposed to be reset... Any chance you're also passing a different key props to the Select so it's unmounted and created from scratch?

burgalon avatar May 04 '16 18:05 burgalon

@burgalon i made a mistake here, my Select component is something like this <Select name= "assignee" options={this.state.options} onInputChange={this._getUsers}/> and i will setState in the _getUsers function in order to passing down new options array to Select component and everything is working fine except the text value get reseted everytime because of DOM update

EdyHartono avatar May 05 '16 12:05 EdyHartono

I have a lot of code written using Select.Async. I want to add a new option dynamically and select it. Is that possible? Thanks.

vijayst avatar Dec 23 '16 13:12 vijayst

I am using the same thing (Select with onInputChange), but even though options get changed the dropdown menu does not update it stays No results found. It actually updates only if I Blur/Click away and click dropdown arrow icon (so I need to re oepn the menu)? Any tips what am I doing wrong?

alphiii avatar Jan 03 '17 08:01 alphiii

I'm using Select.Async with autoload=false option, and it works as well.

The negative point is that the user is forced to search options by typing a letter.

harmo avatar Jan 05 '17 15:01 harmo

Even if you check the demo it has the same issue http://jedwatson.github.io/react-select/ Can you tell me what version are you using and provide some sample code

alphiii avatar Jan 05 '17 17:01 alphiii

I had the same problem, and solved this in this pull request #1480

Maybe this would be helpful to you as well.

VanCoding avatar Jan 12 '17 11:01 VanCoding

I have the same problem. I've tried to walk through the answers above, but nothing helped. I'm using react-select with redux. When new options are loaded, I see in debugger, that render() function is called. However, instead of new options I see "Not found".

The Select component element is like this: <Select labelKey={'id'} valueKey={'id'} isLoading={this.state.isFetching} value={value} options={options} onInputChange={this.startPoll} onChange={handleSelect}/>

this.startPoll function is like this: startPoll(val, cb) { this.setState({isFetching: true}); this.props.updateInput(val); if (this.timeout) { clearTimeout(this.timeout); } this.timeout = setTimeout(() => { this.setState({isFetching: false}); searchUser(val); }, 2000); return val; } .

searchUser function is just using fetch.

When I add a custom input element, where I type the search value instead of standard Select input, Select options are updated perfectly. But when I type in Select, the only way to update options is to reopen list of options. I'm wondering why does this happen?

heyjohnnyfunt avatar Feb 16 '17 13:02 heyjohnnyfunt

Currently using this solution as a work around:

I like the functionality of the Select.Async ~ so I wanted to keep this functionality, but had a similar problem to where I wanted to manually tigger loadOptions (based on my state, my apiCall function makes an api service call that can pass additional params to scope the data).

<Select.Async
    cache={false}
    ref="selectUser"
    multi={false}
    onChange={this.inputChange.bind(this, "selectUser")}
    loadOptions={this.userApiCall.bind(this)}
    value={this.state.selectUser} />

In another function, I use the reference selectUser to load new options into the Select.Async component.

this.refs.selectUser.loadOptions("")

Doesn't feel like the react way of doing this, but I wanted to keep the Async functionality of this plugin. Definitely going to keep an eye on @VanCoding s solution to this problem.

jamesmarrs avatar Feb 16 '17 22:02 jamesmarrs

@jamesmarrs I can not understand, where do you call your this.refs.selectUser.loadOptions("") function? I've tried the way you do with Async functionality, but it's the same result - options are not updated before I reopen the list.

heyjohnnyfunt avatar Feb 17 '17 07:02 heyjohnnyfunt

@heyjohnnyfunt you should be able to call this.refs.selectUser.loadOptions("") anywhere you want as long as the component is mounted. For example an onChange event for a checkbox within the component.

There could be a delay if your api is dependent on a specific state, so you might want to run it in a success callback:

checkboxChanged(e) {
  this.setState({ onlyAdmins: e.currentTarget.checked }, () => {
      this.refs.selectUser.loadOptions("") // <- Has logic to only pull admins if onlyAdmins is set to true
  })
}

As soon as loadOptions is called, the Select.Async component should indicate its loading.

If you want to post an example specific to your use case I can try and help!

jamesmarrs avatar Feb 17 '17 16:02 jamesmarrs