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:
Controlled search
Set searchValue
and onSearchChange
prop to enable controlled search in select:
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:
Styles API
Use data-selected
and data-hovered
attributes to add styles to items depending on their state:
With icon
Invalid state and error
Disabled state
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 element ref
Accessibility
Provide aria-label
in case you use component without label for screen reader support:
If you use clearable
option set clearButtonLabel
:
Select component props
Name | Type | Description |
---|---|---|
allowDeselect | boolean | Allow deselecting items on click |
clearButtonLabel | string | aria-label for clear button |
clearButtonTabIndex | 0 | -1 | Set the clear button tab index to disabled or default after input field |
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 |
disabled | boolean | Disabled input state |
dropdownComponent | any | Change dropdown component, can be used to add native 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, item: SelectItem) => boolean | Function based on which items in dropdown are filtered |
filterDataOnExactSearchMatch | boolean | Should data be filtered when search value exactly matches selected item |
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 in px |
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 | number | Maximum dropdown height in px |
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 value changes |
positionDependencies | any[] | useEffect dependencies to force update dropdown position |
radius | number | "xs" | "sm" | "md" | "lg" | "xl" | Input border-radius from theme or number to set border-radius in px |
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 | Set to true to enable search |
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 |
transition | MantineTransition | Dropdown body appear/disappear transition |
transitionDuration | number | Dropdown body transition duration |
transitionTimingFunction | string | Dropdown body transition timing function, defaults to theme.transitionTimingFunction |
value | string | Controlled input value |
variant | "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 |
Select component Styles API
Name | Static selector | Description |
---|---|---|
dropdown | .mantine-Select-dropdown | Dropdown element |
item | .mantine-Select-item | Item element, rendered inside dropdown |
nothingFound | .mantine-Select-nothingFound | Nothing found label |
separator | .mantine-Select-separator | Divider wrapper |
separatorLabel | .mantine-Select-separatorLabel | Separator Label |
itemsWrapper | .mantine-Select-itemsWrapper | Wraps all items in dropdown |
wrapper | .mantine-Select-wrapper | Root Input element |
invalid | .mantine-Select-invalid | Invalid input modifier |
disabled | .mantine-Select-disabled | Invalid disabled modifier |
icon | .mantine-Select-icon | Input icon wrapper on the left side of the input, controlled by icon prop |
withIcon | .mantine-Select-withIcon | Input element styles when used with icon, controlled by icon prop |
input | .mantine-Select-input | Main input element |
rightSection | .mantine-Select-rightSection | Input right section, controlled by rightSection prop |
root | .mantine-Select-root | Root element |
label | .mantine-Select-label | Label element styles, defined by label prop |
error | .mantine-Select-error | Error element styles, defined by error prop |
description | .mantine-Select-description | Description element styles, defined by description prop |
required | .mantine-Select-required | Required asterisk element styles, defined by required prop |