AsyncSelect
Use AsyncSelect component to load options from a remote source as the user types.
Options are fetched on the following triggers:
- When user enters some text in the input
- When user scrolls through the list of options in dropdown and all the pages of options haven't been fetched yet (pagination)
- On mount when
fetchOnMount
istrue
- On focus (via keyboard or click) when
fetchOnFocus
istrue
For more information, see the documentation of Select.
Note: By default, AsyncSelect looks for id
and label
properties in fetched options. To adapt to custom properties, use the labelKey
and valueKey
props to override the defaults.
Examples
Things to note:
value
prop is always anArray
to provide a consistent interface - no matter if you use multi or single select.value
prop is anObject[]
, wherein values corresponding to both -labelKey
andvalueKey
are present. Since v11.4.0, you can also passid[]
as value to AsyncSelect.valueKeys
will also be present inonChange
function from now on.options
returned byloadOptions
prop is always anObject[]
. Grouping of options is supported with GroupedAsyncSelect. Check next example.
Things to note:
- keys should be unique b/w group options
Things to note:
- Since exclude options is not supported yet, all groups will be collapsed and in disabled state when SelectAll is selected.
- Checked state of SelectAll option needs to be handled by consumer.
- Since, asyncSelect is not aware of all possible options,
valueKeys
andvalue
will be an empty array for when onChangetype
isselectAll
.
Things to note:
- If
loadOnMount
istrue
, options will be fetched on mount irrespective of the value ofminimumInput
. - If
loadOnFocus
istrue
, options will be fetched on focus only if the number of characters entered by user is equal to or exceedsminimumInput
value.
Use when you have some preselected value
s that don't need resolving (i.e. fetching of their corresponding options) by sending them as initialOptions
.
Things to note:
- Options provided as
initialOptions
should always be a subset of all possible options that can returned fromloadOptions
initialOptions
prop when present, is always anObject[]
. Grouping of options is supported with GroupedAsyncSelect.- Change in
initialOptions
on every render will not be honoured byAsyncSelect
.
Things to note:
value
prop is anObject[]
, wherein values corresponding to both -labelKey
andvalueKey
are present. Since v11.4.0, you can also passid[]
as value to AsyncSelect.resolveSelectedOptions
prop will be invoked only once on first mount based on the presence of unresolved selected options with unresolvedvalueKey
s.- Use
resolveValuesOnUpdate
(introduced in 11.7.0) to calculate unresolved values and invokeresolveSelectedOptions
on update as well. options
returned byresolveSelectedOptions
prop is always be anObject[]
. Grouping of options is supported with GroupedAsyncSelect. Check next example.- tombstone will be visible when initial selected options are being resolved.
Things to note:
value
prop is an{ [groupId]: Array<Object|id> }
. Object should have values corresponding to both -labelKey
andvalueKey
.options
returned byresolveSelectedOptions
should be of shape{ [groupId]: Array<Object|id> }
.- tombstone will be visible when initial selected options are being resolved.
useAsyncOptions
hook
To use async select like functionality without rendering the select component, you can use useAsyncOptions
hook. Which would return onPaginate
, onInputChange
and resultCountMap
which can be used with CollapsibleSelectMenu
or simple Menu
to render options as per need. Check the following examples for further understanding.
API
AsyncSelect props
Name | Type | Default | Description |
---|---|---|---|
aria-describedby | string | Sets the aria-describedby attribute. | |
aria-label | string | Sets the aria-label attribute. | |
aria-labelledby | string | Sets the aria-describedby attribute. | |
autoFocus | boolean | Defines if select element is focused on the first mount. | |
backspaceRemoves | boolean | true | Defines if options can be removed by pressing backspace. |
captureScroll | boolean | false | When the user reaches top/bottom of the menu, prevents scroll to the body/scroll-parent |
children | react node | When type is 'overlay', Select is opened in overlay and children works as target element for the popover. For all other types, children is simply ignored. | |
className | string | Enriched style to be provided to Select Root | |
clearAsOption | boolean | false | Add option to clear selected value in the popover instead of clearable icon. |
clearAsOptionLabel | string | Unassign | Defines the label for the option which clears the selected values. |
clearable | boolean | true | Defines if the select value can be cleared. If true a clear icon is rendered when a value is set. |
closeOnSelect | boolean | true | Defines if the menu closes after a selection if made. |
controlRef | ref | Exposes methods to control select's dropdown and other internal state. Check types and examples for more information. | |
creatable | boolean | Defines if new options can be created along with choosing existing options. | |
createOption | function | A custom handler that returns options to be created from the input value (label) entered by the user. It should return Options or Promise<Options>. | |
debounceInterval | number | Number of milliseconds to delay the invocation of input handler. | |
deleteRemoves | boolean | true | Defines if options can be removed by pressing backspace. |
disabled | boolean | Defines if the control is disabled. | |
dropdownFooter | function | Add a footer for dropdown menu | |
escapeClearsValue | boolean | true | Defines if the value is cleared when escape is pressed and the dropdown is closed. |
filterOptions | function | A custom method to filter options to be displayed in the dropdown. | |
filterOutSelected | boolean | true | Defines if currently selected options are filtered out in the dropdown options. Only for `multi` select. |
getOptionLabel | function | A custom method to get a display value for a dropdown option. | |
getValueLabel | function | A custom method to get a display value for a selected option. | |
hideUnresolvedValues | boolean | Unresolved values will not be visible in multi-select. | |
id | string | Sets the id attribute of the internal input element. Allows for usage with labels. | |
initialOptions | array | Initial set of static options | |
inputRef | ref | A ref to access an input element. | |
intent | 'default' | 'success' | 'warning' | 'error' | Defines the intent of the Select. | |
labelKey | string | Defines an option key name for a default label value. | |
limitTags | number | Number of tags which should be rendered rest of the tag will be shown as a popover | |
listenAllInputChanges | boolean | true | Defines if AsyncSelect should listen to all the changes in values for input. |
loadOnFocus | boolean | false | Whether to fetch options on focus. For Overlay select, options will load when search input inside overlay gets focused. |
loadOnMount | boolean | false | Whether to fetch options on mount. |
loadOptions | function | Function for fetching options asynchronously. | |
maxDropdownHeight | string | Sets max height of the dropdown list. | |
minimumInput | number | Minimum number of characters that trigger loadOptions. | |
mountNode | ref | Where to mount the popover. | |
moveSelectedOptionsToTop | boolean | false | Defines if selected options should be moved to the top of options list. |
multi | boolean | Defines if multiple options can be selected. | |
noResultsMsg | react node | Message to be displayed if no options is found for a search query. | |
onBlur | function | Called the onBlur event triggers. | |
onBlurResetsInput | boolean | true | Defines if the input value is reset to an empty string when a blur event happens on the select. |
onChange | function | Change handler of the select to be called when a value is changed. | |
onClose | function | A function that is called when the dropdown closes. | |
onCloseResetsInput | boolean | true | Defines if the input value is reset to an empty string when dropdown is closed. |
onFocus | function | Called the onFocus event triggers. | |
onInputChange | function | Change handler of the search input. | |
onOpen | function | A function that is called when the dropdown opens. | |
onPaste | function | A function that is called when a paste event occurs on the select's input. It receives a single argument of the clipboard event and expects options to be returned.return values from onPaste won't trigger onChange if creatable is false | |
onSelectResetsInput | boolean | true | Defines if the input value is reset to an empty string when a selection is made. |
openOnClick | boolean | true | Defines if the dropdown opens on a click event on the select. |
overlayWidth | string | Sets width of select when type is 'overlay'. Default is '300px'. | |
overrides | custom | Lets you customize all aspects of the component. | |
paginationBuffer | number | Maximum distance (in pixels) from the bottom of the dropdown that triggers the fetch of next page of options. | |
placeholder | string | Sets the placeholder. | |
placement | 'auto' | 'topLeft' | 'top' | 'topRight' | 'rightTop' | 'right' | 'rightBottom' | 'bottomRight' | 'bottom' | 'bottomLeft' | 'leftTop' | 'left' | 'leftBottom' | Controls how to position the dropdown relative to the select. Defaults to `bottom` | |
popoverMinWidth | string | Sets the minWidth of the dropdown container. | |
required | boolean | Defines if the select field is required to have a selection. | |
resolveSelectedOptions | function | Function for fetching options corresponding to the unresolved value keys. It is invoked on mount depending on the presence of unresolved values. | |
resolveValuesOnUpdate | boolean | If true, asyncSelect will calculate unresolvedValues whenever value changes and invokes resolveSelectedOptions. | |
searchable | boolean | true | Defines if the search functionality is enabled. |
searchingText | string | Text to show in dropdown while the options are loading. | |
selectOnTab | boolean | false | When enabled allows to click on/select highlighted item on Tab key press. |
startOpen | boolean | If true, opens the dropdown when the select mounts. | |
style | string | Enriched style to be provided to Select Root | |
type | 'search' | 'select' | 'overlay' | Defines type of the component to be in a select or search mode. When set to 'search' the search icon is rendered on the left and the select arrow icon is not rendered. When set to 'overlay' the select is rendered in overlay. | |
value | array | A current selected value(s). If a selected value has a clearableValue prop set to true it will be rendered as a disabled selected option that can't be cleared. | |
valueKey | string | Defines a key name for an option's unique identifier value. The value of the 'valueKey' prop is used to identify what options are selected or removed from the selection, it also used for default filtering out the selected options from the dropdown list. | |
virtual | boolean | true | Add virtualization to select options |
You can check the implementation of the component for more details.
AsyncSelect exports
You can import this module like so:
import {AsyncSelect} from 'spaceweb/asyncSelect'
It exports the following components or utility functions:
- AsyncSelect
- GroupedAsyncSelect
- STATE_CHANGE_TYPE
- UNGROUPED_ID
- arrayToGroupedOptions
- defaultAsyncSelectFilterOptions
- defaultCreateOption
- normalizeOptions
- useAsyncOptions
- useAsyncSelect
- useGroupedAsyncSelect