beta

useAutocomplete

Provides the behavior and accessibility implementation for an autocomplete component. An autocomplete combines a text input with a collection, allowing users to filter the collection's contents match a query.

installyarn add @react-aria/autocomplete
version3.0.0-beta.0
usageimport {useAutocomplete} from '@react-aria/autocomplete'

API#


useAutocomplete( (props: AriaAutocompleteOptions, , state: AutocompleteState )): AutocompleteAria

Features#


Autocomplete can be implemented using the <datalist> HTML element, but this has limited functionality and behaves differently across browsers. useAutocomplete helps achieve accessible text input and collection that can be styled as needed.

Anatomy#


An autocomplete consists of a text input that displays the current value and a collection of items. Users can type within the input to filter the collection. useAutocomplete handles exposing the correct ARIA attributes for accessibility for each of the elements comprising the autocomplete.

useAutocomplete returns props that you should spread onto the appropriate elements:

NameTypeDescription
textFieldPropsAriaTextFieldPropsProps for the autocomplete textfield/searchfield element. These should be passed to the textfield/searchfield aria hooks respectively.
collectionPropsCollectionOptionsProps for the collection, to be passed to collection's respective aria hook (e.g. useMenu).
collectionRefRefObject<HTMLElementnull>Ref to attach to the wrapped collection.
filter( (nodeTextValue: string )) => booleanA filter function that returns if the provided collection node should be filtered out of the collection.

State is managed by the useAutocompleteState hook from @react-stately/autocomplete. The state object should be passed as an option to useAutocomplete.

Internationalization#


useAutocomplete handles some aspects of internationalization automatically. For example, VoiceOver announcements about the item focus, count, and selection are localized. You are responsible for localizing all labels and option content that is passed into the autocomplete.