king
2021-09-01 31ec63f0419895876cbaba99637a884a32d33d0d
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, {Component} from 'react'
import { is, fromJS } from 'immutable'
import { Select } from 'antd'
 
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
class MKSelect extends Component {
  constructor(props) {
    super(props)
    
    const config = props.config
    let value = config.initval
 
    if (config.type === 'multiselect') {
      if (value) {
        value = value.split(',')
      } else {
        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.mkFormFocus)
  }
 
  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()
      })
 
      if (typeof(config.initval) === 'string' && config.initval.indexOf('$first') > -1) {
        this.setState({
          value: nextProps.config.initval,
        })
      }
    }
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('mkFP', this.mkFormHandle)
    MKEmitter.removeListener('mkFC', this.mkFormFocus)
  }
 
  mkFormFocus = (type, uuid) => {
    if (uuid !== this.props.config.uuid) return
    if (type !== 'focus') return
 
    let _div = document.getElementById(uuid)
    _div && _div.click && _div.click()
  }
 
  mkFormHandle = (uuid, parentId, level) => {
    if (uuid !== this.state.config.uuid) return
 
    const { config } = this.state
 
    let options = config.oriOptions.filter(option => option.ParentID === parentId || option.value === '')
    let _option = options[0] || null
    let val = _option ? _option.value : ''
 
    this.setState({
      options,
      value: val
    })
 
    let other = {}
 
    if (config.subFields && _option) {
      config.subFields.forEach((n, i) => {
        other[n.field] = _option[n.field]
        setTimeout(() => {
          MKEmitter.emit('mkFC', 'input', n.uuid, _option[n.field])
        }, i * 5)
      })
    }
 
    this.props.onChange(val, other)
 
    if (level < 7 && config.linkFields) {
      config.linkFields.forEach((m, i) => {
        setTimeout(() => {
          MKEmitter.emit('mkFP', m.uuid, val, level + 1)
        }, (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.field] = option[n.field]
        setTimeout(() => {
          MKEmitter.emit('mkFC', 'input', n.uuid, option[n.field])
        }, i * 5)
      })
    }
    if (config.linkFields) {
      config.linkFields.forEach((m, i) => {
        setTimeout(() => {
          MKEmitter.emit('mkFP', m.uuid, val, 0)
        }, (i + 1) * 100)
      })
    }
 
    this.props.onChange(val, other)
    this.setState({value: val}, () => {
      if (config.enter === 'tab') {
        MKEmitter.emit('mkFC', 'focus', config.tabUuid)
      } else if (config.enter === 'sub') {
        if (config.linkFields || config.subFields || config.controlFields) {
          setTimeout(() => {
            this.props.onSubmit()
          }, 1000)
        } else {
          this.props.onSubmit()
        }
      }
    })
  }
 
  mutilselectChange = (val) => {
    this.props.onChange(val.join(','))
  }
 
  render() {
    const { value, config, options } = this.state
 
    if (config.type !== 'multiselect') {
      return (
        <Select
          showSearch
          allowClear
          id={config.uuid}
          value={value}
          filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
          onSelect={this.selectChange}
          onChange={(val) => val === undefined && this.selectChange('')}
          disabled={config.readonly}
        >
          {options.map(option =>
            <Select.Option id={option.key} title={option.label} key={option.key} value={option.value}>{option.label}</Select.Option>
          )}
        </Select>
      )
    } else {
      return (<Select
        showSearch
        id={config.uuid}
        mode="multiple"
        defaultValue={value}
        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
        onChange={this.mutilselectChange}
        disabled={config.readonly}
      >
        {options.map(option =>
          <Select.Option id={option.key} title={option.label} key={option.key} value={option.value}>{option.label}</Select.Option>
        )}
      </Select>)
    }
  }
}
 
export default MKSelect