1- import React , { Component } from 'react' ;
1+ import React , { PureComponent } from 'react' ;
22import PropTypes from 'prop-types' ;
3+ import polyfill from 'react-lifecycles-compat' ;
34
45import DayInput from 'react-date-picker/dist/DateInput/DayInput' ;
56import MonthInput from 'react-date-picker/dist/DateInput/MonthInput' ;
@@ -13,11 +14,11 @@ import NativeInput from './DateTimeInput/NativeInput';
1314import { formatDate , formatTime } from './shared/dateFormatter' ;
1415import {
1516 getDay ,
16- getMonth ,
17- getYear ,
1817 getHours ,
1918 getMinutes ,
19+ getMonth ,
2020 getSeconds ,
21+ getYear ,
2122} from './shared/dates' ;
2223import { isMaxDate , isMinDate } from './shared/propTypes' ;
2324
@@ -64,41 +65,52 @@ const removeUnwantedCharacters = str => str
6465 ) )
6566 . join ( '' ) ;
6667
67- export default class DateTimeInput extends Component {
68- state = {
69- year : null ,
70- month : null ,
71- day : null ,
72- hour : null ,
73- minute : null ,
74- second : null ,
75- }
68+ export default class DateTimeInput extends PureComponent {
69+ static getDerivedStateFromProps ( nextProps , prevState ) {
70+ const nextState = { } ;
7671
77- componentWillMount ( ) {
78- this . updateValues ( ) ;
79- }
80-
81- componentWillReceiveProps ( nextProps ) {
82- const { value : nextValue } = nextProps ;
83- const { value } = this . props ;
72+ /**
73+ * If isWidgetOpen flag has changed, we have to update it.
74+ * It's saved in state purely for use in getDerivedStateFromProps.
75+ */
76+ if ( nextProps . isWidgetOpen !== prevState . isWidgetOpen ) {
77+ nextState . isWidgetOpen = nextProps . isWidgetOpen ;
78+ }
8479
80+ /**
81+ * If the next value is different from the current one (with an exception of situation in
82+ * which values provided are limited by minDate and maxDate so that the dates are the same),
83+ * get a new one.
84+ */
85+ const nextValue = nextProps . value ;
8586 if (
86- // Toggling clock visibility resets values
87- ( nextProps . isWidgetOpen !== this . props . isWidgetOpen ) ||
88- datesAreDifferent ( nextValue , value )
87+ // Toggling calendar visibility resets values
88+ nextState . isCalendarOpen || // Flag was toggled
89+ datesAreDifferent ( nextValue , prevState . value )
8990 ) {
90- this . updateValues ( nextProps ) ;
91+ if ( nextValue ) {
92+ nextState . year = getYear ( nextValue ) ;
93+ nextState . month = getMonth ( nextValue ) ;
94+ nextState . day = getDay ( nextValue ) ;
95+ nextState . hour = getHours ( nextValue ) ;
96+ nextState . minute = getMinutes ( nextValue ) ;
97+ nextState . second = getSeconds ( nextValue ) ;
98+ } else {
99+ nextState . year = null ;
100+ nextState . month = null ;
101+ nextState . day = null ;
102+ nextState . hour = null ;
103+ nextState . minute = null ;
104+ nextState . second = null ;
105+ }
106+ nextState . value = nextValue ;
91107 }
92- }
93108
94- /**
95- * Returns value type that can be returned with currently applied settings.
96- */
97- get valueType ( ) {
98- const { maxDetail } = this . props ;
99- return maxDetail ;
109+ return nextState ;
100110 }
101111
112+ state = { } ;
113+
102114 // eslint-disable-next-line class-methods-use-this
103115 get dateDivider ( ) {
104116 const { locale } = this . props ;
@@ -168,17 +180,11 @@ export default class DateTimeInput extends Component {
168180 } ;
169181 }
170182
171- updateValues ( props = this . props ) {
172- const { value } = props ;
173-
174- this . setState ( {
175- year : value ? getYear ( value ) : null ,
176- month : value ? getMonth ( value ) : null ,
177- day : value ? getDay ( value ) : null ,
178- hour : value ? getHours ( value ) : null ,
179- minute : value ? getMinutes ( value ) : null ,
180- second : value ? getSeconds ( value ) : null ,
181- } ) ;
183+ /**
184+ * Returns value type that can be returned with currently applied settings.
185+ */
186+ get valueType ( ) {
187+ return this . props . maxDetail ;
182188 }
183189
184190 onKeyDown = ( event ) => {
@@ -471,3 +477,5 @@ DateTimeInput.propTypes = {
471477 PropTypes . instanceOf ( Date ) ,
472478 ] ) ,
473479} ;
480+
481+ polyfill ( DateTimeInput ) ;
0 commit comments