Skip to content

Commit

Permalink
Merge pull request #630 from XiaoMi/hotfix/checkbox&radio&form
Browse files Browse the repository at this point in the history
Hotfix/checkbox&radio&form
  • Loading branch information
solarjoker authored Sep 4, 2019
2 parents cced73e + 1a94b0a commit 72fce1f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 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 = 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([
Expand Down
3 changes: 2 additions & 1 deletion components/form/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -215,4 +216,4 @@ FormItem.defaultProps = {
size: 'small'
}

export default FormItem
export default depreactedPropsCompat([['field', 'prop']])(FormItem)
3 changes: 1 addition & 2 deletions components/form/index.js
Original file line number Diff line number Diff line change
@@ -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
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 = 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([
Expand Down
21 changes: 16 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72fce1f

Please sign in to comment.