@@ -68,6 +68,7 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
68
68
( category ) => category . items || [ ] ,
69
69
) ;
70
70
const [ inputItems , setInputItems ] = useState < ComboBoxOption [ ] > ( flatItems ) ;
71
+ const [ previewedIndex , setPreviewedIndex ] = useState < number > ( - 1 ) ;
71
72
// Re-create the categories so the two-column layout flows as expected.
72
73
const categories = items . reduce < ComboBoxCategory < ComboBoxOption > [ ] > (
73
74
( cats , cat , index ) => {
@@ -88,7 +89,6 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
88
89
getMenuProps,
89
90
getInputProps,
90
91
getItemProps,
91
- highlightedIndex,
92
92
setHighlightedIndex,
93
93
setInputValue,
94
94
openMenu,
@@ -153,12 +153,11 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
153
153
setInputItems ( filtered ) ;
154
154
// Always reset the first item to highlighted on filtering, to speed up selection.
155
155
setHighlightedIndex ( 0 ) ;
156
+ // Hide any preview when the user starts typing.
157
+ setPreviewedIndex ( - 1 ) ;
156
158
} ,
157
159
} ) ;
158
160
159
- const [ lastHighlightedIndex , setLastHighlightedIndex ] =
160
- useState ( highlightedIndex ) ;
161
-
162
161
useEffect ( ( ) => {
163
162
if ( inputValue ) {
164
163
openMenu ( ) ;
@@ -178,15 +177,8 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
178
177
}
179
178
} , [ inputValue ] ) ;
180
179
181
- if (
182
- inputItems [ highlightedIndex ] &&
183
- highlightedIndex !== lastHighlightedIndex
184
- ) {
185
- setLastHighlightedIndex ( highlightedIndex ) ;
186
- }
187
-
188
180
const previewedBlock =
189
- inputItems [ highlightedIndex ] || inputItems [ lastHighlightedIndex ] ;
181
+ typeof previewedIndex !== 'undefined' ? inputItems [ previewedIndex ] : null ;
190
182
191
183
return (
192
184
< div className = "w-combobox-container" >
@@ -283,8 +275,13 @@ export default function ComboBox<ComboBoxOption extends ComboBoxItem>({
283
275
< button
284
276
className = "w-combobox__option-preview"
285
277
aria-label = { comboBoxPreviewLabel }
278
+ aria-expanded = { previewedIndex === itemIndex }
286
279
type = "button"
287
- onClick = { ( ) => setHighlightedIndex ( itemIndex ) }
280
+ onClick = { ( ) =>
281
+ setPreviewedIndex (
282
+ previewedIndex === itemIndex ? - 1 : itemIndex ,
283
+ )
284
+ }
288
285
>
289
286
< Icon name = "view" />
290
287
</ button >
0 commit comments