Skip to content

Commit a792c1a

Browse files
authored
docs: add initial Autocomplete hook docs (#7811)
* docs: add Autocomplete hook docs * update header * review edits
1 parent 7df1a84 commit a792c1a

File tree

7 files changed

+113
-8
lines changed

7 files changed

+113
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{/* Copyright 2025 Adobe. All rights reserved.
2+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License. You may obtain a copy
4+
of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
Unless required by applicable law or agreed to in writing, software distributed under
6+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
7+
OF ANY KIND, either express or implied. See the License for the specific language
8+
governing permissions and limitations under the License. */}
9+
10+
import {Layout} from '@react-spectrum/docs';
11+
export default Layout;
12+
13+
import docs from 'docs:@react-aria/autocomplete';
14+
import {FunctionAPI, HeaderInfo, InterfaceType, TypeContext, TypeLink, PageDescription} from '@react-spectrum/docs';
15+
import packageData from '@react-aria/autocomplete/package.json';
16+
import statelyDocs from 'docs:@react-stately/autocomplete';
17+
import {InlineAlert, Content, Heading} from '@adobe/react-spectrum';
18+
19+
---
20+
category: Pickers
21+
keywords: [autocomplete, autosuggest, typeahead, search, aria]
22+
after_version: 3.0.0
23+
---
24+
25+
# useAutocomplete
26+
27+
<PageDescription>{docs.exports.useAutocomplete.description}</PageDescription>
28+
29+
<HeaderInfo
30+
packageData={packageData}
31+
componentNames={['useAutocomplete']}
32+
/>
33+
34+
<InlineAlert variant="notice" marginTop={60}>
35+
<Heading>Under construction</Heading>
36+
<Content>This hook is in <strong>beta</strong>. More documentation is coming soon!</Content>
37+
</InlineAlert>
38+
39+
## API
40+
41+
<FunctionAPI function={docs.exports.useAutocomplete} links={docs.links} />
42+
43+
## Features
44+
45+
Autocomplete can be implemented using the [&lt;datalist&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist) HTML element, but this has limited functionality and behaves differently across browsers.
46+
`useAutocomplete` helps achieve accessible text input and collection that can be styled as needed.
47+
48+
## Anatomy
49+
50+
An autocomplete consists of a text input that displays the current value and a collection of items. Users can type within the input
51+
to filter the collection. `useAutocomplete` handles exposing the correct ARIA attributes for accessibility for each of the elements comprising the autocomplete.
52+
53+
`useAutocomplete` returns props that you should spread onto the appropriate elements:
54+
55+
<TypeContext.Provider value={docs.links}>
56+
<InterfaceType properties={docs.links[docs.exports.useAutocomplete.return.base?.id ?? docs.exports.useAutocomplete.return.id].properties} />
57+
</TypeContext.Provider>
58+
59+
State is managed by the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useAutocompleteState} /> hook from `@react-stately/autocomplete`.
60+
The state object should be passed as an option to `useAutocomplete`.
61+
62+
## Internationalization
63+
64+
`useAutocomplete` handles some aspects of internationalization automatically.
65+
For example, VoiceOver announcements about the item focus, count, and selection are localized.
66+
You are responsible for localizing all labels and option
67+
content that is passed into the autocomplete.

packages/@react-aria/autocomplete/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212
export {useSearchAutocomplete} from './useSearchAutocomplete';
13-
export {UNSTABLE_useAutocomplete} from './useAutocomplete';
13+
export {useAutocomplete} from './useAutocomplete';
1414

1515
export type {AriaSearchAutocompleteOptions, SearchAutocompleteAria} from './useSearchAutocomplete';
1616
export type {AriaSearchAutocompleteProps} from '@react-types/autocomplete';

packages/@react-aria/autocomplete/src/useAutocomplete.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface AutocompleteAria {
5959
* @param props - Props for the autocomplete.
6060
* @param state - State for the autocomplete, as returned by `useAutocompleteState`.
6161
*/
62-
export function UNSTABLE_useAutocomplete(props: AriaAutocompleteOptions, state: AutocompleteState): AutocompleteAria {
62+
export function useAutocomplete(props: AriaAutocompleteOptions, state: AutocompleteState): AutocompleteAria {
6363
let {
6464
inputRef,
6565
collectionRef,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{/* Copyright 2025 Adobe. All rights reserved.
2+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License. You may obtain a copy
4+
of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
Unless required by applicable law or agreed to in writing, software distributed under
6+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
7+
OF ANY KIND, either express or implied. See the License for the specific language
8+
governing permissions and limitations under the License. */}
9+
10+
import {Layout} from '@react-spectrum/docs';
11+
export default Layout;
12+
13+
import docs from 'docs:@react-stately/autocomplete';
14+
import {ClassAPI, HeaderInfo, FunctionAPI, PageDescription} from '@react-spectrum/docs';
15+
import packageData from '@react-stately/autocomplete/package.json';
16+
17+
---
18+
category: Pickers
19+
keywords: [autocomplete, state]
20+
after_version: 3.0.0-alpha.0
21+
---
22+
23+
# useAutocompleteState
24+
25+
<PageDescription>{docs.exports.useAutocompleteState.description}</PageDescription>
26+
27+
<HeaderInfo
28+
packageData={packageData}
29+
componentNames={['useAutocompleteState']} />
30+
31+
## API
32+
33+
<FunctionAPI function={docs.exports.useAutocompleteState} links={docs.links} />
34+
35+
## Interface
36+
37+
<ClassAPI links={docs.links} class={docs.links[docs.exports.useAutocompleteState.return.id]} />
38+

packages/@react-stately/autocomplete/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
export {UNSTABLE_useAutocompleteState} from './useAutocompleteState';
13+
export {useAutocompleteState} from './useAutocompleteState';
1414

1515
export type {AutocompleteProps, AutocompleteStateOptions, AutocompleteState} from './useAutocompleteState';

packages/@react-stately/autocomplete/src/useAutocompleteState.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface AutocompleteStateOptions extends Omit<AutocompleteProps, 'child
4141
/**
4242
* Provides state management for an autocomplete component.
4343
*/
44-
export function UNSTABLE_useAutocompleteState(props: AutocompleteStateOptions): AutocompleteState {
44+
export function useAutocompleteState(props: AutocompleteStateOptions): AutocompleteState {
4545
let {
4646
onInputChange: propsOnInputChange,
4747
inputValue: propsInputValue,

packages/react-aria-components/src/Autocomplete.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {AriaAutocompleteProps, CollectionOptions, UNSTABLE_useAutocomplete} from '@react-aria/autocomplete';
14-
import {AutocompleteState, UNSTABLE_useAutocompleteState} from '@react-stately/autocomplete';
13+
import {AriaAutocompleteProps, CollectionOptions, useAutocomplete} from '@react-aria/autocomplete';
14+
import {AutocompleteState, useAutocompleteState} from '@react-stately/autocomplete';
1515
import {InputContext} from './Input';
1616
import {mergeProps} from '@react-aria/utils';
1717
import {Provider, removeDataAttributes, SlotProps, SlottedContextValue, useSlottedContext} from './utils';
@@ -40,15 +40,15 @@ export function Autocomplete(props: AutocompleteProps) {
4040
let ctx = useSlottedContext(AutocompleteContext, props.slot);
4141
props = mergeProps(ctx, props);
4242
let {filter} = props;
43-
let state = UNSTABLE_useAutocompleteState(props);
43+
let state = useAutocompleteState(props);
4444
let inputRef = useRef<HTMLInputElement | null>(null);
4545
let collectionRef = useRef<HTMLElement>(null);
4646
let {
4747
textFieldProps,
4848
collectionProps,
4949
collectionRef: mergedCollectionRef,
5050
filterFn
51-
} = UNSTABLE_useAutocomplete({
51+
} = useAutocomplete({
5252
...removeDataAttributes(props),
5353
filter,
5454
inputRef,

0 commit comments

Comments
 (0)