Skip to content

Commit f892bae

Browse files
jRichardeaumartijnrusschen
authored andcommitted
Fix find dom node warning (Hacker0x01#1898)
* setClickOutsideRef function to fix react warning * onClickOutside usage unit tests
1 parent 83ad058 commit f892bae

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/calendar.jsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ export default class Calendar extends React.Component {
147147
constructor(props) {
148148
super(props);
149149

150+
this.containerRef = React.createRef();
151+
150152
this.state = {
151153
date: this.getDateInView(),
152154
selectingDate: null,
@@ -188,6 +190,10 @@ export default class Calendar extends React.Component {
188190
this.props.onClickOutside(event);
189191
};
190192

193+
setClickOutsideRef = () => {
194+
return this.containerRef.current;
195+
};
196+
191197
handleDropdownFocus = event => {
192198
if (isDropdownSelect(event.target)) {
193199
this.props.onDropdownFocus();
@@ -704,19 +710,21 @@ export default class Calendar extends React.Component {
704710
render() {
705711
const Container = this.props.container || CalendarContainer;
706712
return (
707-
<Container
708-
className={classnames("react-datepicker", this.props.className, {
709-
"react-datepicker--time-only": this.props.showTimeSelectOnly
710-
})}
711-
>
712-
{this.renderPreviousButton()}
713-
{this.renderNextButton()}
714-
{this.renderMonths()}
715-
{this.renderTodayButton()}
716-
{this.renderTimeSection()}
717-
{this.renderInputTimeSection()}
718-
{this.props.children}
719-
</Container>
713+
<div ref={this.containerRef}>
714+
<Container
715+
className={classnames("react-datepicker", this.props.className, {
716+
"react-datepicker--time-only": this.props.showTimeSelectOnly
717+
})}
718+
>
719+
{this.renderPreviousButton()}
720+
{this.renderNextButton()}
721+
{this.renderMonths()}
722+
{this.renderTodayButton()}
723+
{this.renderTimeSection()}
724+
{this.renderInputTimeSection()}
725+
{this.props.children}
726+
</Container>
727+
</div>
720728
);
721729
}
722730
}

test/calendar_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,4 +1155,30 @@ describe("Calendar", function() {
11551155
assert.equal(utils.getYear(calendar.state.date), 1994);
11561156
});
11571157
});
1158+
1159+
describe("using click outside", () => {
1160+
const clickOutsideSpy = sinon.spy();
1161+
const calendar = mount(
1162+
<Calendar
1163+
dateFormat={DATE_FORMAT}
1164+
onSelect={() => {}}
1165+
onClickOutside={clickOutsideSpy}
1166+
/>
1167+
);
1168+
1169+
const instance = calendar.instance();
1170+
1171+
it("calls onClickOutside prop when handles click outside", () => {
1172+
instance.handleClickOutside("__event__");
1173+
1174+
assert(clickOutsideSpy.calledWith("__event__"));
1175+
});
1176+
1177+
it("setClickOutsideRef function returns container ref", () => {
1178+
const ref = instance.setClickOutsideRef();
1179+
1180+
assert.isNotNull(ref);
1181+
assert.equal(ref, instance.containerRef.current);
1182+
});
1183+
});
11581184
});

0 commit comments

Comments
 (0)