@@ -358,8 +358,11 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
358358 // Not trigger `open` when `notFoundContent` is empty
359359 const emptyListContent = ! notFoundContent && emptyOptions ;
360360
361- const [ mergedOpen , triggerOpen ] = useOpen ( open , onPopupVisibleChange , ( nextOpen ) =>
362- disabled || emptyListContent ? false : nextOpen ,
361+ const [ mergedOpen , triggerOpen ] = useOpen (
362+ defaultOpen || false ,
363+ open ,
364+ onPopupVisibleChange ,
365+ ( nextOpen ) => ( disabled || emptyListContent ? false : nextOpen ) ,
363366 ) ;
364367
365368 // ============================= Search =============================
@@ -547,8 +550,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
547550 useSelectTriggerControl ( getSelectElements , mergedOpen , triggerOpen , ! ! mergedComponents . root ) ;
548551
549552 // ========================== Focus / Blur ==========================
550- /** Record real focus status */
551- // const focusRef = React.useRef<boolean>(false);
553+ const internalMouseDownRef = React . useRef ( false ) ;
552554
553555 const onInternalFocus : React . FocusEventHandler < HTMLElement > = ( event ) => {
554556 setFocused ( true ) ;
@@ -564,11 +566,12 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
564566 } ;
565567
566568 const onRootBlur = ( ) => {
567- macroTask ( ( ) => {
568- if ( ! isInside ( getSelectElements ( ) , document . activeElement as HTMLElement ) ) {
569- triggerOpen ( false ) ;
570- }
571- } ) ;
569+ // Delay close should check the activeElement
570+ if ( mergedOpen && ! internalMouseDownRef . current ) {
571+ triggerOpen ( false , {
572+ cancelFun : ( ) => isInside ( getSelectElements ( ) , document . activeElement as HTMLElement ) ,
573+ } ) ;
574+ }
572575 } ;
573576
574577 const onInternalBlur : React . FocusEventHandler < HTMLElement > = ( event ) => {
@@ -593,19 +596,22 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
593596 }
594597 } ;
595598
596- const onInternalMouseDown : React . MouseEventHandler < HTMLDivElement > = ( event , ...restArgs ) => {
599+ const onRootMouseDown : React . MouseEventHandler < HTMLDivElement > = ( event , ...restArgs ) => {
597600 const { target } = event ;
598601 const popupElement : HTMLDivElement = triggerRef . current ?. getPopupElement ( ) ;
599602
600603 // We should give focus back to selector if clicked item is not focusable
601604 if ( popupElement ?. contains ( target as HTMLElement ) && triggerOpen ) {
602605 // Tell `open` not to close since it's safe in the popup
603- triggerOpen ( true , {
604- ignoreNext : true ,
605- } ) ;
606+ triggerOpen ( true ) ;
606607 }
607608
608609 onMouseDown ?.( event , ...restArgs ) ;
610+
611+ internalMouseDownRef . current = true ;
612+ macroTask ( ( ) => {
613+ internalMouseDownRef . current = false ;
614+ } ) ;
609615 } ;
610616
611617 // ============================ Dropdown ============================
@@ -747,7 +753,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
747753 // Token handling
748754 tokenWithEnter = { tokenWithEnter }
749755 // Open
750- onMouseDown = { onInternalMouseDown }
756+ onMouseDown = { onRootMouseDown }
751757 // Components
752758 components = { mergedComponents }
753759 />
@@ -774,7 +780,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
774780 empty = { emptyOptions }
775781 onPopupVisibleChange = { onTriggerVisibleChange }
776782 onPopupMouseEnter = { onPopupMouseEnter }
777- onPopupMouseDown = { onInternalMouseDown }
783+ onPopupMouseDown = { onRootMouseDown }
778784 onPopupBlur = { onRootBlur }
779785 >
780786 { renderNode }
0 commit comments