Skip to content

Commit 1914f61

Browse files
committed
docs: add useAutocomplete example
1 parent 27e5ef1 commit 1914f61

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

packages/@react-aria/autocomplete/docs/useAutocomplete.mdx

+146
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ export default Layout;
1212

1313
import docs from 'docs:@react-aria/autocomplete';
1414
import {FunctionAPI, HeaderInfo, InterfaceType, TypeContext, TypeLink, PageDescription} from '@react-spectrum/docs';
15+
import i18nDocs from 'docs:@react-aria/i18n';
1516
import packageData from '@react-aria/autocomplete/package.json';
1617
import statelyDocs from 'docs:@react-stately/autocomplete';
18+
import ChevronRight from '@spectrum-icons/workflow/ChevronRight';
1719
import {InlineAlert, Content, Heading} from '@adobe/react-spectrum';
1820

1921
---
@@ -59,6 +61,150 @@ to filter the collection. `useAutocomplete` handles exposing the correct ARIA at
5961
State is managed by the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useAutocompleteState} /> hook from `@react-stately/autocomplete`.
6062
The state object should be passed as an option to `useAutocomplete`.
6163

64+
65+
## Example
66+
67+
```tsx example export=true
68+
import {Item} from '@react-stately/collections';
69+
import {useButton} from '@react-aria/button';
70+
import {useAutocompleteState} from '@react-stately/autocomplete'
71+
import {useAutocomplete} from '@react-aria/autocomplete';
72+
import {useFilter} from '@react-aria/i18n';
73+
import {UNSTABLE_useFilteredListState} from '@react-stately/list';
74+
75+
// Reuse the ListBox, Popover, and Button from your component library. See below for details.
76+
import {ListBox} from 'your-component-library';
77+
78+
function Autocomplete(props) {
79+
// Setup filter function and state.
80+
let {contains} = useFilter({sensitivity: 'base'});
81+
let state = useAutocompleteState(props);
82+
83+
// Setup refs and get props for child elements.
84+
let ref = React.useRef(null);
85+
let inputRef = React.useRef(null);
86+
87+
let {textFieldProps, collectionProps, collectionRef, filter} = useAutocomplete(
88+
{
89+
...props,
90+
inputRef,
91+
collectionRef: ref,
92+
filter: contains,
93+
},
94+
state
95+
);
96+
97+
return (
98+
<div style={{display: 'inline-flex', flexDirection: 'column'}}>
99+
<label>{props.label}</label>
100+
<div>
101+
<input
102+
{...textFieldProps}
103+
ref={inputRef}
104+
style={{
105+
height: 24,
106+
boxSizing: 'border-box',
107+
marginRight: 0,
108+
fontSize: 16
109+
}} />
110+
<ListBox
111+
{...collectionProps}
112+
filter={filter}
113+
listBoxRef={collectionRef}
114+
state={state}>
115+
{props.children}
116+
</ListBox>
117+
</div>
118+
</div>
119+
);
120+
}
121+
122+
<Autocomplete label="Favorite Animal">
123+
<Item key="red panda">Red Panda</Item>
124+
<Item key="cat">Cat</Item>
125+
<Item key="dog">Dog</Item>
126+
<Item key="aardvark">Aardvark</Item>
127+
<Item key="kangaroo">Kangaroo</Item>
128+
<Item key="snake">Snake</Item>
129+
</Autocomplete>
130+
```
131+
132+
133+
### ListBox
134+
135+
The `ListBox` and `Option` components are used to show the filtered list of options as the
136+
user types in the ComboBox. They can also be shared with other components like a [Select](useSelect.html). See
137+
[useListBox](useListBox.html) for more examples, including sections and more complex items.
138+
139+
<details>
140+
<summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show code</summary>
141+
142+
```tsx example export=true render=false
143+
import {useListBox, useOption} from '@react-aria/listbox';
144+
145+
function ListBox(props) {
146+
let {listBoxRef, state} = props;
147+
let newState = UNSTABLE_useFilteredListState(state, props.filter);
148+
let {listBoxProps} = useListBox(props, newState, listBoxRef);
149+
150+
return (
151+
<ul
152+
{...listBoxProps}
153+
ref={listBoxRef}
154+
style={{
155+
margin: 0,
156+
padding: 0,
157+
listStyle: "none",
158+
maxHeight: 150,
159+
overflow: "auto",
160+
minWidth: 200
161+
}}>
162+
{[...newState.collection].map(item => (
163+
<Option
164+
key={item.key}
165+
item={item}
166+
state={state} />
167+
))}
168+
</ul>
169+
);
170+
}
171+
172+
function Option({item, state}) {
173+
let ref = React.useRef(null);
174+
let {optionProps, isSelected, isFocused, isDisabled} = useOption({key: item.key}, state, ref);
175+
176+
let backgroundColor;
177+
let color = 'black';
178+
179+
if (isSelected) {
180+
backgroundColor = 'blueviolet';
181+
color = 'white';
182+
} else if (isFocused) {
183+
backgroundColor = 'gray';
184+
} else if (isDisabled) {
185+
backgroundColor = 'transparent';
186+
color = 'gray';
187+
}
188+
189+
return (
190+
<li
191+
{...optionProps}
192+
ref={ref}
193+
style={{
194+
background: backgroundColor,
195+
color: color,
196+
padding: '2px 5px',
197+
outline: 'none',
198+
cursor: 'pointer'
199+
}}>
200+
{item.rendered}
201+
</li>
202+
);
203+
}
204+
```
205+
206+
</details>
207+
62208
## Internationalization
63209

64210
`useAutocomplete` handles some aspects of internationalization automatically.

packages/@react-stately/autocomplete/docs/useAutocompleteState.mdx

+4
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ preRelease: beta
4242

4343
<ClassAPI links={docs.links} class={docs.links[docs.exports.useAutocompleteState.return.id]} />
4444

45+
## Example
46+
47+
See the docs for [useAutocomplete](/react-aria/useAutocomplete.html) in react-aria for an example of `useAutocompleteState`.
48+

0 commit comments

Comments
 (0)