Skip to content

Commit

Permalink
fix(checkbox): #592, #607
Browse files Browse the repository at this point in the history
  • Loading branch information
Bougie committed Sep 2, 2019
1 parent 6cc5348 commit 177f213
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 40 deletions.
37 changes: 17 additions & 20 deletions components/checkbox/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 = ['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)
}
})
}
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([
Expand Down
37 changes: 17 additions & 20 deletions components/radio/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 = ['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
}
})
}
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([
Expand Down

0 comments on commit 177f213

Please sign in to comment.