import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Radio } from 'antd'
|
|
class MKRadio extends Component {
|
static propTpyes = {
|
config: PropTypes.object,
|
onChange: PropTypes.func
|
}
|
|
state = {
|
value: '',
|
config: null,
|
options: []
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
let value = config.initval
|
|
if (value) {
|
let option = null
|
option= config.oriOptions[0]
|
if (typeof(value) === 'string' && option && typeof(option.value) === 'number') {
|
value = +value
|
if (isNaN(value)) {
|
value = config.initval
|
}
|
}
|
}
|
|
this.setState({
|
config: fromJS(config).toJS(),
|
options: fromJS(config.options).toJS(),
|
value,
|
})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { config } = this.state
|
|
if (!is(fromJS(config.oriOptions), fromJS(nextProps.config.oriOptions))) {
|
this.setState({
|
config: fromJS(nextProps.config).toJS(),
|
options: fromJS(nextProps.config.options).toJS()
|
})
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
onChange = (val) => {
|
this.props.onChange(val)
|
this.setState({value: val})
|
}
|
|
render() {
|
const { value, options } = this.state
|
|
return (
|
<Radio.Group value={value} onChange={(e) => this.onChange(e.target.value)}>
|
{options.map(option => <Radio key={option.key} value={option.Value}>{option.Text}</Radio>)}
|
</Radio.Group>
|
)
|
}
|
}
|
|
export default MKRadio
|