Autocomplete
Autocomplete user input with any list of options
Import
Source
Docs
Package
Usage
Controlled
Data prop
Autocomplete support two different data formats:
- An array of strings
- An array of objects with required
value
property and any other additional properties
Dynamic data
Group options
Custom item component
Autocomplete item component and filtering logic can be changed. To do so:
- 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
Limit amount of options
By default, Autocomplete will render 5 items at a time, to change that set limit
prop:
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:
DropdownPosition
Animations
By default, dropdown animations are turned off to increase responsiveness. To enable animations set the following 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
With icon
Invalid state and error
Field is required
Disabled state
Input props
Radius
xs
sm
md
lg
xl
Size
xs
sm
md
lg
xl
Get input ref
Accessibility
Provide aria-label
in case component does not have a label for screen reader support:
Autocomplete component props
Name | Type | Description |
---|---|---|
data * | readonly (string | AutocompleteItem)[] | 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: AutocompleteItem) => boolean | Function based on which items in dropdown are filtered |
hoverOnSearchChange | boolean | Hovers the first result when input 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 |
nothingFound | ReactNode | Nothing found label |
onChange | (value: string) => void | Controlled input onChange handler |
onDropdownClose | () => void | Called when dropdown is closed |
onDropdownOpen | () => void | Called when dropdown is opened |
onItemSubmit | (item: AutocompleteItem) => void | Called when item from dropdown was selected |
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 | 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 |
shadow | MantineShadow | Dropdown shadow from theme or any value to set box-shadow |
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 |
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 |
Autocomplete component Styles API
Name | Static selector | Description |
---|---|---|
dropdown | .mantine-Autocomplete-dropdown | Dropdown element |
item | .mantine-Autocomplete-item | Item element, rendered inside dropdown |
nothingFound | .mantine-Autocomplete-nothingFound | Nothing found label |
separator | .mantine-Autocomplete-separator | Divider wrapper |
separatorLabel | .mantine-Autocomplete-separatorLabel | Separator Label |
itemsWrapper | .mantine-Autocomplete-itemsWrapper | Wraps all items in dropdown |
wrapper | .mantine-Autocomplete-wrapper | Root Input element |
icon | .mantine-Autocomplete-icon | Input icon wrapper on the left side of the input, controlled by icon prop |
input | .mantine-Autocomplete-input | Main input element |
rightSection | .mantine-Autocomplete-rightSection | Input right section, controlled by rightSection prop |
root | .mantine-Autocomplete-root | Root element |
label | .mantine-Autocomplete-label | Label element styles, defined by label prop |
error | .mantine-Autocomplete-error | Error element styles, defined by error prop |
description | .mantine-Autocomplete-description | Description element styles, defined by description prop |
required | .mantine-Autocomplete-required | Required asterisk element styles, defined by required prop |