The Autocomplete
component is comprised of an Autocomplete.Input
component that a user types into, and a Autocomplete.Menu
component that displays the list of selectable values.
The text input is used to filter the options in the dropdown menu. It is also used to show the selected value (or values).
The default input rendered is the TextInput
component. A different text input component may be rendered by passing a different component to the as
prop.
The Autocomplete.Input
should not be rendered without a <label>
who's htmlFor
prop matches the Autocomplete.Input
's id
prop
The Autocomplete.Overlay
wraps the Autocomplete.Menu
to display it in an Overlay component.
This component takes the same props as the Overlay
component.
Most Autocomplete
implementations will use the Autocomplete.Overlay
component, but there could be special cases where the Autocomplete.Menu
should be rendered directly after the Autocomplete.Input
(for example: an Autocomplete
that is already being rendered in an Overlay
).
The Autocomplete.Menu
component renders a list of selectable options in a non-modal dialog. The list is filtered and sorted to make it as easy as possible to find the option/s that a user is looking for.
The Autocomplete.Menu
component should be passed an aria-labelledby
prop that matches the id
prop of the <label>
associated with the Autocomplete.Input
By default, menu items are just rendered as a single line of text. The list in the menu is rendered using the Action List component, so menu items can be rendered with all of the same options as Action List items.
However, the renderGroup
, groupMetadata
, and renderItem
props have not been implemented yet.
Items can be displayed in any order that makes sense, but the Autocomplete.Menu
component comes with a default sort behavior to make it easy to find selected items. The default behavior is to sort selected items to the top of the list after the menu has been closed.
A function may be passed to the sortOnCloseFn
prop if this default sorting logic is not helpful for your use case. The sort function will be only be called after the menu is closed so that items don't shift while users are trying to make a selection.
By default, menu items are filtered based on whether or not they match the value of the text input. The default filter is case-insensitive.
A function may be passed to the filterFn
prop if this default filtering behavior does not make sense for your use case.
In this example, we're passing a TextInputWithTokens component
Autocomplete.Overlay
ActionList.Item
propsIn this example, selected items get sorted to the end. By default, they are sorted to the beginning.
In this example, we show any items whose text
contains the input value. By default, we only show items that start with the input value.
Autocomplete.Overlay
with a customScrollContainerRef
If a Autocomplete.Menu
is rendered without an Autocomplete.Overlay
inside of a scrollable container, the ref of the scrollable container must be passed to the customScrollContainerRef
to ensure that highlighted items are always scrolled into view.
Autocomplete.Context
The Autocomplete.Context
can be used to control the menu open/closed state and customize certain Autocomplete
behaviors
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode |
Name | Type | Default | Description |
---|---|---|---|
as | React.ElementType | TextInput | The underlying element to render — either a HTML element name or a React component. |
Additional props are passed to the <TextInput> element. See TextInput docs for a list of props accepted by the <TextInput> element. |
Name | Type | Default | Description |
---|---|---|---|
menuAnchorRef | React.RefObject<HTMLElement> | ||
children | React.ReactNode | ||
overlayProps Deprecated | Partial<OverlayProps> | Props to be spread on the internal `Overlay` component. | |
Additional props are passed to the <Overlay> element. See Overlay docs for a list of props accepted by the <Overlay> element. |
Name | Type | Default | Description |
---|---|---|---|
items Required | T[] | The options for field values that are displayed in the dropdown menu. One or more may be selected depending on the value of the `selectionVariant` prop. | |
selectedItemIds Required | (string | number)[] | The IDs of the selected items | |
aria-labelledby Required | string | ||
addNewItem | Omit<T, 'id' | 'leadingVisual' | 'onAction'> & { handleAddItem: (item: Omit<T, 'leadingVisual' | 'onAction'>) => void; } | A menu item that is used to allow users make a selection that is not available in the array passed to the `items` prop. This menu item gets appended to the end of the list of options. | |
emptyStateText | React.ReactNode | false | The text that appears in the menu when there are no options in the array passed to the `items` prop. | |
filterFn | (item: T, i: number) => boolean | A custom function used to filter the options in the array passed to the `items` prop. By default, we filter out items that don't match the value of the autocomplete text input. The default filter is not case-sensitive. | |
loading | boolean | Whether the data is loading for the menu items | |
sortOnCloseFn | (itemIdA: string | number, itemIdB: string | number) => number | The sort function that is applied to the options in the array passed to the `items` prop after the user closes the menu. By default, selected items are sorted to the top after the user closes the menu. | |
selectionVariant | 'single' | 'multiple' | Whether there can be one item selected from the menu or multiple items selected from the menu | |
onOpenChange | (open: boolean) => void | Function that gets called when the menu is opened or closed | |
onSelectedChange | (item: T | T[]) => void | The function that is called when an item in the list is selected or deselected | |
customScrollContainerRef | React.MutableRefObject<HTMLElement | null> | If the menu is rendered in a scrolling element other than the `Autocomplete.Overlay` component, pass the ref of that element to `customScrollContainerRef` to ensure the container automatically scrolls when the user highlights an item in the menu that is outside the scroll container |