diff --git a/components/checkbox/Group.js b/components/checkbox/Group.js index a254d1187..26edc82d2 100644 --- a/components/checkbox/Group.js +++ b/components/checkbox/Group.js @@ -9,11 +9,11 @@ const prefixCls = 'hi-checkbox-group' class Group extends Component { constructor (props) { super(props) - this.state = getData(props) + this.state = { data: getData(props), value: props.value } } - static getDerivedStateFromProps (nextProps) { - if (hasValue(nextProps)) { - return getData(nextProps) + static getDerivedStateFromProps (nextProps, state) { + if (nextProps.value !== state.value || getData(nextProps) !== state.data) { + return { data: getData(nextProps), value: nextProps.value } } return null } @@ -63,27 +63,24 @@ class Group extends Component { } function hasValue (props) { - const has = (key) => Object.prototype.hasOwnProperty.call(props, key) - return has('value') + return props.value !== undefined } function getData (props) { const { data, value, defaultValue } = props const _value = hasValue(props) ? value : defaultValue - return { - data: data.map((item) => { - const isPlain = typeof item === 'string' - const label = isPlain ? item : item.content - const value = isPlain ? item : item.id - const disabled = !isPlain && item.disabled - return { - label, - value, - disabled, - checked: (_value || []).includes(value) - } - }) - } + return data.map((item) => { + const isPlain = ['string', 'number'].includes(typeof item) + const label = isPlain ? item : item.content + const value = isPlain ? item : item.id + const disabled = !isPlain && item.disabled + return { + label, + value, + disabled, + checked: (_value || []).includes(value) + } + }) } const PropTypesArrayOfStringOrNumber = PropTypes.oneOfType([ diff --git a/components/form/Item.js b/components/form/Item.js index 01c88b6e2..7b9f43006 100644 --- a/components/form/Item.js +++ b/components/form/Item.js @@ -2,6 +2,7 @@ import React, { Component } from 'react' import classNames from 'classnames' import AsyncValidator from 'async-validator' import PropTypes from 'prop-types' +import { depreactedPropsCompat } from '../_util' class FormItem extends Component { constructor (props, context) { @@ -215,4 +216,4 @@ FormItem.defaultProps = { size: 'small' } -export default FormItem +export default depreactedPropsCompat([['field', 'prop']])(FormItem) diff --git a/components/form/index.js b/components/form/index.js index 334e83aba..936f96218 100644 --- a/components/form/index.js +++ b/components/form/index.js @@ -1,8 +1,7 @@ import Form from './Form' import Item from './Item' import './style/index' -import { depreactedPropsCompat } from '../_util' -Form.Item = depreactedPropsCompat([['field', 'prop']])(Item) +Form.Item = Item export default Form diff --git a/components/radio/Group.js b/components/radio/Group.js index f833510f6..422ae68bb 100644 --- a/components/radio/Group.js +++ b/components/radio/Group.js @@ -10,11 +10,11 @@ const prefixCls = 'hi-radio-group' class Group extends React.Component { constructor (props) { super(props) - this.state = getData(props) + this.state = { data: getData(props), value: props.value } } - static getDerivedStateFromProps (nextProps) { - if (hasValue(nextProps)) { - return getData(nextProps) + static getDerivedStateFromProps (nextProps, state) { + if (nextProps.value !== state.value || getData(nextProps) !== state.data) { + return { data: getData(nextProps), value: nextProps.value } } return null } @@ -71,27 +71,24 @@ function GroupWrapper ({ children, type, ...restProps }) { } function hasValue (props) { - const has = (key) => Object.prototype.hasOwnProperty.call(props, key) - return has('value') + return props.value !== undefined } function getData (props) { const { data, value, defaultValue } = props const _value = hasValue(props) ? value : defaultValue - return { - data: data.map((item) => { - const isPlain = typeof item === 'string' - const label = isPlain ? item : item.content - const value = isPlain ? item : item.id - const disabled = !isPlain && item.disabled - return { - label, - value, - disabled, - checked: _value === value || Number(_value) === value - } - }) - } + return data.map((item) => { + const isPlain = ['string', 'number'].includes(typeof item) + const label = isPlain ? item : item.content + const value = isPlain ? item : item.id + const disabled = !isPlain && item.disabled + return { + label, + value, + disabled, + checked: _value === value || Number(_value) === value + } + }) } const PropTypesOfStringOrNumber = PropTypes.oneOfType([ diff --git a/package-lock.json b/package-lock.json index 181fff307..8aa0e3243 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@hi-ui/hiui", - "version": "2.1.1", + "version": "2.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3156,6 +3156,12 @@ "requires": { "ms": "2.0.0" } + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==", + "dev": true } } }, @@ -6939,6 +6945,12 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=", "dev": true + }, + "qs": { + "version": "6.7.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", + "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==", + "dev": true } } }, @@ -16539,10 +16551,9 @@ "dev": true }, "qs": { - "version": "6.7.0", - "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", - "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==", - "dev": true + "version": "6.8.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.8.0.tgz", + "integrity": "sha512-tPSkj8y92PfZVbinY1n84i1Qdx75lZjMQYx9WZhnkofyxzw2r7Ho39G3/aEvSUdebxpnnM4LZJCtvE/Aq3+s9w==" }, "query-string": { "version": "4.3.4",