11import type * as React from 'react' ;
22import { useState , useEffect , useRef } from 'react' ;
33import KeyCode from 'rc-util/lib/KeyCode' ;
4- import { addGlobalMouseDownEvent } from '../utils/uiUtil' ;
4+ import { addGlobalMouseDownEvent , getTargetFromEvent } from '../utils/uiUtil' ;
55
66export default function usePickerInput ( {
77 open,
@@ -21,10 +21,7 @@ export default function usePickerInput({
2121 isClickOutside : ( clickElement : EventTarget | null ) => boolean ;
2222 triggerOpen : ( open : boolean ) => void ;
2323 forwardKeyDown : ( e : React . KeyboardEvent < HTMLInputElement > ) => boolean ;
24- onKeyDown : (
25- e : React . KeyboardEvent < HTMLInputElement > ,
26- preventDefault : ( ) => void ,
27- ) => void ;
24+ onKeyDown : ( e : React . KeyboardEvent < HTMLInputElement > , preventDefault : ( ) => void ) => void ;
2825 blurToCancel ?: boolean ;
2926 onSubmit : ( ) => void | boolean ;
3027 onCancel : ( ) => void ;
@@ -49,7 +46,7 @@ export default function usePickerInput({
4946 setTyping ( true ) ;
5047 triggerOpen ( true ) ;
5148 } ,
52- onKeyDown : e => {
49+ onKeyDown : ( e ) => {
5350 const preventDefault = ( ) : void => {
5451 preventDefaultRef . current = true ;
5552 } ;
@@ -98,7 +95,7 @@ export default function usePickerInput({
9895 }
9996 } ,
10097
101- onFocus : e => {
98+ onFocus : ( e ) => {
10299 setTyping ( true ) ;
103100 setFocused ( true ) ;
104101
@@ -107,7 +104,7 @@ export default function usePickerInput({
107104 }
108105 } ,
109106
110- onBlur : e => {
107+ onBlur : ( e ) => {
111108 if ( preventBlurRef . current || ! isClickOutside ( document . activeElement ) ) {
112109 preventBlurRef . current = false ;
113110 return ;
@@ -145,16 +142,20 @@ export default function usePickerInput({
145142
146143 // Global click handler
147144 useEffect ( ( ) =>
148- addGlobalMouseDownEvent ( ( { target } : MouseEvent ) => {
145+ addGlobalMouseDownEvent ( ( e : MouseEvent ) => {
146+ const target = getTargetFromEvent ( e ) ;
147+
149148 if ( open ) {
150- if ( ! isClickOutside ( target ) ) {
149+ const clickedOutside = isClickOutside ( target ) ;
150+
151+ if ( ! clickedOutside ) {
151152 preventBlurRef . current = true ;
152153
153154 // Always set back in case `onBlur` prevented by user
154155 requestAnimationFrame ( ( ) => {
155156 preventBlurRef . current = false ;
156157 } ) ;
157- } else if ( ! focused ) {
158+ } else if ( ! focused || clickedOutside ) {
158159 triggerOpen ( false ) ;
159160 }
160161 }
0 commit comments