Skip to content

Commit 1d1b002

Browse files
hudoviskmartijnrusschen
authored andcommitted
Makes open state controllable through props (Hacker0x01#1475)
1 parent 0aa518d commit 1d1b002

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/index.jsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ export default class DatePicker extends React.Component {
108108
onClickOutside: PropTypes.func,
109109
onChangeRaw: PropTypes.func,
110110
onFocus: PropTypes.func,
111+
onInputClick: PropTypes.func,
111112
onKeyDown: PropTypes.func,
112113
onMonthChange: PropTypes.func,
113114
onYearChange: PropTypes.func,
115+
open: PropTypes.bool,
114116
openToDate: PropTypes.object,
115117
peekNextMonth: PropTypes.bool,
116118
placeholderText: PropTypes.string,
@@ -171,6 +173,7 @@ export default class DatePicker extends React.Component {
171173
onFocus() {},
172174
onBlur() {},
173175
onKeyDown() {},
176+
onInputClick() {},
174177
onSelect() {},
175178
onClickOutside() {},
176179
onMonthChange() {},
@@ -272,6 +275,11 @@ export default class DatePicker extends React.Component {
272275
});
273276
};
274277

278+
isCalendarOpen = () =>
279+
this.props.open === undefined
280+
? this.state.open && !this.props.disabled && !this.props.readOnly
281+
: this.props.open;
282+
275283
handleFocus = event => {
276284
if (!this.state.preventFocus) {
277285
this.props.onFocus(event);
@@ -428,6 +436,8 @@ export default class DatePicker extends React.Component {
428436
if (!this.props.disabled && !this.props.readOnly) {
429437
this.setOpen(true);
430438
}
439+
440+
this.props.onInputClick();
431441
};
432442

433443
onInputKeyDown = event => {
@@ -514,10 +524,7 @@ export default class DatePicker extends React.Component {
514524
};
515525

516526
renderCalendar = () => {
517-
if (
518-
!this.props.inline &&
519-
(!this.state.open || this.props.disabled || this.props.readOnly)
520-
) {
527+
if (!this.props.inline && !this.isCalendarOpen()) {
521528
return null;
522529
}
523530
return (
@@ -674,9 +681,7 @@ export default class DatePicker extends React.Component {
674681
return (
675682
<PopperComponent
676683
className={this.props.popperClassName}
677-
hidePopper={
678-
!this.state.open || this.props.disabled || this.props.readOnly
679-
}
684+
hidePopper={!this.isCalendarOpen()}
680685
popperModifiers={this.props.popperModifiers}
681686
targetComponent={
682687
<div className="react-datepicker__input-container">

test/datepicker_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,4 +1053,26 @@ describe("DatePicker", () => {
10531053
datePicker.clear();
10541054
expect(datePicker.state.inputValue).to.be.null;
10551055
});
1056+
it("should not open when open is false and input is focused", () => {
1057+
var datePicker = TestUtils.renderIntoDocument(<DatePicker open={false} />);
1058+
var dateInput = datePicker.input;
1059+
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
1060+
expect(datePicker.calendar).to.not.exist;
1061+
});
1062+
it("should open when open is true", () => {
1063+
var datePicker = TestUtils.renderIntoDocument(<DatePicker open />);
1064+
expect(datePicker.calendar).to.exist;
1065+
});
1066+
it("should fire onInputClick when input is clicked", () => {
1067+
const onInputClickSpy = sinon.spy();
1068+
var datePicker = TestUtils.renderIntoDocument(
1069+
<DatePicker onInputClick={onInputClickSpy} />
1070+
);
1071+
var dateInput = datePicker.input;
1072+
TestUtils.Simulate.click(ReactDOM.findDOMNode(dateInput));
1073+
defer(() => {
1074+
assert(onInputClickSpy.calledOnce, "should fire onInputClick");
1075+
done();
1076+
});
1077+
});
10561078
});

0 commit comments

Comments
 (0)