File tree Expand file tree Collapse file tree 3 files changed +32
-6
lines changed Expand file tree Collapse file tree 3 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -35,7 +35,8 @@ export default class Inline extends React.Component {
3535 < DatePicker
3636 inline
3737 selected = { this . state . startDate }
38- onChange = { this . handleChange } />
38+ onChange = { this . handleChange }
39+ />
3940 </ div >
4041 </ div >
4142 ) ;
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ import {
1515 getSecond ,
1616 getMinute ,
1717 getHour ,
18- getMonth ,
1918 addDays ,
2019 addMonths ,
2120 addWeeks ,
@@ -31,13 +30,26 @@ import {
3130 getEffectiveMaxDate ,
3231 parseDate ,
3332 safeDateFormat ,
34- getHightLightDaysMap
33+ getHightLightDaysMap ,
34+ getYear ,
35+ getMonth
3536} from "./date_utils" ;
3637import onClickOutside from "react-onclickoutside" ;
3738
3839const outsideClickIgnoreClass = "react-datepicker-ignore-onclickoutside" ;
3940const WrappedCalendar = onClickOutside ( Calendar ) ;
4041
42+ // Compares dates year+month combinations
43+ function hasPreSelectionChanged ( date1 , date2 ) {
44+ if ( date1 && date2 ) {
45+ return (
46+ getMonth ( date1 ) !== getMonth ( date2 ) || getYear ( date1 ) !== getYear ( date2 )
47+ ) ;
48+ }
49+
50+ return date1 !== date2 ;
51+ }
52+
4153/**
4254 * General datepicker component.
4355 */
@@ -164,9 +176,10 @@ export default class DatePicker extends React.Component {
164176 }
165177
166178 componentWillReceiveProps ( nextProps ) {
167- const currentMonth = this . props . selected && getMonth ( this . props . selected ) ;
168- const nextMonth = nextProps . selected && getMonth ( nextProps . selected ) ;
169- if ( this . props . inline && currentMonth !== nextMonth ) {
179+ if (
180+ this . props . inline &&
181+ hasPreSelectionChanged ( this . props . selected , nextProps . selected )
182+ ) {
170183 this . setPreSelection ( nextProps . selected ) ;
171184 }
172185 if ( this . props . highlightDates !== nextProps . highlightDates ) {
Original file line number Diff line number Diff line change @@ -930,6 +930,18 @@ describe("DatePicker", () => {
930930 utils . formatDate ( datePicker . state ( "preSelection" ) , "YYYY-MM-DD" )
931931 ) . to . equal ( utils . formatDate ( future , "YYYY-MM-DD" ) ) ;
932932 } ) ;
933+ it ( "should switch month in inline mode immediately, when year is updated" , ( ) => {
934+ const selected = utils . newDate ( ) ;
935+ const future = utils . addYears ( utils . newDate ( ) , 1 ) ;
936+ const datePicker = mount ( < DatePicker inline selected = { selected } /> ) ;
937+ expect (
938+ utils . formatDate ( datePicker . state ( "preSelection" ) , "YYYY-MM-DD" )
939+ ) . to . equal ( utils . formatDate ( selected , "YYYY-MM-DD" ) ) ;
940+ datePicker . setProps ( { selected : future } ) ;
941+ expect (
942+ utils . formatDate ( datePicker . state ( "preSelection" ) , "YYYY-MM-DD" )
943+ ) . to . equal ( utils . formatDate ( future , "YYYY-MM-DD" ) ) ;
944+ } ) ;
933945 it ( "should not set open state when focusing on the date input and the preventOpenOnFocus prop is set" , ( ) => {
934946 const datePicker = TestUtils . renderIntoDocument (
935947 < DatePicker preventOpenOnFocus />
You can’t perform that action at this time.
0 commit comments