king
2021-10-14 dddb2c96f42d9c852dba26ff9a27daa12bd85008
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React, {Component} from 'react'
import { is, fromJS } from 'immutable'
import { Select } from 'antd'
 
import MKEmitter from '@/utils/events.js'
 
class MKSelect extends Component {
  constructor(props) {
    super(props)
    
    const config = props.config
    let value = config.initval
 
    if (config.type === 'multiselect') {
      value = value || []
    }
 
    this.state = {
      config: fromJS(config).toJS(),
      options: fromJS(config.options).toJS(),
      value,
    }
  }
 
  componentDidMount () {
    const { config } = this.state
 
    if (config.type !== 'multiselect') {
      MKEmitter.addListener('mkFP', this.mkFormHandle)
    }
    MKEmitter.addListener('mkFC', this.mkFormControl)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('mkFP', this.mkFormHandle)
    MKEmitter.removeListener('mkFC', this.mkFormControl)
  }
 
  mkFormControl = (type, field, value) => {
    if (field !== this.props.config.field) return
    
    if (type === 'input') {
      this.setState({value})
      this.props.onChange(value, {})
    }
  }
 
  mkFormHandle = (field, parentId) => {
    if (field !== this.state.config.field) return
 
    const { config } = this.state
 
    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)
    })
  }
 
  selectChange = (val) => {
    const { config } = this.state
    let other = {}
 
    if (config.subFields) {
      let option = this.state.options.filter(m => m.value === val)[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, val)
      }, (i + 1) * 100)
    })
 
    this.setState({value: val})
    this.props.onChange(val, other)
  }
 
  mutilselectChange = (val) => {
    this.props.onChange(val)
  }
 
  render() {
    const { value, config, options } = this.state
 
    if (config.type !== 'multiselect') {
      return (
        <Select
          showSearch
          allowClear
          value={value}
          filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
          onSelect={this.selectChange}
          onChange={(val) => val === undefined && this.selectChange('')}
        >
          {options.map((option, i) =>
            <Select.Option key={i} disabled={option.disabled} value={option.value || option.field || ''}>{option.label || option.text}</Select.Option>
          )}
        </Select>
      )
    } else {
      return (<Select
        showSearch
        mode="multiple"
        defaultValue={value}
        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
        onChange={this.mutilselectChange}
      >
        {options.map(option =>
          <Select.Option key={option.value} disabled={option.disabled} value={option.value}>{option.label || option.text}</Select.Option>
        )}
      </Select>)
    }
  }
}
 
export default MKSelect