File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed
Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,7 @@ export default class DayPickerInput extends React.Component {
122122 this . handleInputKeyDown = this . handleInputKeyDown . bind ( this ) ;
123123 this . handleInputKeyUp = this . handleInputKeyUp . bind ( this ) ;
124124 this . handleDayClick = this . handleDayClick . bind ( this ) ;
125+ this . handleMonthChange = this . handleMonthChange . bind ( this ) ;
125126 }
126127
127128 componentWillReceiveProps ( nextProps ) {
@@ -356,6 +357,17 @@ export default class DayPickerInput extends React.Component {
356357 }
357358 }
358359
360+ handleMonthChange ( month ) {
361+ this . setState ( { month } , ( ) => {
362+ if (
363+ this . props . dayPickerProps &&
364+ this . props . dayPickerProps . onMonthChange
365+ ) {
366+ this . props . dayPickerProps . onMonthChange ( month ) ;
367+ }
368+ } ) ;
369+ }
370+
359371 handleDayClick ( day , modifiers , e ) {
360372 const {
361373 clickUnselectsDay,
@@ -443,7 +455,7 @@ export default class DayPickerInput extends React.Component {
443455 month = { this . state . month }
444456 selectedDays = { selectedDay }
445457 onDayClick = { this . handleDayClick }
446- onMonthChange = { month => this . setState ( { month } ) }
458+ onMonthChange = { this . handleMonthChange }
447459 />
448460 </ Overlay >
449461 ) ;
Original file line number Diff line number Diff line change @@ -370,5 +370,40 @@ describe('DayPickerInput', () => {
370370 expect ( selectedDays . at ( 1 ) ) . toHaveText ( '9' ) ;
371371 } ) ;
372372 } ) ;
373+
374+ describe ( 'onMonthChange' , ( ) => {
375+ it ( 'should update state when month changes' , ( ) => {
376+ const wrapper = mount (
377+ < DayPickerInput
378+ dayPickerProps = { {
379+ initialMonth : new Date ( 2015 , 7 ) ,
380+ } }
381+ />
382+ ) ;
383+ const instance = wrapper . instance ( ) ;
384+ instance . showDayPicker ( ) ;
385+ wrapper . update ( ) ;
386+ instance . getDayPicker ( ) . showNextMonth ( ) ;
387+ expect ( instance . state . month . getMonth ( ) ) . toEqual ( 8 ) ;
388+ } ) ;
389+
390+ it ( 'should call onMonthChange when month changes' , ( ) => {
391+ const handleMonthChange = jest . fn ( ) ;
392+ const wrapper = mount (
393+ < DayPickerInput
394+ dayPickerProps = { {
395+ onMonthChange : handleMonthChange ,
396+ initialMonth : new Date ( 2015 , 7 ) ,
397+ } }
398+ />
399+ ) ;
400+ const instance = wrapper . instance ( ) ;
401+ instance . showDayPicker ( ) ;
402+ wrapper . update ( ) ;
403+ instance . getDayPicker ( ) . showNextMonth ( ) ;
404+ expect ( handleMonthChange ) . toHaveBeenCalled ( ) ;
405+ expect ( handleMonthChange . mock . calls [ 0 ] [ 0 ] . getMonth ( ) ) . toEqual ( 8 ) ;
406+ } ) ;
407+ } ) ;
373408 } ) ;
374409} ) ;
You can’t perform that action at this time.
0 commit comments