admin管理员组文章数量:1391947
I am using Async react-select for my ponent and can't solve an issue with default Options . Docs couldn't help me , because there were not such an example related to my issue .
const campaignToOption = campaign => ({value: campaign.id, label: campaign.name});
const loadOptionsWrapper = fetchAll =>
input => fetchAll({
_limit: 10000,
q: input,
_sort: 'name',
_order: 'asc',
}).then(campaigns => _(campaigns).reject('meta').map(campaignToOption).values());
const defaultProps = {
options: [],
placeholder: 'Campaign: Not selected',
};
const CampaignFilter = props => {
return <Async
defaultOptions={[{label: 'All Campaigns', value: '-2', group: true}]}
loadOptions={loadOptionsWrapper(props?.actions?.fetchAllCampaigns)}
{...defaultProps}
{...props}
/>;
};
export default CampaignFilter;
I want to add an option which will let the users to select all options . So in case of AsyncSelect , options are being loaded asynchronously. Docs say we can use defaultOptions.
The defaultOptions prop determines "when" your remote request is initially fired. There are two valid values for this property. Providing an option array to this prop will populate the initial set of options used when opening the select, at which point the remote load only occurs when filtering the options (typing in the control). Providing the prop by itself (or with 'true') tells the control to immediately fire the remote request, described by your loadOptions, to get those initial values for the Select.
And when us it , i only get that default option in my dropdown like this`
But my goal is to get a ponent like in this pic `
I am using Async react-select for my ponent and can't solve an issue with default Options . Docs couldn't help me , because there were not such an example related to my issue .
const campaignToOption = campaign => ({value: campaign.id, label: campaign.name});
const loadOptionsWrapper = fetchAll =>
input => fetchAll({
_limit: 10000,
q: input,
_sort: 'name',
_order: 'asc',
}).then(campaigns => _(campaigns).reject('meta').map(campaignToOption).values());
const defaultProps = {
options: [],
placeholder: 'Campaign: Not selected',
};
const CampaignFilter = props => {
return <Async
defaultOptions={[{label: 'All Campaigns', value: '-2', group: true}]}
loadOptions={loadOptionsWrapper(props?.actions?.fetchAllCampaigns)}
{...defaultProps}
{...props}
/>;
};
export default CampaignFilter;
I want to add an option which will let the users to select all options . So in case of AsyncSelect , options are being loaded asynchronously. Docs say we can use defaultOptions.
The defaultOptions prop determines "when" your remote request is initially fired. There are two valid values for this property. Providing an option array to this prop will populate the initial set of options used when opening the select, at which point the remote load only occurs when filtering the options (typing in the control). Providing the prop by itself (or with 'true') tells the control to immediately fire the remote request, described by your loadOptions, to get those initial values for the Select.
And when us it , i only get that default option in my dropdown like this`
But my goal is to get a ponent like in this pic `
Share Improve this question edited Feb 5, 2019 at 13:52 Norayr Ghukasyan asked Feb 5, 2019 at 13:10 Norayr GhukasyanNorayr Ghukasyan 1,4182 gold badges19 silver badges39 bronze badges 4-
Your first picture shows an option 'All campaigns', but I don't see how that is added in your code. Looks like you are setting options to an empty array in
defaultProps
. Where did that 'All campaigns' option e from? Also I think it would be helpful if you can provide a simplified codesandbox. – mehamasum Commented Feb 5, 2019 at 13:47 - I updated my code... – Norayr Ghukasyan Commented Feb 5, 2019 at 13:52
- When you start typing, do the other options show up? – mehamasum Commented Feb 5, 2019 at 13:55
- Yes , it shows the options list filtered by input value. – Norayr Ghukasyan Commented Feb 5, 2019 at 13:57
1 Answer
Reset to default 4I think the best way to achieve your desired result is to add a custom option to whatever campaignToOption
returns.
For example you could use this method to append a special option to your array:
const appendSpecialOption = filteredOptions => {
return [
{ label: "All Campaigns", value: "-2", group: true },
...filteredOptions
];
};
Keep defaultOptions
to True (not an array).
Here is a simplified version of your scenario but I hope it helps:
本文标签: javascriptHow to add defaultOption to AsyncSelectStack Overflow
版权声明:本文标题:javascript - How to add defaultOption to AsyncSelect? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744696674a2620319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论