@@ -25,7 +25,11 @@ import { isEqual } from './utils/dateUtil';
2525import { toArray } from './utils/miscUtil' ;
2626import PanelContext , { ContextOperationRefProps } from './PanelContext' ;
2727import { PickerMode } from './interface' ;
28- import { getDefaultFormat , getInputSize } from './utils/uiUtil' ;
28+ import {
29+ getDefaultFormat ,
30+ getInputSize ,
31+ addGlobalClickEvent ,
32+ } from './utils/uiUtil' ;
2933
3034export interface PickerSharedProps < DateType > {
3135 dropdownClassName ?: string ;
@@ -128,6 +132,10 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
128132 getDefaultFormat ( format , picker , showTime , use12Hours ) ,
129133 ) ;
130134
135+ // Panel ref
136+ const panelDivRef = React . useRef < HTMLDivElement > ( null ) ;
137+ const inputDivRef = React . useRef < HTMLDivElement > ( null ) ;
138+
131139 // Real value
132140 const [ innerValue , setInnerValue ] = React . useState < DateType | null > ( ( ) => {
133141 if ( value !== undefined ) {
@@ -351,6 +359,22 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
351359 }
352360 } , [ mergedValue ] ) ;
353361
362+ // Global click handler
363+ React . useEffect ( ( ) =>
364+ addGlobalClickEvent ( ( { target } : MouseEvent ) => {
365+ if (
366+ mergedOpen &&
367+ panelDivRef . current &&
368+ ! panelDivRef . current . contains ( target as Node ) &&
369+ inputDivRef . current &&
370+ ! inputDivRef . current . contains ( target as Node ) &&
371+ onOpenChange
372+ ) {
373+ onOpenChange ( false ) ;
374+ }
375+ } ) ,
376+ ) ;
377+
354378 // ============================= Panel =============================
355379 const panelProps = {
356380 // Remove `picker` & `format` here since TimePicker is little different with other panel
@@ -401,6 +425,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
401425 value = { {
402426 operationRef,
403427 hideHeader : picker === 'time' ,
428+ panelRef : panelDivRef ,
404429 } }
405430 >
406431 < div
@@ -420,7 +445,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
420445 getPopupContainer = { getPopupContainer }
421446 transitionName = { transitionName }
422447 >
423- < div className = { `${ prefixCls } -input` } >
448+ < div className = { `${ prefixCls } -input` } ref = { inputDivRef } >
424449 < input
425450 disabled = { disabled }
426451 readOnly = { inputReadOnly || ! typing }
0 commit comments