@@ -210,12 +210,14 @@ const DropdownLight = ({
210
210
const [ cursor , setCursor ] = useState ( - 1 ) ;
211
211
const buttonRef = useRef ( ) ;
212
212
const listOptions = useRef ( ) ;
213
+ const ButtonIsClosedAndFocused = ( ) =>
214
+ ! isOpen && document . activeElement === buttonRef . current ;
213
215
214
- const downPress = useKeyPress ( 'ArrowDown' ) ;
215
- const upPress = useKeyPress ( 'ArrowUp' ) ;
216
+ const downPress = useKeyPress ( 'ArrowDown' , ButtonIsClosedAndFocused ) ;
217
+ const upPress = useKeyPress ( 'ArrowUp' , ButtonIsClosedAndFocused ) ;
216
218
const enterPress = useKeyPress ( 'Enter' ) ;
219
+ const escapePress = useKeyPress ( 'Escape' ) ;
217
220
const focusedItemIndex = useKeyboardSearchItems ( items , cursor , isOpen ) ;
218
- const EscapeKeyPressValue = 'Escape' ;
219
221
220
222
const handleToggleDropdown = ( ) => {
221
223
setIsOpen ( ! isOpen ) ;
@@ -249,44 +251,64 @@ const DropdownLight = ({
249
251
}
250
252
} ;
251
253
252
- const handleEscPress = ( { key } ) => {
253
- if ( key === EscapeKeyPressValue ) {
254
+ useEffect ( ( ) => {
255
+ if ( isOpen && escapePress ) {
254
256
setIsOpen ( false ) ;
255
257
setCursor ( - 1 ) ;
256
258
buttonRef . current . focus ( ) ;
257
259
}
258
- } ;
260
+ } , [ escapePress , isOpen ] ) ;
259
261
260
262
useEffect ( ( ) => {
263
+ const GetNextCursor = ( currentCursor ) =>
264
+ currentCursor < items . length - 1 ? currentCursor + 1 : currentCursor ;
265
+
261
266
if ( isOpen && downPress ) {
262
- const selectedCursor = cursor < items . length - 1 ? cursor + 1 : cursor ;
267
+ const selectedCursor = GetNextCursor ( cursor ) ;
263
268
setCursor ( selectedCursor ) ;
264
269
}
270
+
271
+ if ( downPress && ButtonIsClosedAndFocused ( ) ) {
272
+ const selectedItemIndex = items . findIndex (
273
+ ( item ) => ( item ?. label || item ) === selectedOptionItem ,
274
+ ) ;
275
+ const selectedCursor = GetNextCursor ( selectedItemIndex ) ;
276
+
277
+ if ( selectedItemIndex !== selectedCursor ) {
278
+ selectItem ( items [ selectedCursor ] ) ;
279
+ }
280
+ }
265
281
} , [ downPress , items , isOpen ] ) ;
266
282
267
283
useEffect ( ( ) => {
268
284
if ( isOpen && upPress && cursor > 0 ) {
269
285
const selectedCursor = cursor - 1 ;
270
286
setCursor ( selectedCursor ) ;
271
287
}
288
+
289
+ if ( upPress && ButtonIsClosedAndFocused ( ) ) {
290
+ const selectedItemIndex = items . findIndex (
291
+ ( item ) => ( item ?. label || item ) === selectedOptionItem ,
292
+ ) ;
293
+ const previousCursor =
294
+ selectedItemIndex > 0 ? selectedItemIndex - 1 : selectedItemIndex ;
295
+ const selectedCursor = previousCursor ;
296
+
297
+ if ( selectedItemIndex !== selectedCursor ) {
298
+ selectItem ( items [ selectedCursor ] ) ;
299
+ }
300
+ }
272
301
} , [ upPress , items , isOpen ] ) ;
273
302
274
303
useEffect ( ( ) => {
275
304
window . addEventListener ( 'click' , handleClickOutside ) ;
276
- window . addEventListener ( 'keydown' , handleEscPress ) ;
277
305
return ( ) => {
278
306
window . removeEventListener ( 'click' , handleClickOutside ) ;
279
- window . removeEventListener ( 'keydown' , handleEscPress ) ;
280
307
} ;
281
308
} , [ ] ) ;
282
309
283
310
useEffect ( ( ) => {
284
- if (
285
- enterPress &&
286
- ! isOpen &&
287
- document . activeElement === buttonRef . current &&
288
- selectedOptionItem
289
- ) {
311
+ if ( enterPress && ButtonIsClosedAndFocused ( ) && selectedOptionItem ) {
290
312
const selectedItemIndex = items . findIndex (
291
313
( item ) => ( item ?. label || item ) === selectedOptionItem ,
292
314
) ;
0 commit comments