Skip to content

Commit

Permalink
Makes open state controllable through props
Browse files Browse the repository at this point in the history
  • Loading branch information
hudovisk committed Sep 8, 2018
1 parent 8105ede commit b43f816
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ export default class DatePicker extends React.Component {
onClickOutside: PropTypes.func,
onChangeRaw: PropTypes.func,
onFocus: PropTypes.func,
onInputClick: PropTypes.func,
onKeyDown: PropTypes.func,
onMonthChange: PropTypes.func,
onYearChange: PropTypes.func,
open: PropTypes.bool,
openToDate: PropTypes.object,
peekNextMonth: PropTypes.bool,
placeholderText: PropTypes.string,
Expand Down Expand Up @@ -171,6 +173,7 @@ export default class DatePicker extends React.Component {
onFocus() {},
onBlur() {},
onKeyDown() {},
onInputClick() {},
onSelect() {},
onClickOutside() {},
onMonthChange() {},
Expand Down Expand Up @@ -272,6 +275,11 @@ export default class DatePicker extends React.Component {
});
};

isCalendarOpen = () =>
this.props.open === undefined
? this.state.open && !this.props.disabled && !this.props.readOnly
: this.props.open;

handleFocus = event => {
if (!this.state.preventFocus) {
this.props.onFocus(event);
Expand Down Expand Up @@ -428,6 +436,8 @@ export default class DatePicker extends React.Component {
if (!this.props.disabled && !this.props.readOnly) {
this.setOpen(true);
}

this.props.onInputClick();
};

onInputKeyDown = event => {
Expand Down Expand Up @@ -514,10 +524,7 @@ export default class DatePicker extends React.Component {
};

renderCalendar = () => {
if (
!this.props.inline &&
(!this.state.open || this.props.disabled || this.props.readOnly)
) {
if (!this.props.inline && !this.isCalendarOpen()) {
return null;
}
return (
Expand Down Expand Up @@ -673,9 +680,7 @@ export default class DatePicker extends React.Component {
return (
<PopperComponent
className={this.props.popperClassName}
hidePopper={
!this.state.open || this.props.disabled || this.props.readOnly
}
hidePopper={!this.isCalendarOpen()}
popperModifiers={this.props.popperModifiers}
targetComponent={
<div className="react-datepicker__input-container">
Expand Down
22 changes: 22 additions & 0 deletions test/datepicker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1037,4 +1037,26 @@ describe("DatePicker", () => {
datePicker.clear();
expect(datePicker.state.inputValue).to.be.null;
});
it("should not open when open is false and input is focused", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker open={false} />);
var dateInput = datePicker.input;
TestUtils.Simulate.focus(ReactDOM.findDOMNode(dateInput));
expect(datePicker.calendar).to.not.exist;
});
it("should open when open is true", () => {
var datePicker = TestUtils.renderIntoDocument(<DatePicker open />);
expect(datePicker.calendar).to.exist;
});
it("should fire onInputClick when input is clicked", () => {
const onInputClickSpy = sinon.spy();
var datePicker = TestUtils.renderIntoDocument(
<DatePicker onInputClick={onInputClickSpy} />
);
var dateInput = datePicker.input;
TestUtils.Simulate.click(ReactDOM.findDOMNode(dateInput));
defer(() => {
assert(onInputClickSpy.calledOnce, "should fire onInputClick");
done();
});
});
});

0 comments on commit b43f816

Please sign in to comment.