Skip to content

Commit 1624ecf

Browse files
committed
Don't close calendar when clicked within its wrapper
1 parent 2254895 commit 1624ecf

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/DatePicker.jsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { PureComponent } from 'react';
1+
import React, { createRef, PureComponent } from 'react';
22
import { createPortal } from 'react-dom';
33
import PropTypes from 'prop-types';
44
import makeEventProps from 'make-event-props';
@@ -28,6 +28,10 @@ export default class DatePicker extends PureComponent {
2828

2929
state = {};
3030

31+
wrapper = createRef();
32+
33+
calendarWrapper = createRef();
34+
3135
componentDidMount() {
3236
this.handleOutsideActionListeners();
3337
}
@@ -52,9 +56,16 @@ export default class DatePicker extends PureComponent {
5256
}
5357

5458
onOutsideAction = (event) => {
59+
const { wrapper, calendarWrapper } = this;
60+
5561
// Try event.composedPath first to handle clicks inside a Shadow DOM.
5662
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+
) {
5869
this.closeCalendar();
5970
}
6071
};
@@ -254,7 +265,12 @@ export default class DatePicker extends PureComponent {
254265
);
255266

256267
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+
)
258274
) : (
259275
<Fit>
260276
<div
@@ -288,13 +304,7 @@ export default class DatePicker extends PureComponent {
288304
)}
289305
{...eventPropsWithoutOnChange}
290306
onFocus={this.onFocus}
291-
ref={(ref) => {
292-
if (!ref) {
293-
return;
294-
}
295-
296-
this.wrapper = ref;
297-
}}
307+
ref={this.wrapper}
298308
>
299309
{this.renderInputs()}
300310
{this.renderCalendar()}

0 commit comments

Comments
 (0)