king
2021-08-26 966ff7fb84181f0fa86a56569a8492453c3ae80a
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, Row, Col, Tooltip, Icon, Cascader, Input } from 'antd'
 
import asyncComponent from '@/utils/asyncComponent'
import MKEInput from './mkInput'
import MKNumberInput from './mkNumberInput'
import MKSelect from './mkSelect'
import './index.scss'
 
const { TextArea } = Input
 
const MKRadio = asyncComponent(() => import('./mkRadio'))
const StyleInput = asyncComponent(() => import('./styleInput'))
const MKFileUpload = asyncComponent(() => import('@/tabviews/zshare/fileupload'))
const MKColor = asyncComponent(() => import('@/tabviews/zshare/mutilform/mkColor'))
const MkIcon = asyncComponent(() => import('@/components/mkIcon'))
const SourceComponent = asyncComponent(() => import('@/menu/components/share/sourcecomponent'))
 
class ModalForm extends Component {
  static propTpyes = {
    formlist: PropTypes.array,   // 表单列表
    inputSubmit: PropTypes.func  // input回车提交
  }
 
  state = {
    formlist: [],    // 表单项
  }
 
  record = {}
 
  componentDidMount () {
    let record = {}
    let controlFields = {}
    let fieldMap = new Map()
 
    let formlist = this.props.formlist.filter(item => {
      if (item.controlFields) { // 多层表单控制
        controlFields[item.field] = item.controlFields
      }
      
      item.hidden = false
 
      if (item.forbid) {
        item.hidden = true
      }
      if (item.options) {
        item.oriOptions = fromJS(item.options).toJS()
      }
 
      if (item.type === 'text') {
        let _rules = [{
          required: item.required,
          message: item.label + '不可为空!'
        }]
        
        item.rules = _rules
      } else if (item.type === 'number') {
        item.rules = [{
          required: item.required,
          message: item.label + '不可为空!'
        }, {
          validator: (rule, value, callback) => this.handleConfirmPassword(rule, value, callback, item)
        }]
      } else if (item.type === 'textarea') {
        let _rules = [
          {
            required: item.required,
            message: item.label + '不可为空!'
          }
        ]
        item.rules = _rules
      } else {
        item.rules = [
          {
            required: item.required,
            message: '请选择' + item.label + '!'
          }
        ]
      }
 
      record[item.field] = item.initval
 
      fieldMap.set(item.field, item)
 
      return true
    })
 
    Object.keys(controlFields).forEach(key => {
      if (!fieldMap.has(key)) return
 
      let supItem = fieldMap.get(key)
      let fields = []
      
      controlFields[key].forEach(item => {
        if (!fieldMap.has(item.field)) return
 
        let cell = fieldMap.get(item.field)
 
        if (cell.hidden) return
 
        if (supItem.hidden || !item.values.includes(supItem.initval)) {
          cell.hidden = true
          fieldMap.set(item.field, cell)
        }
 
        fields.push(item)
      })
 
      supItem.controlFields = fields
      
      fieldMap.set(key, supItem)
    })
 
    formlist = formlist.map(cell => {
      let item = fieldMap.get(cell.field)
 
      if (item.linkField) {
        let supInitVal = fieldMap.get(item.linkField).initval || ''
        
        item.options = item.oriOptions.filter(option => option.ParentID === supInitVal)
      }
 
      return item
    })
 
    this.record = record
 
    this.setState({ formlist })
  }
 
  handleConfirmPassword = (rule, value, callback, item) => {
    let val = parseFloat(value)
 
    if (!isNaN(val)) {
      if (typeof(item.min) === 'number' && val < item.min) {
        callback(item.label + '最小值为 ' + item.min)
      } else if (typeof(item.max) === 'number' && val > item.max) {
        callback(item.label + '最大值为 ' + item.max)
      }
    }
    callback()
  }
 
  recordChange = (values, item) => {
    this.record = {...this.record, ...values}
 
    if (item && item.controlFields) {
      let map = new Map()
      this.state.formlist.forEach(cell => {
        if (!cell.field) return
        map.set(cell.field, cell)
      })
 
      let reset = (current) => {
        let val = this.record[current.field]
 
        current.controlFields.forEach(cell => {
          let m = map.get(cell.field)
          m.hidden = current.hidden || !cell.values.includes(val)
 
          if (m.hidden) {
            m.initval = this.record[m.field]
          }
 
          map.set(cell.field, m)
 
          if (m.controlFields) {
            reset(m)
          }
        })
      }
 
      reset(item)
 
      this.setState({
        formlist: this.state.formlist.map(cell => {
          if (cell.field) {
            return map.get(cell.field)
          }
          return cell
        })
      })
    }
  }
 
  getFields() {
    const { getFieldDecorator } = this.props.form
    const { formlist } = this.state
 
    const fields = []
 
    formlist.forEach((item, index) => {
      if (item.hidden || item.forbid) return
 
      let content = null
      let label = item.tooltip ? <Tooltip placement="topLeft" title={item.tooltip}><Icon type="question-circle" />{item.label}</Tooltip> : item.label
    
      if (item.type === 'text') {
        content = (<MKEInput config={item} onChange={(val, defer) => !defer && this.recordChange({[item.field]: val})} onSubmit={this.props.inputSubmit} />)
      } else if (item.type === 'number') {
        content = (<MKNumberInput config={item} onChange={(val, defer) => !defer && this.recordChange({[item.field]: val})} onSubmit={this.props.inputSubmit} />)
      } else if (item.type === 'select' || item.type === 'multiselect') {
        content = (<MKSelect config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)} />)
      } else if (item.type === 'color') {
        content = (<MKColor config={item} onChange={(val) => this.recordChange({[item.field]: val})}/>)
      } else if (item.type === 'styleInput') {
        content = (<StyleInput config={item} onChange={(val) => this.recordChange({[item.field]: val})}/>)
      } else if (item.type === 'radio') {
        content = (<MKRadio config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)}/>)
      } else if (item.type === 'fileupload') {
        content = (<MKFileUpload config={item} onChange={(val) => this.recordChange({[item.field]: val})} />)
      } else if (item.type === 'cascader') {
        content = (<Cascader options={item.options} expandTrigger="hover" placeholder="" />)
      } else if (item.type === 'textarea') {
        content = (<TextArea rows={item.rows || 2} placeholder=""/>)
      } else if (item.type === 'mkicon') {
        content = (<MkIcon allowClear={item.allowClear}/>)
      } else if (item.type === 'source') {
        content = (<SourceComponent type="" placement="right"/>)
      }
 
      if (!content) return
 
      fields.push(
        <Col span={item.span || 12} key={index}>
          <Form.Item label={label}>
            {getFieldDecorator(item.field, {
              initialValue: item.initval,
              rules: item.rules
            })(content)}
          </Form.Item>
        </Col>
      )
    })
    
    return fields
  }
 
  handleConfirm = () => {
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (err) {
          reject(err)
          return
        }
 
        resolve(values)
      })
    })
  }
 
  render() {
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      }
    }
 
    return (
      <Form {...formItemLayout} className="normal-form-field">
        <Row gutter={24}>{this.getFields()}</Row>
      </Form>
    )
  }
}
 
export default Form.create()(ModalForm)