import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Radio } from 'antd'
|
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
class MKRadio extends Component {
|
static propTpyes = {
|
config: PropTypes.object,
|
onChange: PropTypes.func
|
}
|
|
state = {
|
value: this.props.config.initval,
|
config: fromJS(this.props.config).toJS(),
|
options: fromJS(this.props.config.options).toJS(),
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('mkFP', this.mkFormHandle)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('mkFP', this.mkFormHandle)
|
}
|
|
mkFormHandle = (field, parentId) => {
|
const { config } = this.state
|
|
if (field !== config.field) return
|
|
let options = config.oriOptions ? config.oriOptions.filter(option => option.ParentID === parentId) : []
|
let val = options[0] ? options[0].value : ''
|
|
this.setState({
|
options,
|
value: val
|
})
|
|
this.props.onChange(val)
|
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkFP', m, val)
|
}, (i + 1) * 70)
|
})
|
}
|
|
onChange = (e) => {
|
const { config } = this.state
|
let value = e.target.value
|
let other = {}
|
|
if (config.subFields) {
|
let option = this.state.options.filter(m => m.value === value)[0]
|
option && config.subFields.forEach((n, i) => {
|
other[n] = option[n] || ''
|
setTimeout(() => {
|
MKEmitter.emit('mkFC', 'input', n, option[n] || '')
|
}, i * 5)
|
})
|
}
|
|
config.linkFields && config.linkFields.forEach((m, i) => {
|
setTimeout(() => {
|
MKEmitter.emit('mkFP', m, value)
|
}, (i + 1) * 100)
|
})
|
|
this.setState({value})
|
this.props.onChange(value, other)
|
}
|
|
render() {
|
const { value, options } = this.state
|
|
return (
|
<Radio.Group style={{whiteSpace: 'nowrap'}} value={value} onChange={this.onChange}>
|
{options.map(option => <Radio key={option.value} value={option.value}>{option.label}</Radio>)}
|
</Radio.Group>
|
)
|
}
|
}
|
|
export default MKRadio
|