From b69b5f6329ca5f87932436b7a6c1ddfc3377e10f Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期四, 16 五月 2024 10:56:41 +0800
Subject: [PATCH] 2024-05-16

---
 src/menu/components/table/normal-table/columns/editColumn/index.jsx |  373 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 260 insertions(+), 113 deletions(-)

diff --git a/src/menu/components/table/normal-table/columns/editColumn/index.jsx b/src/menu/components/table/normal-table/columns/editColumn/index.jsx
index 5ab0873..5f3d09c 100644
--- a/src/menu/components/table/normal-table/columns/editColumn/index.jsx
+++ b/src/menu/components/table/normal-table/columns/editColumn/index.jsx
@@ -1,27 +1,35 @@
 import React, {Component} from 'react'
 import PropTypes from 'prop-types'
-import { fromJS } from 'immutable'
-import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Icon, Cascader, Modal } from 'antd'
+import { is, fromJS } from 'immutable'
+import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Cascader, Modal, Checkbox, Popover } from 'antd'
+import { QuestionCircleOutlined } from '@ant-design/icons'
 
 import { getColumnForm } from './formconfig'
 import { formRule } from '@/utils/option.js'
 import './index.scss'
 
+const { TextArea } = Input
 const columnTypeOptions = {
-  text: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'textFormat', 'fieldlength', 'blacklist', 'perspective', 'rowspan'],
-  number: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'decimal', 'format', 'prefix', 'postfix', 'blacklist', 'perspective', 'sum'],
-  link: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'joint', 'Width', 'fieldlength', 'blacklist'],
-  textarea: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'fieldlength', 'blacklist'],
-  picture: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'fieldlength', 'blacklist', 'scale', 'maxHeight']
+  text: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'textFormat', 'blacklist', 'perspective', 'rowspan'],
+  number: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'decimal', 'format', 'prefix', 'postfix', 'blacklist', 'perspective', 'sum', 'rowspan'],
+  link: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'blacklist', 'nameField'],
+  textarea: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'blacklist'],
+  picture: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'blacklist', 'scale', 'lenWidRadio', 'backgroundSize', 'span', 'lostTip'],
+  video: ['label', 'field', 'type', 'Align', 'Hide', 'startTime', 'Width', 'blacklist', 'aspectRatio'],
+  colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
+  custom: ['label', 'type', 'Align', 'Width', 'blacklist', 'IsSort'],
+  action: ['label', 'type', 'Align', 'Width'],
+  formula: ['label', 'type', 'Align', 'Hide', 'Width', 'prefix', 'postfix', 'eval', 'formula', 'blacklist'],
+  extend: ['label', 'field', 'type', 'Align', 'Width', 'colUnit', 'shift', 'quota', 'supField'],
+  index: ['label', 'type', 'Align', 'Width']
 }
 
-class MainSearch extends Component {
+class NormalTableColumn extends Component {
   static propTpyes = {
-    dict: PropTypes.object,     // 瀛楀吀椤�
     column: PropTypes.object,
     fields: PropTypes.array,
-    updateCol: PropTypes.func,  // 鎻愪氦浜嬩欢
-    deleteCol: PropTypes.func   // 鍙栨秷鏃跺垹闄や簨浠�
+    submitCol: PropTypes.func,  // 鎻愪氦浜嬩欢
+    cancelCol: PropTypes.func   // 鍙栨秷鏃跺垹闄や簨浠�
   }
 
   state = {
@@ -29,89 +37,168 @@
     formlist: null
   }
 
-  /**
-   * @description 鍒濇娣诲姞鐨勬樉绀哄垪鍏冪礌锛岃仛鐒︽彁绀烘枃瀛�
-   */
-  componentDidMount () {
-    if (this.props.column.focus) {
-      this.editColumn()
+  record = null
+
+  UNSAFE_componentWillReceiveProps (nextProps) {
+    if (nextProps.column && !is(fromJS(this.props.column), fromJS(nextProps.column))) {
+      this.editColumn(nextProps.column)
     }
   }
 
-  editColumn = () => {
-    let menulist = sessionStorage.getItem('fstMenuList')
-    if (menulist) {
-      try {
-        menulist = JSON.parse(menulist)
-      } catch {
-        menulist = []
-      }
-    } else {
-      menulist = []
-    }
+  editColumn = (column) => {
+    let fields = fromJS(this.props.fields).toJS().map(item => {
+      item.text = `${item.field}锛�${item.label}锛塦
+      return item
+    })
+    
+    let formlist = getColumnForm(column, fields)
+    this.record = {}
+
+    formlist.forEach(item => {
+      this.record[item.key] = item.initVal
+    })
+    
+    let _options = this.getOptions()
 
     this.setState({
       visible: true,
-      formlist: getColumnForm(this.props.column, menulist, this.props.fields)
-    }, () => {
-      if (this.props.column.focus) {
+      formlist: formlist.map(item => {
+        item.hidden = !_options.includes(item.key)
+
+        if (item.key === 'formula') {
+          item.fields = this.props.fields.map(col => col.field)
+          item.fields = item.fields.join(', ')
+        }
+
+        return item
+      })
+    })
+    if (column.focus) {
+      setTimeout(() => {
         try {
           let _form = document.getElementById('label')
-          _form.select()
-        } catch {
+          _form && _form.select()
+        } catch (e) {
           console.warn('琛ㄥ崟focus澶辫触锛�')
         }
+      }, 200)
+    }
+  }
+
+  getOptions = () => {
+    let _options = fromJS(columnTypeOptions[this.record.type]).toJS()
+
+    if (this.record.type === 'text' || this.record.type === 'number') {
+      if (this.record.perspective === 'linkmenu') {
+        _options.push('linkmenu', 'open')
+      } else if (this.record.perspective === 'linkurl') {
+        _options.push('linkurl', 'open')
       }
-    })
+    } else if (this.record.type === 'formula' && this.record.eval === 'true') {
+      _options.push('decimal')
+    } else if (this.record.type === 'custom' && this.record.IsSort === 'true') {
+      _options.push('sortField')
+    } else if (this.record.type === 'extend') {
+      if (this.record.colUnit === 'day') {
+        _options.push('dayFormat')
+      } else {
+        _options.push('hourFormat')
+      }
+    }
+
+    if (this.record.Hide !== 'true') {
+      if (['number', 'formula'].includes(this.record.type)) {
+        _options.push('noValue')
+      } else if (this.record.type === 'text' && ['YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss'].includes(this.record.textFormat)) {
+        _options.push('noValue')
+      }
+    }
+
+    return _options
   }
 
   typeChange = (key, value, option) => {
+    this.record[key] = value
+
     if (key === 'type') {
-      let _options = fromJS(columnTypeOptions[value]).toJS()
-
-      if (value === 'text' || value === 'number') {
-        _options.push('linkmenu')
+      if (['link', 'textarea', 'picture', 'custom'].includes(value)) {
+        this.record.IsSort = 'false'
       }
+      
+      let _options = this.getOptions()
 
-      let fieldlength = 50
-
-      if (value !== 'text') {
-        fieldlength = 512
+      let _field = ''
+      if (value === 'formula') {
+        _field = this.props.form.getFieldValue('field') || ''
       }
 
       this.setState({
-        type: value,
         formlist: this.state.formlist.map(item => {
+          if (item.key === 'decimal' && value === 'formula') {
+            this.record.decimal = ''
+          }
+
+          item.initVal = this.record[item.key]
           item.hidden = !_options.includes(item.key)
 
           return item
         })
       }, () => {
-        if (this.props.form.getFieldValue('fieldlength') !== undefined) {
-          this.props.form.setFieldsValue({fieldlength: fieldlength})
-        } else if (this.props.form.getFieldValue('perspective') !== undefined) {
-          this.props.form.setFieldsValue({perspective: 'linkmenu'})
+        if (['link', 'textarea', 'picture', 'custom'].includes(value)) {
+          this.props.form.setFieldsValue({IsSort: 'false'})
+        } else if (value === 'text' || value === 'number') {
+          this.props.form.setFieldsValue({perspective: ''})
+        } else if (value === 'colspan') {
+          this.props.form.setFieldsValue({Align: 'center'})
+        } else if (value === 'formula' && _field) {
+          this.props.form.setFieldsValue({formula: '@' + _field + '@'})
+        } else if (value === 'index') {
+          this.props.form.setFieldsValue({label: '搴忓彿'})
         }
       })
     } else if (key === 'field') {
-      this.props.form.setFieldsValue({label: option.props.children})
+      let values = {label: option.props.label || option.props.children}
+      if (/Decimal|int/ig.test(option.props.datatype)) {
+        let decimal = 0
+        if (/Decimal/ig.test(option.props.datatype)) {
+          decimal = +option.props.datatype.replace(/Decimal\(18,/ig, '').replace(')', '')
+        }
+        values.type = 'number'
+        values.decimal = decimal
+      } else {
+        values.type = 'text'
+      }
+
+      let _type = this.record.type
+      this.record.type = values.type
+
+      if (values.type !== _type) {
+        values.perspective = ''
+        this.record.perspective = ''
+
+        let _options = this.getOptions()
+
+        this.setState({
+          formlist: this.state.formlist.map(item => {
+            item.initVal = this.record[item.key]
+            item.hidden = !_options.includes(item.key)
+
+            return item
+          })
+        }, () => {
+          this.props.form.setFieldsValue(values)
+        })
+      } else {
+        this.props.form.setFieldsValue(values)
+      }
     } else if (key === 'format' && value === 'percent') {
       this.props.form.setFieldsValue({postfix: '%'})
-    }
-  }
-
-  changeRadio = (key, value) => {
-    if (key === 'perspective') {
-      let _options = fromJS(columnTypeOptions[this.state.type]).toJS()
-
-      if (value !== 'linkurl') {
-        _options.push('linkmenu')
-      } else {
-        _options.push('linkurl')
-      }
+    } else if (['perspective', 'eval', 'IsSort', 'textFormat', 'colUnit'].includes(key)) {
+      let _options = this.getOptions()
 
       this.setState({
         formlist: this.state.formlist.map(item => {
+          item.initVal = this.record[item.key]
           item.hidden = !_options.includes(item.key)
 
           return item
@@ -128,30 +215,34 @@
     if (!formlist) return null
 
     formlist.forEach((item, index) => {
-      if (item.hidden) return
+      if (item.hidden || item.forbid) return
 
-      if (item.type === 'text') { // 鏂囨湰鎼滅储
-        let rules = []
+      let label = item.label
+      if (item.tooltip) {
+        if (item.toolWidth) {
+          label = <Tooltip placement="topLeft" overlayStyle={{maxWidth: item.toolWidth}} title={<div onClick={(e) => e.stopPropagation()}>{item.tooltip}</div>}><QuestionCircleOutlined className="mk-form-tip" />{item.label}</Tooltip>
+        } else {
+          label = <Tooltip placement="topLeft" title={<div onClick={(e) => e.stopPropagation()}>{item.tooltip}</div>}><QuestionCircleOutlined className="mk-form-tip" />{item.label}</Tooltip>
+        }
+      }
+
+      if (item.type === 'text') {
+        let rules = item.rules || []
         if (item.key !== 'linkurl') {
-          rules = [{
+          rules.push({
             max: formRule.input.max,
             message: formRule.input.message
-          }]
+          })
         }
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.tooltip ?
-              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
-                <Icon type="question-circle" />
-                {item.label}
-              </Tooltip> : item.label
-            }>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
                 initialValue: item.initVal || '',
                 rules: [
                   {
                     required: !!item.required,
-                    message: this.props.dict['form.required.input'] + item.label + '!'
+                    message: '璇疯緭鍏�' + item.label + '!'
                   },
                   ...rules
                 ]
@@ -162,35 +253,30 @@
       } else if (item.type === 'number') {
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.tooltip ?
-              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
-                <Icon type="question-circle" />
-                {item.label}
-              </Tooltip> : item.label
-            }>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
                 initialValue: item.initVal,
                 rules: [
                   {
                     required: !!item.required,
-                    message: this.props.dict['form.required.input'] + item.label + '!'
+                    message: '璇疯緭鍏�' + item.label + '!'
                   }
                 ]
-              })(item.unlimit ? <InputNumber /> :
-                  <InputNumber min={item.min} max={item.max} precision={item.decimal} />)}
+              })(item.unlimit ? <InputNumber onPressEnter={this.handleSubmit}/> :
+                  <InputNumber min={item.min} max={item.max} precision={item.decimal} onPressEnter={this.handleSubmit}/>)}
             </Form.Item>
           </Col>
         )
-      } else if (item.type === 'select') { // 涓嬫媺鎼滅储
+      } else if (item.type === 'select') {
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.label}>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
                 initialValue: item.initVal || '',
                 rules: [
                   {
                     required: !!item.required,
-                    message: this.props.dict['form.required.select'] + item.label + '!'
+                    message: '璇烽�夋嫨' + item.label + '!'
                   }
                 ]
               })(
@@ -201,8 +287,8 @@
                   getPopupContainer={() => document.getElementById('columnwinter')}
                 >
                   {item.options.map((option, index) =>
-                    <Select.Option key={index} value={option.value || option.field}>
-                      {option.text || option.label}
+                    <Select.Option key={index} datatype={option.datatype || ''} label={option.label || ''} value={(option.value || option.field || option.MenuID)}>
+                      {(option.text || option.label || option.MenuName)}
                     </Select.Option>
                   )}
                 </Select>
@@ -213,30 +299,35 @@
       } else if (item.type === 'radio') {
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.tooltip ?
-              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
-                <Icon type="question-circle" />
-                {item.label}
-              </Tooltip> : item.label
-            }>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
                 initialValue: item.initVal,
                 rules: [
                   {
                     required: !!item.required,
-                    message: this.props.dict['form.required.select'] + item.label + '!'
+                    message: '璇烽�夋嫨' + item.label + '!'
                   }
                 ]
               })(
-                <Radio.Group onChange={(e) => {this.changeRadio(item.key, e.target.value)}}>
-                  {
-                    item.options.map(option => {
-                      return (
-                        <Radio key={option.value} value={option.value}>{option.text}</Radio>
-                      )
-                    })
-                  }
+                <Radio.Group onChange={(e) => {this.typeChange(item.key, e.target.value)}}>
+                  {item.options.map(option => {
+                    return (<Radio key={option.value} value={option.value}>{option.text}</Radio>)
+                  })}
                 </Radio.Group>
+              )}
+            </Form.Item>
+          </Col>
+        )
+      } else if (item.type === 'checkbox') {
+        fields.push(
+          <Col span={12} key={index}>
+            <Form.Item label={label}>
+              {getFieldDecorator(item.key, {
+                initialValue: item.initVal
+              })(
+                <Checkbox.Group>
+                  {item.options.map(option => <Checkbox key={option.value} value={option.value}>{option.text}</Checkbox>)}
+                </Checkbox.Group>
               )}
             </Form.Item>
           </Col>
@@ -244,7 +335,7 @@
       } else if (item.type === 'multiselect') { // 澶氶��
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.label}>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
                 initialValue: item.initVal || []
               })(
@@ -254,19 +345,25 @@
                   filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                 >
                   {item.options.map((option, i) =>
-                    <Select.Option id={i} key={i} value={option.value}>{option.text}</Select.Option>
+                    <Select.Option id={i} key={i} value={option.value || option.field}>{option.text || option.label}</Select.Option>
                   )}
                 </Select>
               )}
             </Form.Item>
           </Col>
         )
-      } else if (item.type === 'cascader') { // 澶氶��
+      } else if (item.type === 'cascader') {
         fields.push(
           <Col span={12} key={index}>
-            <Form.Item label={item.label}>
+            <Form.Item label={label}>
               {getFieldDecorator(item.key, {
-                initialValue: item.initVal || []
+                initialValue: item.initVal || [],
+                rules: [
+                  {
+                    required: !!item.required,
+                    message: '璇烽�夋嫨' + item.label + '!'
+                  }
+                ]
               })(
                 <Cascader
                   options={item.options}
@@ -277,17 +374,68 @@
             </Form.Item>
           </Col>
         )
+      } else if (item.type === 'textarea') {
+        if (item.key === 'formula') {
+          fields.push(
+            <Col span={24} className="textarea" key={index}>
+              <Form.Item label={label}>
+                {getFieldDecorator(item.key, {
+                  initialValue: item.initVal || '',
+                  rules: [
+                    {
+                      required: !!item.required,
+                      message: '璇疯緭鍏�' + item.label + '!'
+                    }
+                  ]
+                })(<TextArea autoSize={{minRows: 2}} disabled={item.readonly} placeholder={item.placeholder || ''} />)}
+              </Form.Item>
+              <Popover overlayClassName="formula-fields" placement="topLeft" title="" content={<div>{item.fields}</div>} trigger="click">
+                <span className="formula-icon">瀛楁闆�</span>
+              </Popover>
+            </Col>
+          )
+        } else {
+          fields.push(
+            <Col span={24} key={index} className="textarea">
+              <Form.Item label={label}>
+                {getFieldDecorator(item.key, {
+                  initialValue: item.initVal || '',
+                  rules: [
+                    {
+                      required: !!item.required,
+                      message: '璇疯緭鍏�' + item.label + '!'
+                    }
+                  ]
+                })(<TextArea rows={2} disabled={item.readonly} placeholder={item.placeholder || ''}/>)}
+              </Form.Item>
+            </Col>
+          )
+        }
       }
     })
     return fields
   }
 
   handleSubmit = () => {
+    const { fields } = this.props
     // 琛ㄥ崟鎻愪氦鏃舵鏌ヨ緭鍏ュ�兼槸鍚︽纭�
     this.props.form.validateFieldsAndScroll((err, values) => {
       if (!err) {
+        // eslint-disable-next-line
+        if (values.type === 'formula' && values.eval === 'true' && /^[\u4E00-\u9FA50-9a-zA-Z_\s@\+\-\*\/]*$/ig.test(values.formula) && /[\+\-\*\/]/ig.test(values.formula)) {
+          let cols = []
+          fields.forEach(col => {
+            if (/^(Int|Decimal)/ig.test(col.datatype)) {
+              cols.push({reg: new RegExp('@' + col.field + '@', 'ig'), value: `(@${col.field}@)`})
+            }
+          })
+
+          cols.forEach(col => {
+            values.formula = values.formula.replace(col.reg, col.value)
+          })
+        }
         this.setState({visible: false, formlist: null})
-        this.props.updateCol(values)
+        this.props.submitCol(values)
       }
     })
   }
@@ -295,9 +443,7 @@
   editModalCancel = () => {
     this.setState({visible: false, formlist: null})
 
-    if (this.props.column.focus) {
-      this.props.deleteCol()
-    }
+    this.props.cancelCol()
   }
 
   render() {
@@ -312,13 +458,14 @@
         sm: { span: 18 }
       }
     }
+
     return (
       <div style={{display: 'inline-block'}}>
-        <Icon className="edit" title="缂栬緫" type="edit" onClick={this.editColumn} />
         <Modal
           title="鏄剧ず鍒楃紪杈�"
+          wrapClassName="mk-scroll-modal"
           visible={visible}
-          width={800}
+          width={850}
           maskClosable={false}
           onOk={this.handleSubmit}
           onCancel={this.editModalCancel}
@@ -333,4 +480,4 @@
   }
 }
 
-export default Form.create()(MainSearch)
\ No newline at end of file
+export default Form.create()(NormalTableColumn)
\ No newline at end of file

--
Gitblit v1.8.0