@@ -394,6 +394,35 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
394
394
[ options , resetOption , setActiveOption , selectedOptions ]
395
395
) ;
396
396
397
+ const handleListboxKeyDown = useCallback (
398
+ ( e : KeyboardEvent < HTMLUListElement > ) => {
399
+ // We don't prevent default on Tab, so that the Tab or Shift+Tab goes
400
+ // through after we've repositioned to the Button.
401
+ if ( e . key !== 'Tab' ) {
402
+ e . preventDefault ( ) ;
403
+ } else {
404
+ handleClose ( ) ;
405
+ }
406
+
407
+ if ( e . key === 'Escape' ) {
408
+ handleClose ( ) ;
409
+ } else if ( e . key === 'ArrowDown' ) {
410
+ moveActiveOptionDown ( ) ;
411
+ } else if ( e . key === 'ArrowUp' ) {
412
+ moveActiveOptionUp ( ) ;
413
+ } else if ( e . key === 'Home' ) {
414
+ setActiveOption ( options [ 0 ] ) ;
415
+ } else if ( e . key === 'End' ) {
416
+ setActiveOption ( options [ options . length - 1 ] ) ;
417
+ } else if ( e . key === 'PageDown' ) {
418
+ moveActiveOptionDown ( PAGE_STEP_SIZE ) ;
419
+ } else if ( e . key === 'PageUp' ) {
420
+ moveActiveOptionUp ( PAGE_STEP_SIZE ) ;
421
+ }
422
+ } ,
423
+ [ handleClose , moveActiveOptionDown , moveActiveOptionUp , options , setActiveOption ]
424
+ ) ;
425
+
397
426
// Transfer focus to the active option whenever we open the listbox.
398
427
useEffect ( ( ) => {
399
428
if ( isOpen && activeOption ) {
@@ -471,31 +500,7 @@ export const Select = forwardRef<HTMLButtonElement, SelectProps>(
471
500
id = { listboxId }
472
501
ref = { listboxRef }
473
502
aria-multiselectable = { multiSelect }
474
- onKeyDown = { ( e ) => {
475
- // We don't prevent default on Tab, so that the Tab or Shift+Tab goes
476
- // through after we've repositioned to the Button.
477
- if ( e . key !== 'Tab' ) {
478
- e . preventDefault ( ) ;
479
- } else {
480
- handleClose ( ) ;
481
- }
482
-
483
- if ( e . key === 'Escape' ) {
484
- handleClose ( ) ;
485
- } else if ( e . key === 'ArrowDown' ) {
486
- moveActiveOptionDown ( ) ;
487
- } else if ( e . key === 'ArrowUp' ) {
488
- moveActiveOptionUp ( ) ;
489
- } else if ( e . key === 'Home' ) {
490
- setActiveOption ( options [ 0 ] ) ;
491
- } else if ( e . key === 'End' ) {
492
- setActiveOption ( options [ options . length - 1 ] ) ;
493
- } else if ( e . key === 'PageDown' ) {
494
- moveActiveOptionDown ( PAGE_STEP_SIZE ) ;
495
- } else if ( e . key === 'PageUp' ) {
496
- moveActiveOptionUp ( PAGE_STEP_SIZE ) ;
497
- }
498
- } }
503
+ onKeyDown = { handleListboxKeyDown }
499
504
>
500
505
{ options . map ( ( option ) => (
501
506
< SelectOption
0 commit comments