Skip to content

Commit 8d3b25d

Browse files
committed
feat(CascaderSelect): upgrade life circle
1 parent af6e701 commit 8d3b25d

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/cascader-select/cascader-select.jsx

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3+
import { polyfill } from 'react-lifecycles-compat';
34
import classNames from 'classnames';
45
import Select from '../select';
56
import Cascader from '../cascader';
@@ -10,10 +11,22 @@ const { bindCtx } = func;
1011
const { pickOthers } = obj;
1112
const { getStyle } = dom;
1213

14+
const normalizeValue = value => {
15+
if (value) {
16+
if (Array.isArray(value)) {
17+
return value;
18+
}
19+
20+
return [value];
21+
}
22+
23+
return [];
24+
};
25+
1326
/**
1427
* CascaderSelect
1528
*/
16-
export default class CascaderSelect extends Component {
29+
class CascaderSelect extends Component {
1730
static propTypes = {
1831
prefix: PropTypes.string,
1932
pure: PropTypes.bool,
@@ -266,7 +279,7 @@ export default class CascaderSelect extends Component {
266279
super(props, context);
267280

268281
this.state = {
269-
value: this.normalizeValue(
282+
value: normalizeValue(
270283
'value' in props ? props.value : props.defaultValue
271284
),
272285
searchValue: '',
@@ -291,31 +304,17 @@ export default class CascaderSelect extends Component {
291304
]);
292305
}
293306

294-
componentWillReceiveProps(nextProps) {
307+
static getDerivedStateFromProps(props) {
295308
const st = {};
296309

297-
if ('value' in nextProps) {
298-
st.value = this.normalizeValue(nextProps.value);
310+
if ('value' in props) {
311+
st.value = normalizeValue(props.value);
299312
}
300-
if ('visible' in nextProps) {
301-
st.visible = nextProps.visible;
313+
if ('visible' in props) {
314+
st.visible = props.visible;
302315
}
303316

304-
if (Object.keys(st).length) {
305-
this.setState(st);
306-
}
307-
}
308-
309-
normalizeValue(value) {
310-
if (value) {
311-
if (Array.isArray(value)) {
312-
return value;
313-
}
314-
315-
return [value];
316-
}
317-
318-
return [];
317+
return st;
319318
}
320319

321320
updateCache(dataSource) {
@@ -917,3 +916,5 @@ export default class CascaderSelect extends Component {
917916
return <Select {...props} {...others} />;
918917
}
919918
}
919+
920+
export default polyfill(CascaderSelect);

0 commit comments

Comments
 (0)