king
2024-06-01 4cc738e535d1a20701d206e12cf9de8cc5a01170
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
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)
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.config.timestamp && nextProps.config.timestamp !== this.state.config.timestamp) {
      this.setState({
        config: fromJS(nextProps.config).toJS(),
        options: fromJS(nextProps.config.options).toJS(),
      })
    }
  }
 
  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 || option.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') {
      if (config.extendName) {
        return (
          <Select
            showSearch
            allowClear
            value={value}
            dropdownMatchSelectWidth={config.dropdown !== 'false'}
            filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
              option.props.extend.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} title={option.label || option.text} extend={option[config.extendName] || ''} value={option.value || option.field || ''}>{option.label || option.text}</Select.Option>
            )}
          </Select>
        )
      } else {
        return (
          <Select
            showSearch
            allowClear
            value={value}
            dropdownMatchSelectWidth={config.dropdown !== 'false'}
            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, i) =>
          <Select.Option key={i} disabled={option.disabled} value={option.value || option.field}>{option.label || option.text}</Select.Option>
        )}
      </Select>)
    }
  }
}
 
export default MKSelect