Skip to content

Commit

Permalink
Merge pull request #604 from ah-adarlow/master
Browse files Browse the repository at this point in the history
Fixed DayPickerInput not calling dayPickerProps.onMonthChange
  • Loading branch information
gpbl authored Jan 9, 2018
2 parents 8c69e20 + 87ebbcd commit 305bd5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/DayPickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default class DayPickerInput extends React.Component {
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
this.handleInputKeyUp = this.handleInputKeyUp.bind(this);
this.handleDayClick = this.handleDayClick.bind(this);
this.handleMonthChange = this.handleMonthChange.bind(this);
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -356,6 +357,17 @@ export default class DayPickerInput extends React.Component {
}
}

handleMonthChange(month) {
this.setState({ month }, () => {
if (
this.props.dayPickerProps &&
this.props.dayPickerProps.onMonthChange
) {
this.props.dayPickerProps.onMonthChange(month);
}
});
}

handleDayClick(day, modifiers, e) {
const {
clickUnselectsDay,
Expand Down Expand Up @@ -443,7 +455,7 @@ export default class DayPickerInput extends React.Component {
month={this.state.month}
selectedDays={selectedDay}
onDayClick={this.handleDayClick}
onMonthChange={month => this.setState({ month })}
onMonthChange={this.handleMonthChange}
/>
</Overlay>
);
Expand Down
35 changes: 35 additions & 0 deletions test/daypickerinput/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,40 @@ describe('DayPickerInput', () => {
expect(selectedDays.at(1)).toHaveText('9');
});
});

describe('onMonthChange', () => {
it('should update state when month changes', () => {
const wrapper = mount(
<DayPickerInput
dayPickerProps={{
initialMonth: new Date(2015, 7),
}}
/>
);
const instance = wrapper.instance();
instance.showDayPicker();
wrapper.update();
instance.getDayPicker().showNextMonth();
expect(instance.state.month.getMonth()).toEqual(8);
});

it('should call onMonthChange when month changes', () => {
const handleMonthChange = jest.fn();
const wrapper = mount(
<DayPickerInput
dayPickerProps={{
onMonthChange: handleMonthChange,
initialMonth: new Date(2015, 7),
}}
/>
);
const instance = wrapper.instance();
instance.showDayPicker();
wrapper.update();
instance.getDayPicker().showNextMonth();
expect(handleMonthChange).toHaveBeenCalled();
expect(handleMonthChange.mock.calls[0][0].getMonth()).toEqual(8);
});
});
});
});

0 comments on commit 305bd5a

Please sign in to comment.