MultiSelect
Usage
MultiSelect component allows user to pick any amount of option from the given data
:
A bare minimum example:
Controlled
Note that MultiSelect value should always be an array of either string or null:
Data prop
MultiSelect 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 all values at once.
When prop is true and at least value is selected clear button will replace chevron in right section:
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:
Group 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
andvalueComponent
which will consumedata
objects
You can add any other fields to your data:
Based on this data shape you can create custom filter
function and itemComponent
:
Custom label component
Apart from itemComponent
you can customize appearance of label by providing valueComponent
:
Max selected values
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:
Performance
If you have a large data set (> 100 items) you will have to optimize items rendering.
The best strategy is to use searchable
option with limit
:
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, MultiSelect uses ScrollArea to render dropdown.
If you want to use native scrollbars instead, set div
as a dropdown component:
With icon
You can use any React node as icon:
Invalid state and error
Disabled state
In disabled state:
- options to remove, add or search is disabled
- input cannot be cleared with clear button
- cursor is changed to
not-allowed
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
Component supports all props from Input and InputWrapper components:
Get input ref
You can get input ref by passing ref
prop to Select component:
Accessibility
Provide aria-label
in case you use component without label for screen reader support:
If you use clearable
option set clearButtonLabel
: