Skip to content

Commit a368934

Browse files
authored
Merge pull request #6 from wojtekmaj/react-17
Implement compatibility with React 17
2 parents 9639266 + 1c3079f commit a368934

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"lodash.once": "^4.1.1",
4141
"merge-class-names": "^1.1.1",
4242
"prop-types": "^15.6.0",
43-
"react-clock": "^2.2.1"
43+
"react-clock": "^2.2.1",
44+
"react-lifecycles-compat": "^1.1.0"
4445
},
4546
"devDependencies": {
4647
"babel-cli": "^6.26.0",

src/TimeInput.jsx

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33

44
import Divider from './Divider';
@@ -60,30 +60,46 @@ const removeUnwantedCharacters = str => str
6060
))
6161
.join('');
6262

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 = {};
7366

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+
}
7774

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;
7881
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)
8285
) {
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;
8496
}
97+
98+
return nextState;
8599
}
86100

101+
state = {};
102+
87103
/**
88104
* Gets current value in a desired format.
89105
*/
@@ -97,8 +113,7 @@ export default class TimeInput extends Component {
97113
* Returns value type that can be returned with currently applied settings.
98114
*/
99115
get valueType() {
100-
const { maxDetail } = this.props;
101-
return maxDetail;
116+
return this.props.maxDetail;
102117
}
103118

104119
get nativeValueParser() {
@@ -158,16 +173,6 @@ export default class TimeInput extends Component {
158173
};
159174
}
160175

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-
171176
onKeyDown = (event) => {
172177
switch (event.key) {
173178
case 'ArrowLeft': {

src/TimePicker.jsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@ import { isTime } from './shared/propTypes';
1212
const allViews = ['hour', 'minute', 'second'];
1313

1414
export default class TimePicker extends PureComponent {
15-
state = {
16-
isOpen: this.props.isOpen,
15+
static getDerivedStateFromProps(nextProps, prevState) {
16+
if (nextProps.isOpen !== prevState.propsIsOpen) {
17+
return {
18+
isOpen: nextProps.isOpen,
19+
propsIsOpen: nextProps.isOpen,
20+
};
21+
}
22+
23+
return null;
1724
}
1825

26+
state = {};
27+
1928
componentDidMount() {
2029
document.addEventListener('mousedown', this.onClick);
2130
}
@@ -24,14 +33,6 @@ export default class TimePicker extends PureComponent {
2433
document.removeEventListener('mousedown', this.onClick);
2534
}
2635

27-
componentWillReceiveProps(nextProps) {
28-
const { props } = this;
29-
30-
if (nextProps.isOpen !== props.isOpen) {
31-
this.setState({ isOpen: nextProps.isOpen });
32-
}
33-
}
34-
3536
onClick = (event) => {
3637
if (this.wrapper && !this.wrapper.contains(event.target)) {
3738
this.closeClock();

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3699,6 +3699,10 @@ react-dom@^16.3.0:
36993699
object-assign "^4.1.1"
37003700
prop-types "^15.6.0"
37013701

3702+
react-lifecycles-compat@^1.1.0:
3703+
version "1.1.0"
3704+
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-1.1.0.tgz#6641d0709bd5505329b5c90322147ef2d343485c"
3705+
37023706
react-reconciler@^0.7.0:
37033707
version "0.7.0"
37043708
resolved "https://registry.yarnpkg.com/react-reconciler/-/react-reconciler-0.7.0.tgz#9614894103e5f138deeeb5eabaf3ee80eb1d026d"

0 commit comments

Comments
 (0)