@@ -81,15 +81,15 @@ export default class Datetime extends React.Component {
8181
8282 constructor ( props ) {
8383 super ( props ) ;
84- this . state = this . getInitialState ( props ) ;
84+ this . state = this . getInitialState ( ) ;
8585 }
8686
8787 render ( ) {
8888 return (
8989 < ClickableWrapper className = { this . getClassName ( ) } onClickOut = { this . _handleClickOutside } >
9090 { this . renderInput ( ) }
9191 < div className = "rdtPicker" >
92- { this . renderView ( this . state . currentView , this . _renderCalendar ) }
92+ { this . renderView ( ) }
9393 </ div >
9494 </ ClickableWrapper >
9595 ) ;
@@ -122,14 +122,14 @@ export default class Datetime extends React.Component {
122122 ) ;
123123 }
124124
125- renderView ( currentView , renderer ) {
125+ renderView ( ) {
126126 if ( this . props . renderView ) {
127- return this . props . renderView ( currentView , ( ) => renderer ( currentView ) ) ;
127+ return this . props . renderView ( this . state . currentView , this . _renderCalendar ) ;
128128 }
129- return renderer ( this . state . currentView ) ;
129+ return this . _renderCalendar ( ) ;
130130 }
131131
132- _renderCalendar = currentView => {
132+ _renderCalendar = ( ) => {
133133 const props = this . props ;
134134 const state = this . state ;
135135
@@ -145,7 +145,7 @@ export default class Datetime extends React.Component {
145145
146146 // Probably updateOn, updateSelectedDate and setDate can be merged in the same method
147147 // that would update viewDate or selectedDate depending on the view and the dateFormat
148- switch ( currentView ) {
148+ switch ( state . currentView ) {
149149 case viewModes . YEARS :
150150 // Used viewProps
151151 // { viewDate, selectedDate, renderYear, isValidDate, navigate, showView, updateDate }
@@ -173,31 +173,32 @@ export default class Datetime extends React.Component {
173173 }
174174 }
175175
176- getInitialState ( p ) {
177- let props = p || this . props ;
176+ getInitialState ( ) {
177+ let props = this . props ;
178178 let inputFormat = this . getFormat ( 'datetime' ) ;
179179 let selectedDate = this . parseDate ( props . value || props . initialValue , inputFormat ) ;
180180
181- this . checkTZ ( props ) ;
181+ this . checkTZ ( ) ;
182182
183183 return {
184184 open : ! props . input ,
185- currentView : props . initialViewMode || this . getInitialView ( this . getFormat ( 'date' ) ) ,
186- viewDate : this . getInitialViewDate ( props . initialViewDate , selectedDate , inputFormat ) ,
185+ currentView : props . initialViewMode || this . getInitialView ( ) ,
186+ viewDate : this . getInitialViewDate ( selectedDate ) ,
187187 selectedDate : selectedDate && selectedDate . isValid ( ) ? selectedDate : undefined ,
188- inputValue : this . getInitialInputValue ( props , selectedDate , inputFormat )
188+ inputValue : this . getInitialInputValue ( selectedDate )
189189 } ;
190190 }
191191
192- getInitialViewDate ( propDate , selectedDate , format ) {
192+ getInitialViewDate ( selectedDate ) {
193+ const propDate = this . props . initialViewDate ;
193194 let viewDate ;
194195 if ( propDate ) {
195- viewDate = this . parseDate ( propDate , format ) ;
196+ viewDate = this . parseDate ( propDate , this . getFormat ( 'datetime' ) ) ;
196197 if ( viewDate && viewDate . isValid ( ) ) {
197198 return viewDate ;
198199 }
199200 else {
200- this . log ( 'The initialViewDated given "' + propDate + '" is not valid. Using current date instead.' ) ;
201+ log ( 'The initialViewDated given "' + propDate + '" is not valid. Using current date instead.' ) ;
201202 }
202203 }
203204 else if ( selectedDate && selectedDate . isValid ( ) ) {
@@ -212,9 +213,9 @@ export default class Datetime extends React.Component {
212213 return m ;
213214 }
214215
215- getInitialView ( dateFormat ) {
216- if ( ! dateFormat ) return viewModes . TIME ;
217- return this . getUpdateOn ( dateFormat ) ;
216+ getInitialView ( ) {
217+ const dateFormat = this . getFormat ( 'date' ) ;
218+ return dateFormat ? this . getUpdateOn ( dateFormat ) : viewModes . TIME ;
218219 }
219220
220221 parseDate ( date , dateFormat ) {
@@ -277,19 +278,21 @@ export default class Datetime extends React.Component {
277278 return viewModes . DAYS ;
278279 }
279280
280- getLocaleData ( props ) {
281- let p = props || this . props ;
281+ getLocaleData ( ) {
282+ let p = this . props ;
282283 return this . localMoment ( p . value || p . defaultValue || new Date ( ) ) . localeData ( ) ;
283284 }
284285
285- getDateFormat ( locale ) {
286+ getDateFormat ( ) {
287+ const locale = this . getLocaleData ( ) ;
286288 let format = this . props . dateFormat ;
287289 if ( format === true ) return locale . longDateFormat ( 'L' ) ;
288290 if ( format ) return format ;
289291 return '' ;
290292 }
291293
292- getTimeFormat ( locale ) {
294+ getTimeFormat ( ) {
295+ const locale = this . getLocaleData ( ) ;
293296 let format = this . props . timeFormat ;
294297 if ( format === true ) {
295298 return locale . longDateFormat ( 'LT' ) ;
@@ -299,15 +302,14 @@ export default class Datetime extends React.Component {
299302
300303 getFormat ( type ) {
301304 if ( type === 'date' ) {
302- return this . getDateFormat ( this . getLocaleData ( ) ) ;
305+ return this . getDateFormat ( ) ;
303306 }
304307 else if ( type === 'time' ) {
305- return this . getTimeFormat ( this . getLocaleData ( ) ) ;
308+ return this . getTimeFormat ( ) ;
306309 }
307310
308- let locale = this . getLocaleData ( ) ;
309- let dateFormat = this . getDateFormat ( locale ) ;
310- let timeFormat = this . getTimeFormat ( locale ) ;
311+ let dateFormat = this . getDateFormat ( ) ;
312+ let timeFormat = this . getTimeFormat ( ) ;
311313 return dateFormat && timeFormat ? dateFormat + ' ' + timeFormat : ( dateFormat || timeFormat ) ;
312314 }
313315
@@ -437,10 +439,11 @@ export default class Datetime extends React.Component {
437439 return m ;
438440 }
439441
440- checkTZ ( props ) {
441- if ( props . displayTimeZone && ! this . tzWarning && ! moment . tz ) {
442+ checkTZ ( ) {
443+ const { displayTimeZone } = this . props ;
444+ if ( displayTimeZone && ! this . tzWarning && ! moment . tz ) {
442445 this . tzWarning = true ;
443- this . log ( 'displayTimeZone prop with value "' + props . displayTimeZone + '" is used but moment.js timezone is not loaded.' , 'error' ) ;
446+ log ( 'displayTimeZone prop with value "' + displayTimeZone + '" is used but moment.js timezone is not loaded.' , 'error' ) ;
444447 }
445448 }
446449
@@ -455,17 +458,18 @@ export default class Datetime extends React.Component {
455458 } ) ;
456459
457460 if ( needsUpdate ) {
458- this . regenerateDates ( this . props ) ;
461+ this . regenerateDates ( ) ;
459462 }
460463
461464 if ( thisProps . value && thisProps . value !== prevProps . value ) {
462465 this . setViewDate ( thisProps . value ) ;
463466 }
464467
465- this . checkTZ ( this . props ) ;
468+ this . checkTZ ( ) ;
466469 }
467470
468- regenerateDates ( props ) {
471+ regenerateDates ( ) {
472+ const props = this . props ;
469473 let viewDate = this . state . viewDate . clone ( ) ;
470474 let selectedDate = this . state . selectedDate && this . state . selectedDate . clone ( ) ;
471475
@@ -500,12 +504,13 @@ export default class Datetime extends React.Component {
500504 return selectedDate && selectedDate . isValid ( ) ? selectedDate : false ;
501505 }
502506
503- getInitialInputValue ( props , selectedDate , inputFormat ) {
507+ getInitialInputValue ( selectedDate ) {
508+ const props = this . props ;
504509 if ( props . inputProps . value )
505510 return props . inputProps . value ;
506511
507512 if ( selectedDate && selectedDate . isValid ( ) )
508- return selectedDate . format ( inputFormat ) ;
513+ return selectedDate . format ( this . getFormat ( 'datetime' ) ) ;
509514
510515 if ( props . value && typeof props . value === 'string' )
511516 return props . value ;
@@ -555,16 +560,6 @@ export default class Datetime extends React.Component {
555560 this . _showView ( mode ) ;
556561 }
557562
558- log ( message , method ) {
559- let con = typeof window !== 'undefined' && window . console ;
560- if ( ! con ) return ;
561-
562- if ( ! method ) {
563- method = 'warn' ;
564- }
565- con [ method ] ( '***react-datetime:' + message ) ;
566- }
567-
568563 _onInputFocus = e => {
569564 if ( ! this . callHandler ( this . props . inputProps . onFocus , e ) ) return ;
570565 this . _openCalendar ( ) ;
@@ -613,6 +608,16 @@ export default class Datetime extends React.Component {
613608 }
614609}
615610
611+ function log ( message , method ) {
612+ let con = typeof window !== 'undefined' && window . console ;
613+ if ( ! con ) return ;
614+
615+ if ( ! method ) {
616+ method = 'warn' ;
617+ }
618+ con [ method ] ( '***react-datetime:' + message ) ;
619+ }
620+
616621class ClickOutBase extends React . Component {
617622 container = React . createRef ( ) ;
618623
0 commit comments