@@ -13,16 +13,29 @@ class Calendar extends Component {
1313 constructor ( props ) {
1414 super ( props ) ;
1515
16+ let currentDateDisplayed = moment ( ) . startOf ( 'day' ) ;
17+ let selectedDateOrDates = ! props . enableRangeSelection ? moment ( { year : 0 } ) : [ ] ;
18+
19+ const customDateEmpty = ( ! props . customDate || ( props . customDate && props . customDate . length === 0 ) ) ;
20+
21+ if ( ! customDateEmpty ) {
22+ selectedDateOrDates = props . customDate ;
23+ currentDateDisplayed = props . customDate ;
24+
25+ if ( props . customDate . length ) {
26+ currentDateDisplayed = props . customDate [ 0 ] ;
27+ }
28+ }
29+
1630 this . state = {
1731 todayDate : moment ( ) . startOf ( 'day' ) ,
1832 gridBoundaryContext : null ,
19- refocusGrid : this . props . focusOnInit ,
20- currentDateDisplayed : moment ( ) . startOf ( 'day' ) ,
21- arrSelectedDates : [ ] ,
22- selectedDate : moment ( { year : 0 } ) ,
33+ refocusGrid : props . focusOnInit ,
34+ currentDateDisplayed : currentDateDisplayed ,
35+ arrSelectedDates : props . enableRangeSelection ? selectedDateOrDates : [ ] ,
36+ selectedDate : ! props . enableRangeSelection ? selectedDateOrDates : null ,
2337 showMonths : false ,
24- showYears : false ,
25- dateClick : false
38+ showYears : false
2639 } ;
2740
2841 this . tableRef = React . createRef ( ) ;
@@ -35,36 +48,6 @@ class Calendar extends Component {
3548 this . gridManager = new GridManager ( this . getGridOptions ( ) ) ;
3649 }
3750
38- // sync the selected date of the calendar with the date picker
39- static getDerivedStateFromProps ( updatedPropsParent , previousStates ) {
40- const { customDate, enableRangeSelection } = updatedPropsParent ;
41-
42- if ( typeof customDate === 'undefined' ) {
43- return null ;
44- }
45-
46- if ( ! previousStates . dateClick ) {
47- if ( typeof enableRangeSelection !== 'undefined' ) {
48- if ( customDate !== previousStates . arrSelectedDates ) {
49- if ( ! customDate || ! customDate . length ) {
50- // reset calendar state when date picker input is empty and did not click on a date
51- return ( { currentDateDisplayed : moment ( ) . startOf ( 'day' ) , arrSelectedDates : [ ] , selectedDate : moment ( { year : 0 } ) } ) ;
52- }
53- // update calendar state with date picker input
54- return ( { currentDateDisplayed : customDate [ 0 ] , arrSelectedDates : customDate , selectedDate : moment ( { year : 0 } ) } ) ;
55- }
56- } else if ( customDate !== previousStates . currentDateDisplayed ) {
57- if ( ! customDate ) {
58- // reset calendar state when date picker input is empty and did not click on a date
59- return ( { currentDateDisplayed : moment ( ) . startOf ( 'day' ) , selectedDate : moment ( { year : 0 } ) } ) ;
60- }
61- // update calendar state with date picker input
62- return ( { currentDateDisplayed : customDate , selectedDate : customDate } ) ;
63- }
64- }
65- return ( { dateClick : false } ) ;
66- }
67-
6851 componentDidUpdate = ( prevProps , prevState ) => {
6952 // if switching picker view or switching to a new month or year, reconstruct grid
7053 const newView = this . state . showMonths !== prevState . showMonths || this . state . showYears !== prevState . showYears ;
@@ -117,7 +100,7 @@ class Calendar extends Component {
117100 firstFocusedCoordinates = { row : firstFocusedRow , col : firstFocusedCol } ;
118101 }
119102
120- this . setState ( { gridBoundaryContext : null , refocusGrid : false , dateClick : true } ) ;
103+ this . setState ( { gridBoundaryContext : null , refocusGrid : false } ) ;
121104
122105 return {
123106 gridNode : this . tableRef . current ,
@@ -136,8 +119,7 @@ class Calendar extends Component {
136119 showMonths = ( ) => {
137120 this . setState ( {
138121 showMonths : ! this . state . showMonths ,
139- showYears : false ,
140- dateClick : true
122+ showYears : false
141123 } ) ;
142124 }
143125
@@ -169,8 +151,7 @@ class Calendar extends Component {
169151 showYears = ( ) => {
170152 this . setState ( {
171153 showMonths : false ,
172- showYears : ! this . state . showYears ,
173- dateClick : true
154+ showYears : ! this . state . showYears
174155 } ) ;
175156 }
176157
@@ -181,8 +162,7 @@ class Calendar extends Component {
181162
182163 this . setState ( {
183164 currentDateDisplayed : newDate ,
184- showMonths : false ,
185- dateClick : true
165+ showMonths : false
186166 } ) ;
187167 }
188168
@@ -191,15 +171,14 @@ class Calendar extends Component {
191171
192172 this . setState ( {
193173 currentDateDisplayed : newDate ,
194- showYears : false ,
195- dateClick : true
174+ showYears : false
196175 } ) ;
197176 }
198177
199178 onPassGridBoundary = ( { currentCell, directionX, directionY } ) => {
200179 if ( ! this . state . showMonths && ! this . state . showYears ) {
201180 currentCell . element . setAttribute ( 'tabindex' , - 1 ) ;
202- this . setState ( { gridBoundaryContext : { currentCell, directionX, directionY } , dateClick : true } ) ;
181+ this . setState ( { gridBoundaryContext : { currentCell, directionX, directionY } } ) ;
203182
204183 if ( directionX === - 1 || directionY === - 1 ) {
205184 this . handlePrevious ( ) ;
@@ -222,8 +201,7 @@ class Calendar extends Component {
222201 newDate . date ( newDate . daysInMonth ( ) < focusedDate ? newDate . daysInMonth ( ) : focusedDate ) ;
223202 this . setState ( {
224203 currentDateDisplayed : newDate ,
225- refocusGrid : true ,
226- dateClick : true
204+ refocusGrid : true
227205 } ) ;
228206 break ;
229207 case 'page down' :
@@ -232,8 +210,7 @@ class Calendar extends Component {
232210 newDate . date ( newDate . daysInMonth ( ) < focusedDate ? newDate . daysInMonth ( ) : focusedDate ) ;
233211 this . setState ( {
234212 currentDateDisplayed : newDate ,
235- refocusGrid : true ,
236- dateClick : true
213+ refocusGrid : true
237214 } ) ;
238215 break ;
239216 default :
@@ -364,21 +341,21 @@ class Calendar extends Component {
364341 const { currentDateDisplayed } = this . state ;
365342 if ( this . state . showYears ) {
366343 const newDate = moment ( currentDateDisplayed ) . add ( 12 , 'year' ) ;
367- this . setState ( { currentDateDisplayed : newDate , dateClick : true } ) ;
344+ this . setState ( { currentDateDisplayed : newDate } ) ;
368345 } else {
369346 const newDate = moment ( currentDateDisplayed ) . add ( 1 , 'month' ) ;
370- this . setState ( { currentDateDisplayed : newDate , dateClick : true } ) ;
347+ this . setState ( { currentDateDisplayed : newDate } ) ;
371348 }
372349 }
373350
374351 handlePrevious = ( ) => {
375352 const { currentDateDisplayed } = this . state ;
376353 if ( this . state . showYears ) {
377354 const newDate = moment ( currentDateDisplayed ) . subtract ( 12 , 'year' ) ;
378- this . setState ( { currentDateDisplayed : newDate , dateClick : true } ) ;
355+ this . setState ( { currentDateDisplayed : newDate } ) ;
379356 } else {
380357 const newDate = moment ( currentDateDisplayed ) . subtract ( 1 , 'month' ) ;
381- this . setState ( { currentDateDisplayed : newDate , dateClick : true } ) ;
358+ this . setState ( { currentDateDisplayed : newDate } ) ;
382359 }
383360 }
384361
@@ -398,8 +375,7 @@ class Calendar extends Component {
398375 this . setState ( {
399376 currentDateDisplayed : day ,
400377 selectedDate : day ,
401- arrSelectedDates : selectedDates ,
402- dateClick : true
378+ arrSelectedDates : selectedDates
403379 } , function ( ) {
404380 if ( isRangeEnabled ) {
405381 this . props . onChange ( selectedDates ) ;
@@ -430,7 +406,7 @@ class Calendar extends Component {
430406
431407 return (
432408 < header className = 'fd-calendar__header' >
433- < div aria-live = 'polite ' className = 'fd-calendar__navigation' >
409+ < div aria-live = 'assertive ' className = 'fd-calendar__navigation' >
434410 < div className = 'fd-calendar__action' >
435411 < Button
436412 aria-label = { previousButtonLabel }
0 commit comments