king
2022-06-10 bf292de8879e2ce71696cb3ddc4dc8fa88d91a20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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'
 
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, config } = this.state
 
    return (
      <Radio.Group style={{whiteSpace: 'nowrap'}} disabled={config.disabled} value={value} onChange={this.onChange}>
        {options.map(option => <Radio key={option.value} disabled={option.disabled} value={option.value}>{option.label}</Radio>)}
      </Radio.Group>
    )
  }
}
 
export default MKRadio