Usage
Select component allows user to pick one option from the given data
,
use this component when you need to create custom value selection which is not possible with NativeSelect:
Bare minimum usage example:
Controlled
Note that Select value should always be either string or null:
Data prop
Select support two different data formats:
- An array of strings – use when you do not need to customize item component or display
label
different thanvalue
. - An array of objects with required
value
andlabel
properties and any other additional properties.
Searchable
Set searchable
prop to enable search in select and nothingFound
prop to provide label that will be shown when no options were found:
Clearable
Set clearable
prop to enable clearing select value.
When prop is true and value is selected clear button will replace chevron in right section:
Allow deselect items
To allow items deselection set allowDeselect
, user will also be able to deselect item if clearable
prop is set:
Creatable
Set creatable
and getCreateLabel
props to enable creating new select item.
Note that you will need to handle data state to manage item creation correctly:
Grouping items
Disable items
Large data set
When dropdown height is exceeded dropdown becomes scrollable, to change max-height set maxDropdownHeight
prop with value in px:
Custom item component
You can change select item component and filtering logic that is used in search. To do so you will need to:
- Add extra props to
data
objects - Create
filter
function which determines whether item should be added to the search results - Provide
itemComponent
which will consumedata
objects
Dropdown position
By default, dropdown is placed below the input and when there is not enough space, it flips to be above the input.
To change this behavior, set dropdownPosition
prop:
Animations
By default, dropdown animations are turned off to increase responsiveness. You can enable them by setting optional props:
transition
– premade transition name or custom transition styles object, see Transition component for all available optionstransitionDuration
– transition duration in ms, defaults to 0transitionTimingFunction
– defaults totheme.transitionTimingFunction
Native scrollbars
By default, Select uses ScrollArea to render dropdown.
If you want to use native scrollbars instead, set div
as a dropdown component:
With icon
Invalid state and error
Disabled state
Right section
You can replace icon in right section with rightSection
prop.
Note that in this case clearable
option will not work and will need to handle it yourself:
Input props
Get input element ref
Accessibility
Provide aria-label
in case you use component without label for screen reader support:
If you use clearable
option set clearButtonLabel
: