1
- import React , { PureComponent } from 'react' ;
1
+ import React , { createRef , PureComponent } from 'react' ;
2
2
import { createPortal } from 'react-dom' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import makeEventProps from 'make-event-props' ;
@@ -28,6 +28,10 @@ export default class DatePicker extends PureComponent {
28
28
29
29
state = { } ;
30
30
31
+ wrapper = createRef ( ) ;
32
+
33
+ calendarWrapper = createRef ( ) ;
34
+
31
35
componentDidMount ( ) {
32
36
this . handleOutsideActionListeners ( ) ;
33
37
}
@@ -52,9 +56,16 @@ export default class DatePicker extends PureComponent {
52
56
}
53
57
54
58
onOutsideAction = ( event ) => {
59
+ const { wrapper, calendarWrapper } = this ;
60
+
55
61
// Try event.composedPath first to handle clicks inside a Shadow DOM.
56
62
const target = 'composedPath' in event ? event . composedPath ( ) [ 0 ] : event . target ;
57
- if ( this . wrapper && ! this . wrapper . contains ( target ) ) {
63
+
64
+ if (
65
+ wrapper . current &&
66
+ ! wrapper . current . contains ( target ) &&
67
+ ( ! calendarWrapper . current || ! calendarWrapper . current . contains ( target ) )
68
+ ) {
58
69
this . closeCalendar ( ) ;
59
70
}
60
71
} ;
@@ -254,7 +265,12 @@ export default class DatePicker extends PureComponent {
254
265
) ;
255
266
256
267
return portalContainer ? (
257
- createPortal ( < div className = { classNames } > { calendar } </ div > , portalContainer )
268
+ createPortal (
269
+ < div ref = { this . calendarWrapper } className = { classNames } >
270
+ { calendar }
271
+ </ div > ,
272
+ portalContainer ,
273
+ )
258
274
) : (
259
275
< Fit >
260
276
< div
@@ -288,13 +304,7 @@ export default class DatePicker extends PureComponent {
288
304
) }
289
305
{ ...eventPropsWithoutOnChange }
290
306
onFocus = { this . onFocus }
291
- ref = { ( ref ) => {
292
- if ( ! ref ) {
293
- return ;
294
- }
295
-
296
- this . wrapper = ref ;
297
- } }
307
+ ref = { this . wrapper }
298
308
>
299
309
{ this . renderInputs ( ) }
300
310
{ this . renderCalendar ( ) }
0 commit comments