|
| 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-components'; |
| 14 | +import {PropTable, HeaderInfo, TypeLink, PageDescription, StateTable, ContextTable} from '@react-spectrum/docs'; |
| 15 | +import styles from '@react-spectrum/docs/src/docs.css'; |
| 16 | +import packageData from 'react-aria-components/package.json'; |
| 17 | +import ChevronRight from '@spectrum-icons/workflow/ChevronRight'; |
| 18 | +import {InlineAlert, Content, Heading} from '@adobe/react-spectrum'; |
| 19 | + |
| 20 | +--- |
| 21 | +category: Pickers |
| 22 | +keywords: [autocomplete, search, menu, command palette, aria] |
| 23 | +type: component |
| 24 | +preRelease: alpha |
| 25 | +--- |
| 26 | + |
| 27 | +# Autocomplete |
| 28 | + |
| 29 | +<PageDescription>{docs.exports.UNSTABLE_Autocomplete.description}</PageDescription> |
| 30 | + |
| 31 | +<HeaderInfo |
| 32 | + packageData={packageData} |
| 33 | + componentNames={['UNSTABLE_Autocomplete']} |
| 34 | + sourceData={[ |
| 35 | + {type: 'W3C', url: 'https://w3c.github.io/aria/#aria-autocomplete'} |
| 36 | + ]} /> |
| 37 | + |
| 38 | +<InlineAlert variant="notice" marginTop={60}> |
| 39 | + <Heading>Under construction</Heading> |
| 40 | + <Content>This component is in <strong>alpha</strong>. More documentation is coming soon!</Content> |
| 41 | +</InlineAlert> |
| 42 | + |
| 43 | +## Example |
| 44 | + |
| 45 | +```tsx example |
| 46 | +import {UNSTABLE_Autocomplete as Autocomplete, Menu, MenuItem, useFilter} from 'react-aria-components'; |
| 47 | +import {MySearchField} from './SearchField'; |
| 48 | + |
| 49 | +function Example() { |
| 50 | + let {contains} = useFilter({sensitivity: 'base'}); |
| 51 | + return ( |
| 52 | + <div className="autocomplete"> |
| 53 | + <Autocomplete filter={contains}> |
| 54 | + <MySearchField placeholder="Search commands..." /> |
| 55 | + <Menu> |
| 56 | + <MenuItem>Create new file...</MenuItem> |
| 57 | + <MenuItem>Create new folder...</MenuItem> |
| 58 | + <MenuItem>Assign to...</MenuItem> |
| 59 | + <MenuItem>Assign to me</MenuItem> |
| 60 | + <MenuItem>Change status...</MenuItem> |
| 61 | + <MenuItem>Change priority...</MenuItem> |
| 62 | + <MenuItem>Add label...</MenuItem> |
| 63 | + <MenuItem>Remove label...</MenuItem> |
| 64 | + </Menu> |
| 65 | + </Autocomplete> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +<details> |
| 72 | + <summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show CSS</summary> |
| 73 | +```css hidden |
| 74 | +@import './Menu.mdx' layer(listbox); |
| 75 | +@import './Button.mdx' layer(button); |
| 76 | +@import './SearchField.mdx' layer(searchfield); |
| 77 | +``` |
| 78 | + |
| 79 | +```css |
| 80 | +@import "@react-aria/example-theme"; |
| 81 | + |
| 82 | +.autocomplete { |
| 83 | + display: flex; |
| 84 | + flex-direction: column; |
| 85 | + gap: 12px; |
| 86 | + max-width: 300px; |
| 87 | + height: 180px; |
| 88 | + border: 1px solid var(--border-color); |
| 89 | + padding: 16px; |
| 90 | + border-radius: 10px; |
| 91 | + background: var(--overlay-background); |
| 92 | + |
| 93 | + .react-aria-SearchField { |
| 94 | + width: 100%; |
| 95 | + } |
| 96 | + |
| 97 | + .react-aria-Menu { |
| 98 | + flex: 1; |
| 99 | + overflow: auto; |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +</details> |
| 105 | + |
| 106 | +## Anatomy |
| 107 | + |
| 108 | +`Autocomplete` is a controller for a child text input, such as a [TextField](TextField.html) or [SearchField](SearchField), and a collection component such as a [Menu](Menu.html) or [ListBox](ListBox.html). It enables the user to filter a list of items, and navigate via the arrow keys while keeping focus on the input. |
| 109 | + |
| 110 | +```tsx |
| 111 | +import {UNSTABLE_Autocomplete as Autocomplete, SearchField, Menu} from 'react-aria-components'; |
| 112 | + |
| 113 | +<Autocomplete> |
| 114 | + <SearchField /> |
| 115 | + <Menu /> |
| 116 | +</Autocomplete> |
| 117 | +``` |
| 118 | + |
| 119 | +## Props |
| 120 | + |
| 121 | +<PropTable component={docs.exports.UNSTABLE_Autocomplete} links={docs.links} /> |
0 commit comments