Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes open state controllable through props #1475

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});