1
- import React , { Component } from 'react' ;
1
+ import React , { PureComponent } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
4
import Divider from './Divider' ;
@@ -60,30 +60,46 @@ const removeUnwantedCharacters = str => str
60
60
) )
61
61
. join ( '' ) ;
62
62
63
- export default class TimeInput extends Component {
64
- state = {
65
- hour : null ,
66
- minute : null ,
67
- second : null ,
68
- }
69
-
70
- componentWillMount ( ) {
71
- this . updateValues ( ) ;
72
- }
63
+ export default class TimeInput extends PureComponent {
64
+ static getDerivedStateFromProps ( nextProps , prevState ) {
65
+ const nextState = { } ;
73
66
74
- componentWillReceiveProps ( nextProps ) {
75
- const { value : nextValue } = nextProps ;
76
- const { value } = this . props ;
67
+ /**
68
+ * If isClockOpen flag has changed, we have to update it.
69
+ * It's saved in state purely for use in getDerivedStateFromProps.
70
+ */
71
+ if ( nextProps . isClockOpen !== prevState . isClockOpen ) {
72
+ nextState . isClockOpen = nextProps . isClockOpen ;
73
+ }
77
74
75
+ /**
76
+ * If the next value is different from the current one (with an exception of situation in
77
+ * which values provided are limited by minDate and maxDate so that the dates are the same),
78
+ * get a new one.
79
+ */
80
+ const nextValue = nextProps . value ;
78
81
if (
79
- // Toggling clock visibility resets values
80
- ( nextProps . isClockOpen !== this . props . isClockOpen ) ||
81
- hoursAreDifferent ( nextValue , value )
82
+ // Toggling calendar visibility resets values
83
+ nextState . isClockOpen || // Flag was toggled
84
+ hoursAreDifferent ( nextValue , prevState . value )
82
85
) {
83
- this . updateValues ( nextProps ) ;
86
+ if ( nextValue ) {
87
+ nextState . hour = getHours ( nextValue ) ;
88
+ nextState . minute = getMinutes ( nextValue ) ;
89
+ nextState . second = getSeconds ( nextValue ) ;
90
+ } else {
91
+ nextState . hour = null ;
92
+ nextState . minute = null ;
93
+ nextState . second = null ;
94
+ }
95
+ nextState . value = nextValue ;
84
96
}
97
+
98
+ return nextState ;
85
99
}
86
100
101
+ state = { } ;
102
+
87
103
/**
88
104
* Gets current value in a desired format.
89
105
*/
@@ -97,8 +113,7 @@ export default class TimeInput extends Component {
97
113
* Returns value type that can be returned with currently applied settings.
98
114
*/
99
115
get valueType ( ) {
100
- const { maxDetail } = this . props ;
101
- return maxDetail ;
116
+ return this . props . maxDetail ;
102
117
}
103
118
104
119
get nativeValueParser ( ) {
@@ -158,16 +173,6 @@ export default class TimeInput extends Component {
158
173
} ;
159
174
}
160
175
161
- updateValues ( props = this . props ) {
162
- const { value } = props ;
163
-
164
- this . setState ( {
165
- hour : value ? getHours ( value ) : null ,
166
- minute : value ? getMinutes ( value ) : null ,
167
- second : value ? getSeconds ( value ) : null ,
168
- } ) ;
169
- }
170
-
171
176
onKeyDown = ( event ) => {
172
177
switch ( event . key ) {
173
178
case 'ArrowLeft' : {
0 commit comments