-
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
[1.0.0-beta11] Setting an initial value from loadOptions promise #861
Comments
Ok, I actually found a solution to this. I'm binding the keyword Internal Select component setup: var SelectComponent = Select;
if (has(props, 'loadOptions')) {
SelectComponent = Select.Async;
props.loadOptions = props.loadOptions.bind(this);
}
return (
<SelectComponent
ref='select'
{...props}
onChange={this.handleChange}
value={this.state.value || value}
/>
); Custom Select component used in parent component: <FormSelect
loadOptions={function() {
return axios.get('/my-endpoint')
.then((response)=>{
return response.data.data;
})
.then((data)=>{
var options = data.map((obj)=>{
return {
value: obj.id,
label: obj.name,
};
});
this.setState({
value: options[0]
});
return {
options
};
});
}} |
This worked for me! I think it'd be cleaner to allow for setting a property on the option that designated it as the default, but this seems to work well. |
Here's my hack: <Select
loadOptions={(() => {
let initialLoad = true;
return input => {
if(initialLoad) {
initialLoad = false;
return Promise.resolve({
options: [formData.s2customer],
complete: false,
});
}
if(input.length < 2) {
return Promise.resolve({
options: [],
complete: false,
});
}
return ajax(customerSelect2Url, {
method: 'GET',
queryParams: {term: input}
}).then(res => {
if(!res.ok) {
throw new Error("Error fetching customer data");
}
//console.log('res.data',res.data);
return {
options: res.data.results,
complete: !res.data.more,
}
})
}
})()}
/> The key is to set this dummy |
Hello - In an effort to sustain the 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 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. |
As the title states, I'd like to set an initial/default value from the response that gets returned from the loadOptions promise in Select.Async. Am I missing something or is this possible through the API?
The text was updated successfully, but these errors were encountered: