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.1 |
usage | import {useAutocomplete} from '@react-aria/autocomplete' |
API#
useAutocomplete(
(props: ,
, state:
)):
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 |
textFieldProps |
| Props for the autocomplete textfield/searchfield element. These should be passed to the textfield/searchfield aria hooks respectively. |
collectionProps |
| Props for the collection, to be passed to collection's respective aria hook (e.g. useMenu). |
collectionRef | <HTMLElement | null> | Ref to attach to the wrapped collection. |
filter | (
(nodeTextValue: string
)) => boolean | A filter function that returns if the provided collection node should be filtered out of the collection. |
State is managed by the hook from
@react-stately/autocomplete
.
The state object should be passed as an option to useAutocomplete
.
Example#
import {AriaAutocompleteProps, CollectionOptions, useAutocomplete} from '@react-aria/autocomplete';
import {AutocompleteState, useAutocompleteState} from '@react-stately/autocomplete';
import {mergeProps} from '@react-aria/utils';
import React, {createContext, RefObject, useRef} from 'react';
import {Input, InputContext, Label, ListBox, ListBoxItem, Provider, SearchField, SearchFieldContext, SlotProps, SlottedContextValue, Text, UNSTABLE_InternalAutocompleteContext, useFilter, useSlottedContext} from 'react-aria-components';
interface AutocompleteProps extends AriaAutocompleteProps, SlotProps {}
interface InternalAutocompleteContextValue {
filter?: (nodeTextValue: string) => boolean;
collectionProps: CollectionOptions;
collectionRef: RefObject<HTMLElement | null>;
}
const AutocompleteContext = createContext<
SlottedContextValue<Partial<AutocompleteProps>>
>(null);
const AutocompleteStateContext = createContext<AutocompleteState | null>(null);
function Autocomplete(props: AutocompleteProps) {
let { contains } = useFilter({ sensitivity: 'base' });
let filter = (textValue, inputValue) => contains(textValue, inputValue);
let ctx = useSlottedContext(AutocompleteContext, props.slot);
props = mergeProps(ctx, props);
let { disableAutoFocusFirst } = props;
let state = useAutocompleteState(props);
let inputRef = useRef<HTMLInputElement | null>(null);
let collectionRef = useRef<HTMLElement>(null);
let {
textFieldProps,
collectionProps,
collectionRef: mergedCollectionRef,
filter: filterFn
} = useAutocomplete({
...props,
filter,
disableAutoFocusFirst,
inputRef,
collectionRef
}, state);
return (
<Provider
values={[
[AutocompleteStateContext, state],
[SearchFieldContext, textFieldProps],
[InputContext, { ref: inputRef }],
[UNSTABLE_InternalAutocompleteContext, {
filter: filterFn,
collectionProps,
collectionRef: mergedCollectionRef
}]
]}
>
{props.children}
</Provider>
);
}
<div className="autocomplete">
<Autocomplete>
<SearchField>
<Label>Favorite animal</Label>
<Input />
<Text slot="description">Please select a pet below.</Text>
</SearchField>
<ListBox selectionMode="single" aria-label="Possible pets">
<ListBoxItem id="red panda">Red Panda</ListBoxItem>
<ListBoxItem id="cat">Cat</ListBoxItem>
<ListBoxItem id="dog">Dog</ListBoxItem>
<ListBoxItem id="aardvark">Aardvark</ListBoxItem>
<ListBoxItem id="kangaroo">Kangaroo</ListBoxItem>
<ListBoxItem id="snake">Snake</ListBoxItem>
</ListBox>
</Autocomplete>
</div>
import {
AriaAutocompleteProps,
CollectionOptions,
useAutocomplete
} from '@react-aria/autocomplete';
import {
AutocompleteState,
useAutocompleteState
} from '@react-stately/autocomplete';
import {mergeProps} from '@react-aria/utils';
import React, {
createContext,
RefObject,
useRef
} from 'react';
import {
Input,
InputContext,
Label,
ListBox,
ListBoxItem,
Provider,
SearchField,
SearchFieldContext,
SlotProps,
SlottedContextValue,
Text,
UNSTABLE_InternalAutocompleteContext,
useFilter,
useSlottedContext
} from 'react-aria-components';
interface AutocompleteProps
extends AriaAutocompleteProps, SlotProps {}
interface InternalAutocompleteContextValue {
filter?: (nodeTextValue: string) => boolean;
collectionProps: CollectionOptions;
collectionRef: RefObject<HTMLElement | null>;
}
const AutocompleteContext = createContext<
SlottedContextValue<Partial<AutocompleteProps>>
>(null);
const AutocompleteStateContext = createContext<
AutocompleteState | null
>(null);
function Autocomplete(props: AutocompleteProps) {
let { contains } = useFilter({ sensitivity: 'base' });
let filter = (textValue, inputValue) =>
contains(textValue, inputValue);
let ctx = useSlottedContext(
AutocompleteContext,
props.slot
);
props = mergeProps(ctx, props);
let { disableAutoFocusFirst } = props;
let state = useAutocompleteState(props);
let inputRef = useRef<HTMLInputElement | null>(null);
let collectionRef = useRef<HTMLElement>(null);
let {
textFieldProps,
collectionProps,
collectionRef: mergedCollectionRef,
filter: filterFn
} = useAutocomplete({
...props,
filter,
disableAutoFocusFirst,
inputRef,
collectionRef
}, state);
return (
<Provider
values={[
[AutocompleteStateContext, state],
[SearchFieldContext, textFieldProps],
[InputContext, { ref: inputRef }],
[UNSTABLE_InternalAutocompleteContext, {
filter: filterFn,
collectionProps,
collectionRef: mergedCollectionRef
}]
]}
>
{props.children}
</Provider>
);
}
<div className="autocomplete">
<Autocomplete>
<SearchField>
<Label>Favorite animal</Label>
<Input />
<Text slot="description">
Please select a pet below.
</Text>
</SearchField>
<ListBox
selectionMode="single"
aria-label="Possible pets"
>
<ListBoxItem id="red panda">Red Panda</ListBoxItem>
<ListBoxItem id="cat">Cat</ListBoxItem>
<ListBoxItem id="dog">Dog</ListBoxItem>
<ListBoxItem id="aardvark">Aardvark</ListBoxItem>
<ListBoxItem id="kangaroo">Kangaroo</ListBoxItem>
<ListBoxItem id="snake">Snake</ListBoxItem>
</ListBox>
</Autocomplete>
</div>
import {
AriaAutocompleteProps,
CollectionOptions,
useAutocomplete
} from '@react-aria/autocomplete';
import {
AutocompleteState,
useAutocompleteState
} from '@react-stately/autocomplete';
import {mergeProps} from '@react-aria/utils';
import React, {
createContext,
RefObject,
useRef
} from 'react';
import {
Input,
InputContext,
Label,
ListBox,
ListBoxItem,
Provider,
SearchField,
SearchFieldContext,
SlotProps,
SlottedContextValue,
Text,
UNSTABLE_InternalAutocompleteContext,
useFilter,
useSlottedContext
} from 'react-aria-components';
interface AutocompleteProps
extends
AriaAutocompleteProps,
SlotProps {}
interface InternalAutocompleteContextValue {
filter?: (
nodeTextValue: string
) => boolean;
collectionProps:
CollectionOptions;
collectionRef:
RefObject<
HTMLElement | null
>;
}
const AutocompleteContext =
createContext<
SlottedContextValue<
Partial<
AutocompleteProps
>
>
>(null);
const AutocompleteStateContext =
createContext<
| AutocompleteState
| null
>(null);
function Autocomplete(
props:
AutocompleteProps
) {
let { contains } =
useFilter({
sensitivity: 'base'
});
let filter = (
textValue,
inputValue
) =>
contains(
textValue,
inputValue
);
let ctx =
useSlottedContext(
AutocompleteContext,
props.slot
);
props = mergeProps(
ctx,
props
);
let {
disableAutoFocusFirst
} = props;
let state =
useAutocompleteState(
props
);
let inputRef = useRef<
| HTMLInputElement
| null
>(null);
let collectionRef =
useRef<HTMLElement>(
null
);
let {
textFieldProps,
collectionProps,
collectionRef:
mergedCollectionRef,
filter: filterFn
} = useAutocomplete({
...props,
filter,
disableAutoFocusFirst,
inputRef,
collectionRef
}, state);
return (
<Provider
values={[
[
AutocompleteStateContext,
state
],
[
SearchFieldContext,
textFieldProps
],
[InputContext, {
ref: inputRef
}],
[
UNSTABLE_InternalAutocompleteContext,
{
filter:
filterFn,
collectionProps,
collectionRef:
mergedCollectionRef
}
]
]}
>
{props.children}
</Provider>
);
}
<div className="autocomplete">
<Autocomplete>
<SearchField>
<Label>
Favorite animal
</Label>
<Input />
<Text slot="description">
Please select a
pet below.
</Text>
</SearchField>
<ListBox
selectionMode="single"
aria-label="Possible pets"
>
<ListBoxItem id="red panda">
Red Panda
</ListBoxItem>
<ListBoxItem id="cat">
Cat
</ListBoxItem>
<ListBoxItem id="dog">
Dog
</ListBoxItem>
<ListBoxItem id="aardvark">
Aardvark
</ListBoxItem>
<ListBoxItem id="kangaroo">
Kangaroo
</ListBoxItem>
<ListBoxItem id="snake">
Snake
</ListBoxItem>
</ListBox>
</Autocomplete>
</div>
Show CSS
@import "@react-aria/example-theme";
.autocomplete {
display: flex;
flex-direction: column;
gap: 12px;
max-width: 300px;
height: 180px;
border: 1px solid var(--border-color);
padding: 16px;
border-radius: 10px;
background: var(--overlay-background);
.react-aria-SearchField {
width: 100%;
}
.react-aria-ListBox {
flex: 1;
overflow: auto;
}
.react-aria-Label {
margin-bottom: .5em;
}
}
@import "@react-aria/example-theme";
.autocomplete {
display: flex;
flex-direction: column;
gap: 12px;
max-width: 300px;
height: 180px;
border: 1px solid var(--border-color);
padding: 16px;
border-radius: 10px;
background: var(--overlay-background);
.react-aria-SearchField {
width: 100%;
}
.react-aria-ListBox {
flex: 1;
overflow: auto;
}
.react-aria-Label {
margin-bottom: .5em;
}
}
@import "@react-aria/example-theme";
.autocomplete {
display: flex;
flex-direction: column;
gap: 12px;
max-width: 300px;
height: 180px;
border: 1px solid var(--border-color);
padding: 16px;
border-radius: 10px;
background: var(--overlay-background);
.react-aria-SearchField {
width: 100%;
}
.react-aria-ListBox {
flex: 1;
overflow: auto;
}
.react-aria-Label {
margin-bottom: .5em;
}
}
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.