|
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'; |
@@ -35,6 +35,10 @@ export default class DateTimePicker extends PureComponent { |
35 | 35 |
|
36 | 36 | state = {}; |
37 | 37 |
|
| 38 | + wrapper = createRef(); |
| 39 | + |
| 40 | + widgetWrapper = createRef(); |
| 41 | + |
38 | 42 | componentDidMount() { |
39 | 43 | this.handleOutsideActionListeners(); |
40 | 44 | } |
@@ -70,9 +74,16 @@ export default class DateTimePicker extends PureComponent { |
70 | 74 | } |
71 | 75 |
|
72 | 76 | onOutsideAction = (event) => { |
| 77 | + const { wrapper, widgetWrapper } = this; |
| 78 | + |
73 | 79 | // Try event.composedPath first to handle clicks inside a Shadow DOM. |
74 | 80 | const target = 'composedPath' in event ? event.composedPath()[0] : event.target; |
75 | | - if (this.wrapper && !this.wrapper.contains(target)) { |
| 81 | + |
| 82 | + if ( |
| 83 | + wrapper.current && |
| 84 | + !wrapper.current.contains(target) && |
| 85 | + (!widgetWrapper.current || !widgetWrapper.current.contains(target)) |
| 86 | + ) { |
76 | 87 | this.closeWidgets(); |
77 | 88 | } |
78 | 89 | }; |
@@ -338,7 +349,12 @@ export default class DateTimePicker extends PureComponent { |
338 | 349 | ); |
339 | 350 |
|
340 | 351 | return portalContainer ? ( |
341 | | - createPortal(<div className={classNames}>{calendar}</div>, portalContainer) |
| 352 | + createPortal( |
| 353 | + <div ref={this.widgetWrapper} className={classNames}> |
| 354 | + {calendar} |
| 355 | + </div>, |
| 356 | + portalContainer, |
| 357 | + ) |
342 | 358 | ) : ( |
343 | 359 | <Fit> |
344 | 360 | <div |
@@ -394,7 +410,12 @@ export default class DateTimePicker extends PureComponent { |
394 | 410 | ); |
395 | 411 |
|
396 | 412 | return portalContainer ? ( |
397 | | - createPortal(<div className={classNames}>{clock}</div>, portalContainer) |
| 413 | + createPortal( |
| 414 | + <div ref={this.widgetWrapper} className={classNames}> |
| 415 | + {clock} |
| 416 | + </div>, |
| 417 | + portalContainer, |
| 418 | + ) |
398 | 419 | ) : ( |
399 | 420 | <Fit> |
400 | 421 | <div |
@@ -428,13 +449,7 @@ export default class DateTimePicker extends PureComponent { |
428 | 449 | )} |
429 | 450 | {...eventPropsWithoutOnChange} |
430 | 451 | onFocus={this.onFocus} |
431 | | - ref={(ref) => { |
432 | | - if (!ref) { |
433 | | - return; |
434 | | - } |
435 | | - |
436 | | - this.wrapper = ref; |
437 | | - }} |
| 452 | + ref={this.wrapper} |
438 | 453 | > |
439 | 454 | {this.renderInputs()} |
440 | 455 | {this.renderCalendar()} |
|
0 commit comments