@@ -342,11 +342,11 @@ const Select = forwardRef<SelectRef, SelectProps>((
342342 ? menuOptions . findIndex ( ( x ) => x . isSelected )
343343 : - 1 ;
344344
345- const index = ( selectedIndex > - 1 )
345+ const index = selectedIndex > - 1
346346 ? selectedIndex
347- : ( position === OptionIndexEnum . FIRST )
348- ? 0
349- : ( menuOptions . length - 1 ) ;
347+ : position === OptionIndexEnum . FIRST
348+ ? 0
349+ : menuOptions . length - 1 ;
350350
351351 ! menuOpenRef . current && setMenuOpen ( true ) ;
352352 setFocusedOption ( { index, ...menuOptions [ index ] } ) ;
@@ -366,7 +366,9 @@ const Select = forwardRef<SelectRef, SelectProps>((
366366 }
367367
368368 // Use 'blurInputOnSelect' if defined, otherwise evaluate to true if current device is touch-device
369- const blurControl = isBoolean ( blurInputOnSelect ) ? blurInputOnSelect : IS_TOUCH_DEVICE ;
369+ const blurControl = isBoolean ( blurInputOnSelect )
370+ ? blurInputOnSelect
371+ : IS_TOUCH_DEVICE ;
370372
371373 if ( blurControl ) {
372374 blurInput ( ) ;
@@ -533,6 +535,13 @@ const Select = forwardRef<SelectRef, SelectProps>((
533535 scrollToItemIndex ( index ) ;
534536 } ;
535537
538+ const handleVerticalKeySubRoutine = ( key : string ) : void => {
539+ const downKey = key === 'ArrowDown' ;
540+ const downUpIndex = downKey ? OptionIndexEnum . DOWN : OptionIndexEnum . UP ;
541+ const posIndex = downKey ? OptionIndexEnum . FIRST : OptionIndexEnum . LAST ;
542+ menuOpen ? focusOptionOnArrowKey ( downUpIndex ) : openMenuAndFocusOption ( posIndex ) ;
543+ } ;
544+
536545 const handleOnKeyDown = ( e : KeyboardEvent < HTMLDivElement > ) : void => {
537546 if ( isDisabled ) return ;
538547
@@ -544,25 +553,22 @@ const Select = forwardRef<SelectRef, SelectProps>((
544553 switch ( e . key ) {
545554 case 'ArrowDown' :
546555 case 'ArrowUp' : {
547- const downKey = e . key === 'ArrowDown' ;
548- menuOpen
549- ? focusOptionOnArrowKey ( downKey ? OptionIndexEnum . DOWN : OptionIndexEnum . UP )
550- : openMenuAndFocusOption ( downKey ? OptionIndexEnum . FIRST : OptionIndexEnum . LAST ) ;
551-
556+ handleVerticalKeySubRoutine ( e . key ) ;
552557 break ;
553558 }
554559 case 'ArrowLeft' :
555560 case 'ArrowRight' : {
556561 if ( ! isMulti || inputValue || renderMultiOptions ) return ;
557- focusValueOnArrowKey ( e . key === 'ArrowLeft' ? ValueIndexEnum . PREVIOUS : ValueIndexEnum . NEXT ) ;
558562
563+ const leftRightIndex = e . key === 'ArrowLeft' ? ValueIndexEnum . PREVIOUS : ValueIndexEnum . NEXT ;
564+ focusValueOnArrowKey ( leftRightIndex ) ;
559565 break ;
560566 }
561567 // Handle spacebar keydown events
562568 case ' ' : {
563- if ( inputValue ) {
564- return ;
565- } else if ( ! menuOpen ) {
569+ if ( inputValue ) return ;
570+
571+ if ( ! menuOpen ) {
566572 openMenuAndFocusOption ( OptionIndexEnum . FIRST ) ;
567573 } else if ( ! focusedOption . data ) {
568574 return ;
@@ -605,14 +611,13 @@ const Select = forwardRef<SelectRef, SelectProps>((
605611 setFocusedMultiValue ( nexFocusedMultiValue ) ;
606612 } else {
607613 if ( ! backspaceClearsValue ) return ;
614+ if ( ! isArrayWithLength ( selectedOption ) ) break ;
608615
609- if ( isArrayWithLength ( selectedOption ) ) {
610- if ( isMulti && ! renderMultiOptions ) {
611- const { value } = selectedOption [ selectedOption . length - 1 ] ;
612- removeSelectedOption ( value ) ;
613- } else if ( isClearable ) {
614- setSelectedOption ( EMPTY_ARRAY ) ;
615- }
616+ if ( isMulti && ! renderMultiOptions ) {
617+ const { value } = selectedOption [ selectedOption . length - 1 ] ;
618+ removeSelectedOption ( value ) ;
619+ } else if ( isClearable ) {
620+ setSelectedOption ( EMPTY_ARRAY ) ;
616621 }
617622 }
618623
0 commit comments