Skip to content

Autocomplete

Used to render a text input that allows a user to quickly filter through a list of options to pick one or more values.
  • Alpha
  • Not reviewed for accessibility

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.

Anatomy

Autocomplete.Input

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

Autocomplete.Overlay

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).

Autocomplete.Menu

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

Customizing how menu items are rendered

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.

Sorting menu items

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.

Filtering menu items

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.

Examples

Basic example

Autocomplete.Input with a custom text input

In this example, we're passing a TextInputWithTokens component

Without Autocomplete.Overlay

Render items using ActionList.Item props

Customize sort when the menu is re-opened

In this example, selected items get sorted to the end. By default, they are sorted to the beginning.

Custom filtering

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.

Rendered without 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.

Select multiple values

Select multiple values - new values can be added

Rendered with Autocomplete.Context

The Autocomplete.Context can be used to control the menu open/closed state and customize certain Autocomplete behaviors

Props

Autocomplete

NameTypeDefaultDescription
children
React.ReactNode

Autocomplete.Input

NameTypeDefaultDescription
as
React.ElementType
TextInputThe 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.

Autocomplete.Overlay

NameTypeDefaultDescription
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.

Autocomplete.Menu

NameTypeDefaultDescription
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

Status

Alpha

  • Component props and basic example usage of the component are documented on primer.style/react.
  • Component does not have any unnecessary third-party dependencies.
  • Component can adapt to different themes.
  • Component can adapt to different screen sizes.
  • Component has robust unit test coverage (100% where achievable).
  • Component has visual regression coverage of its default and interactive states.
  • Component does not introduce any axe violations.
  • Component has been manually reviewed by the accessibility team and any resulting issues have been addressed.

Beta

  • Component is used in a production application.
  • Common usage examples are documented on primer.style/react.
  • Common usage examples are documented in storybook stories.
  • Component has been reviewed by a systems designer and any resulting issues have been addressed.
  • Component does not introduce any performance regressions.

Stable

  • Component API has been stable with no breaking changes for at least one month.
  • Feedback on API usability has been sought from developers using the component and any resulting issues have been addressed.
  • Component has corresponding design guidelines documented in the interface guidelines.
  • Component has corresponding Figma component in the Primer Web library.
  • Tooling (such as linters, codemods, etc.) exists to prevent further use of alternatives.