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.
install | yarn add @react-aria/autocomplete |
---|---|
version | 3.0.0-beta.6 |
usage | import {useAutocomplete} from '@react-aria/autocomplete' |
Under construction
API#
useAutocomplete<T>(
(props: AriaAutocompleteOptions<T>,
, state: AutocompleteState
)): AutocompleteAria<T>
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:
Name | Type | Description |
inputProps | InputProps | Props for the autocomplete input element. These should be passed to the input's aria hooks (e.g. useTextField/useSearchField/etc) respectively. |
collectionProps | CollectionOptions | Props for the collection, to be passed to collection's respective aria hook (e.g. useMenu). |
collectionRef | RefObject<HTMLElement | null> | Ref to attach to the wrapped collection. |
filter | (
(nodeTextValue: string,
, node: Node<T>
)) => boolean | A 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.