|
| 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 [<datalist>](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. |
0 commit comments