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 undefined:
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:
Controlled search
Set searchValue
and onSearchChange
prop to enable controlled search in select:
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
Disabled state
Disable items
Large data set
When dropdown height is exceeded dropdown becomes scrollable, to change max-height set maxDropdownHeight
prop with value:
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
Custom label component
Apart from itemComponent
you can customize appearance of label by providing valueComponent
:
Max selected values
Disable selected item filtering
When used along filter
, be aware that the second parameter selected
will always be false
.
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
Read only
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 ref
Accessibility
Provide aria-label
in case you use component without label for screen reader support:
If you use clearable
option set clear button label:
MultiSelect component props
Name | Type | Description |
---|---|---|
clearButtonProps | Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "key" | keyof ButtonHTMLAttributes<...>> | Props added to clear button |
clearSearchOnBlur | boolean | Clear search field value on blur |
clearSearchOnChange | boolean | Clear search value when item is selected |
clearable | boolean | Allow to clear item |
creatable | boolean | Allow creatable option |
data * | readonly (string | SelectItem)[] | Select data used to render items in dropdown |
defaultValue | string[] | Uncontrolled input defaultValue |
description | ReactNode | Input description, displayed after label |
descriptionProps | Record<string, any> | Props spread to description element |
disableSelectedItemFiltering | boolean | Disable removing selected items from the list |
disabled | boolean | Disabled input state |
dropdownComponent | any | Change dropdown component, can be used to add custom scrollbars |
dropdownPosition | "bottom" | "top" | "flip" | Dropdown positioning behavior |
error | ReactNode | Displays error message after input |
errorProps | Record<string, any> | Props spread to error element |
filter | (value: string, selected: boolean, item: SelectItem) => boolean | Function based on which items in dropdown are filtered |
getCreateLabel | (query: string) => ReactNode | Function to get create Label |
hoverOnSearchChange | boolean | Hovers the first result when search query changes |
icon | ReactNode | Adds icon on the left side of input |
iconWidth | Width<string | number> | Width of icon section |
initiallyOpened | boolean | Initial dropdown opened state |
inputContainer | (children: ReactNode) => ReactNode | Input container component, defaults to React.Fragment |
inputWrapperOrder | ("input" | "label" | "error" | "description")[] | Controls order of the Input.Wrapper elements |
itemComponent | FC<any> | Change item renderer |
label | ReactNode | Input label, displayed before input |
labelProps | Record<string, any> | Props spread to label element |
limit | number | Limit amount of items displayed at a time for searchable select |
maxDropdownHeight | string | number | Maximum dropdown height |
maxSelectedValues | number | Limit amount of items selected |
nothingFound | ReactNode | Nothing found label |
onChange | (value: string[]) => void | Controlled input onChange handler |
onCreate | (query: string) => string | SelectItem | Called when create option is selected |
onDropdownClose | () => void | Called when dropdown is closed |
onDropdownOpen | () => void | Called when dropdown is opened |
onSearchChange | (query: string) => void | Called each time search query changes |
portalProps | Omit<PortalProps, "children" | "withinPortal"> | Props to pass down to the portal when withinPortal is true |
positionDependencies | any[] | useEffect dependencies to force update dropdown position |
radius | number | "xs" | "sm" | "md" | "lg" | "xl" | Key of theme.radius or any valid CSS value to set border-radius, theme.defaultRadius by default |
required | boolean | Adds required attribute to the input and red asterisk on the right side of label Sets required on input element |
rightSection | ReactNode | Right section of input, similar to icon but on the right |
rightSectionProps | Record<string, any> | Props spread to rightSection div element |
rightSectionWidth | Width<string | number> | Width of right section, is used to calculate input padding-right |
searchValue | string | Controlled search input value |
searchable | boolean | Enable items searching |
selectOnBlur | boolean | Select highlighted item on blur |
shadow | MantineShadow | Dropdown shadow from theme or any value to set box-shadow |
shouldCreate | (query: string, data: SelectItem[]) => boolean | Function to determine if create label should be displayed |
size | "xs" | "sm" | "md" | "lg" | "xl" | Input size |
switchDirectionOnFlip | boolean | Whether to switch item order and keyboard navigation on dropdown position flip |
transitionProps | Partial<Omit<TransitionProps, "mounted">> | Props added to Transition component that used to animate dropdown presence, use to configure duration and animation type, { duration: 0, transition: 'fade' } by default |
value | string[] | Controlled input value |
valueComponent | FC<any> | Component used to render values |
variant | Variants<"unstyled" | "default" | "filled"> | Defines input appearance, defaults to default in light color scheme and filled in dark |
withAsterisk | boolean | Determines whether required asterisk should be rendered, overrides required prop, does not add required attribute to the input |
withinPortal | boolean | Whether to render the dropdown in a Portal |
wrapperProps | Record<string, any> | Properties spread to root element |
zIndex | ZIndex | Dropdown z-index |
MultiSelect component Styles API
Name | Static selector | Description |
---|---|---|
wrapper | .mantine-MultiSelect-wrapper | Root Input element |
dropdown | .mantine-MultiSelect-dropdown | Dropdown element |
item | .mantine-MultiSelect-item | Item element, rendered inside dropdown |
nothingFound | .mantine-MultiSelect-nothingFound | Nothing found label |
values | .mantine-MultiSelect-values | Values wrapper |
value | .mantine-MultiSelect-value | Value element |
searchInput | .mantine-MultiSelect-searchInput | Search input, rendered after all values |
defaultValue | .mantine-MultiSelect-defaultValue | Default value component wrapper |
defaultValueRemove | .mantine-MultiSelect-defaultValueRemove | Default value remove control |
defaultValueLabel | .mantine-MultiSelect-defaultValueLabel | Default value label |
separator | .mantine-MultiSelect-separator | Divider wrapper |
separatorLabel | .mantine-MultiSelect-separatorLabel | Divider Label |
itemsWrapper | .mantine-MultiSelect-itemsWrapper | Wraps all items in dropdown |
icon | .mantine-MultiSelect-icon | Input icon wrapper on the left side of the input, controlled by icon prop |
input | .mantine-MultiSelect-input | Main input element |
root | .mantine-MultiSelect-root | Root element |
label | .mantine-MultiSelect-label | Label element styles, defined by label prop |
error | .mantine-MultiSelect-error | Error element styles, defined by error prop |
description | .mantine-MultiSelect-description | Description element styles, defined by description prop |
required | .mantine-MultiSelect-required | Required asterisk element styles, defined by required prop |