AsyncSelect

Selected .
open

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 is true
  • On focus (via keyboard or click) when fetchOnFocus is true

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

Basic usage

Select...
open

Limit number of tags rendered

Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
Select...
open

Things to note:

  • value prop is always an Array to provide a consistent interface - no matter if you use multi or single select.
  • value prop is an Object[], wherein values corresponding to both - labelKey and valueKey are present. Since v11.4.0, you can also pass id[] as value to AsyncSelect. valueKeys will also be present in onChange function from now on.
  • options returned by loadOptions prop is always an Object[]. Grouping of options is supported with GroupedAsyncSelect. Check next example.

Basic Grouped usage

You are currently using a collapsible grouped menu. To move to next group press Shift and down arrow. To move to previous group press Shift and up arrow. Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
Select...
open

Things to note:

  • keys should be unique b/w group options

Grouped Select with SelectAllOption

You are currently using a collapsible grouped menu. To move to next group press Shift and down arrow. To move to previous group press Shift and up arrow. Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
Select...
open

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 and value will be an empty array for when onChange type is selectAll.

Usage with minimum input

Select...
open

Things to note:

  • If loadOnMount is true, options will be fetched on mount irrespective of the value of minimumInput.
  • If loadOnFocus is true, options will be fetched on focus only if the number of characters entered by user is equal to or exceeds minimumInput value.

Usage with loadOnFocus

Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
Select...
open

Usage with prefetched options

Select...
open

Usage with resolved selected options

Beth's MythologGuy from The Bachelor
Selected Beth's Mytholog,Guy from The Bachelor. Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
open

Use when you have some preselected values 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 from loadOptions
  • initialOptions prop when present, is always an Object[]. Grouping of options is supported with GroupedAsyncSelect.
  • Change in initialOptions on every render will not be honoured by AsyncSelect.

Usage with unresolved selected options

Beth's Mytholog
Selected 2,3,4,5,Beth's Mytholog. Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
open

Things to note:

  • value prop is an Object[], wherein values corresponding to both - labelKey and valueKey are present. Since v11.4.0, you can also pass id[] as value to AsyncSelect.
  • resolveSelectedOptions prop will be invoked only once on first mount based on the presence of unresolved selected options with unresolved valueKeys.
  • Use resolveValuesOnUpdate(introduced in 11.7.0) to calculate unresolved values and invoke resolveSelectedOptions on update as well.
  • options returned by resolveSelectedOptions prop is always be an Object[]. Grouping of options is supported with GroupedAsyncSelect. Check next example.
  • tombstone will be visible when initial selected options are being resolved.

Usage with unresolved selected options grouped select

Beth's Mytholog
Selected 2,5,3,4,Beth's Mytholog. You are currently using a collapsible grouped menu. To move to next group press Shift and down arrow. To move to previous group press Shift and up arrow. Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
open

Things to note:

  • value prop is an { [groupId]: Array<Object|id> }. Object should have values corresponding to both - labelKey and valueKey.
  • options returned by resolveSelectedOptions should be of shape { [groupId]: Array<Object|id> }.
  • tombstone will be visible when initial selected options are being resolved.
Select...
open

Usage with createOption returning Promise

Use Left and Right arrow keys to navigate between selected values. Enter BackSpace to remove currently focused value
Select...
open

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.

with Collapsible Menu

with Single Menu


API

AsyncSelect props

NameTypeDefaultDescription
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
trueDefines if options can be removed by pressing backspace.
captureScroll
boolean
falseWhen 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
falseAdd option to clear selected value in the popover instead of clearable icon.
clearAsOptionLabel
string
UnassignDefines the label for the option which clears the selected values.
clearable
boolean
trueDefines if the select value can be cleared. If true a clear icon is rendered when a value is set.
closeOnSelect
boolean
trueDefines 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
trueDefines if options can be removed by pressing backspace.
disabled
boolean
Defines if the control is disabled.
escapeClearsValue
boolean
trueDefines 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
trueDefines 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
trueDefines if AsyncSelect should listen to all the changes in values for input.
loadOnFocus
boolean
falseWhether to fetch options on focus. For Overlay select, options will load when search input inside overlay gets focused.
loadOnMount
boolean
falseWhether 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
falseDefines 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
trueDefines 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
trueDefines 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
trueDefines if the input value is reset to an empty string when a selection is made.
openOnClick
boolean
trueDefines 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
trueDefines if the search functionality is enabled.
searchingText
string
Text to show in dropdown while the options are loading.
selectOnTab
boolean
falseWhen 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
trueAdd 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
  • arrayToGroupedOptions
  • defaultAsyncSelectFilterOptions
  • defaultCreateOption
  • normalizeOptions
  • useAsyncOptions