@@ -12,8 +12,10 @@ export default Layout;
12
12
13
13
import docs from ' docs:@react-aria/autocomplete' ;
14
14
import {FunctionAPI , HeaderInfo , InterfaceType , TypeContext , TypeLink , PageDescription } from ' @react-spectrum/docs' ;
15
+ import i18nDocs from ' docs:@react-aria/i18n' ;
15
16
import packageData from ' @react-aria/autocomplete/package.json' ;
16
17
import statelyDocs from ' docs:@react-stately/autocomplete' ;
18
+ import ChevronRight from ' @spectrum-icons/workflow/ChevronRight' ;
17
19
import {InlineAlert , Content , Heading } from ' @adobe/react-spectrum' ;
18
20
19
21
---
@@ -59,6 +61,150 @@ to filter the collection. `useAutocomplete` handles exposing the correct ARIA at
59
61
State is managed by the <TypeLink links = { statelyDocs .links } type = { statelyDocs .exports .useAutocompleteState } /> hook from ` @react-stately/autocomplete ` .
60
62
The state object should be passed as an option to ` useAutocomplete ` .
61
63
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
+
62
208
## Internationalization
63
209
64
210
` useAutocomplete ` handles some aspects of internationalization automatically.
0 commit comments