@@ -417,39 +417,39 @@ const Select = forwardRef<SelectRef, SelectProps>((
417417 }
418418 } , [ isMulti , closeMenuOnSelect , removeSelectedOption , blurInputOnSelect ] ) ;
419419
420+ const hasSelectedOptions = isArrayWithLength ( selectedOption ) ;
421+
420422 /**
421423 * useImperativeHandle.
422424 * Exposed API methods/properties available on a ref instance of this Select.tsx component.
425+ * Dependency list passed as the third param to re-create the handle when one of them updates.
423426 */
424- useImperativeHandle ( ref , ( ) => ( {
425- empty : ! isArrayWithLength ( selectedOption ) ,
426- menuOpen,
427- blur : blurInput ,
428- focus : focusInput ,
429- clearValue : ( ) => {
430- if ( selectOption . length )
427+ useImperativeHandle (
428+ ref ,
429+ ( ) => ( {
430+ empty : ! hasSelectedOptions ,
431+ menuOpen : menuOpenRef . current ,
432+ blur : blurInput ,
433+ focus : focusInput ,
434+ clearValue : ( ) => {
431435 setSelectedOption ( EMPTY_ARRAY ) ;
432- if ( focusedOption . data )
433436 setFocusedOption ( FOCUSED_OPTION_DEFAULT ) ;
434- } ,
435- setValue : ( option ?: OptionData ) => {
436- const normalizedOptions = normalizeValue (
437- option ,
438- getOptionValueFn ,
439- getOptionLabelFn
440- ) ;
441-
442- setSelectedOption ( normalizedOptions ) ;
443- } ,
444- toggleMenu : ( state ?: boolean ) => {
445- if ( state === true || ( state === undefined && ! menuOpen ) ) {
446- ! isFocused && focusInput ( ) ;
447- openMenuAndFocusOption ( OptionIndexEnum . FIRST ) ;
448- } else {
449- blurInput ( ) ;
437+ } ,
438+ setValue : ( option ?: OptionData ) => {
439+ const normalizedOptions = normalizeValue ( option , getOptionValueFn , getOptionLabelFn ) ;
440+ setSelectedOption ( normalizedOptions ) ;
441+ } ,
442+ toggleMenu : ( state ?: boolean ) => {
443+ if ( state === true || ( state === undefined && ! menuOpenRef . current ) ) {
444+ focusInput ( ) ;
445+ openMenuAndFocusOption ( OptionIndexEnum . FIRST ) ;
446+ } else {
447+ blurInput ( ) ;
448+ }
450449 }
451- }
452- } ) ) ;
450+ } ) ,
451+ [ hasSelectedOptions , getOptionValueFn , getOptionLabelFn , openMenuAndFocusOption ]
452+ ) ;
453453
454454 /**
455455 * useMountEffect:
@@ -543,7 +543,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
543543
544544 // Only Multiselect mode supports value focusing
545545 const focusValueOnArrowKey = ( direction : ValueIndexEnum ) : void => {
546- if ( ! isArrayWithLength ( selectedOption ) ) return ;
546+ if ( ! hasSelectedOptions ) return ;
547547
548548 let nextFocusedIdx = - 1 ;
549549 const lastValueIdx = selectedOption . length - 1 ;
@@ -662,7 +662,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
662662 setFocusedMultiValue ( nexFocusedMultiValue ) ;
663663 } else {
664664 if ( ! backspaceClearsValue ) return ;
665- if ( ! isArrayWithLength ( selectedOption ) ) break ;
665+ if ( ! hasSelectedOptions ) break ;
666666
667667 if ( isMulti && ! renderMultiOptions ) {
668668 const { value } = selectedOption [ selectedOption . length - 1 ] ;
@@ -735,8 +735,8 @@ const Select = forwardRef<SelectRef, SelectProps>((
735735 } , [ ] ) ;
736736
737737 const renderMenu = ! lazyLoadMenu || ( lazyLoadMenu && menuOpen ) ;
738+ const showClear = ! ! ( isClearable && ! isDisabled && hasSelectedOptions ) ;
738739 const inputReadOnly = isDisabled || ! isSearchable || ! ! focusedMultiValue ;
739- const showClear = ! ! ( isClearable && ! isDisabled && isArrayWithLength ( selectedOption ) ) ;
740740 const handleOnCaretMouseDownOrNoop = ( ! isDisabled && ! openMenuOnClick ) ? handleOnCaretMouseDown : undefined ;
741741
742742 return (
@@ -779,7 +779,7 @@ const Select = forwardRef<SelectRef, SelectProps>((
779779 onFocus = { handleOnInputFocus }
780780 onChange = { handleOnInputChange }
781781 ariaLabelledBy = { ariaLabelledBy }
782- selectedOption = { selectedOption }
782+ hasSelectedOptions = { hasSelectedOptions }
783783 />
784784 </ ValueWrapper >
785785 < IndicatorIcons
0 commit comments