From 87ebbcd311940a7f7a0d44cfb69b1078e1be3778 Mon Sep 17 00:00:00 2001 From: Adam Darlow Date: Mon, 8 Jan 2018 20:17:29 -0500 Subject: [PATCH] Fixed DayPickerInput not calling dayPickerProps.onMonthChange --- src/DayPickerInput.js | 14 +++++++++++++- test/daypickerinput/events.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/DayPickerInput.js b/src/DayPickerInput.js index 03345b2dd9..a5ca65c5f1 100644 --- a/src/DayPickerInput.js +++ b/src/DayPickerInput.js @@ -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) { @@ -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, @@ -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} /> ); diff --git a/test/daypickerinput/events.js b/test/daypickerinput/events.js index 1fffec2106..c948ddf67a 100644 --- a/test/daypickerinput/events.js +++ b/test/daypickerinput/events.js @@ -370,5 +370,40 @@ describe('DayPickerInput', () => { expect(selectedDays.at(1)).toHaveText('9'); }); }); + + describe('onMonthChange', () => { + it('should update state when month changes', () => { + const wrapper = mount( + + ); + 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( + + ); + const instance = wrapper.instance(); + instance.showDayPicker(); + wrapper.update(); + instance.getDayPicker().showNextMonth(); + expect(handleMonthChange).toHaveBeenCalled(); + expect(handleMonthChange.mock.calls[0][0].getMonth()).toEqual(8); + }); + }); }); });