king
2021-09-01 31ec63f0419895876cbaba99637a884a32d33d0d
src/menu/components/table/normal-table/columns/editColumn/index.jsx
@@ -1,27 +1,33 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Icon, Cascader, Modal } from 'antd'
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', 'joint', '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', 'picSort'],
  colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
  custom: ['label', 'type', 'Align', 'Hide', 'Width', 'blacklist'],
  action: ['label', 'type', 'Align', 'Width'],
  index: ['label', 'type', 'Align', 'Width']
}
class MainSearch extends Component {
  static propTpyes = {
    dict: PropTypes.object,     // 字典项
    visible: PropTypes.bool,
    column: PropTypes.object,
    fields: PropTypes.array,
    updateCol: PropTypes.func,  // 提交事件
    deleteCol: PropTypes.func   // 取消时删除事件
    submitCol: PropTypes.func,  // 提交事件
    cancelCol: PropTypes.func   // 取消时删除事件
  }
  state = {
@@ -29,36 +35,37 @@
    formlist: null
  }
  /**
   * @description 初次添加的显示列元素,聚焦提示文字
   */
  componentDidMount () {
    if (this.props.column.focus) {
      this.editColumn()
  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 = []
  editColumn = (column) => {
    let formlist = getColumnForm(column, this.props.fields)
    let _options = fromJS(columnTypeOptions[column.type]).toJS()
    if (column.type === 'text' || column.type === 'number') {
      if (column.perspective === 'linkmenu') {
        _options.push('linkmenu', 'linkfields', 'open')
      } else if (column.perspective === 'linkurl') {
        _options.push('linkurl', 'linkfields', 'open')
      }
    } else {
      menulist = []
    }
    this.setState({
      visible: true,
      formlist: getColumnForm(this.props.column, menulist, this.props.fields)
      type: column.type,
      formlist: formlist.map(item => {
        item.hidden = !_options.includes(item.key)
        return item
      })
    }, () => {
      if (this.props.column.focus) {
      if (column.focus) {
        try {
          let _form = document.getElementById('label')
          _form.select()
        } catch {
          _form && _form.select()
        } catch (e) {
          console.warn('表单focus失败!')
        }
      }
@@ -69,16 +76,6 @@
    if (key === 'type') {
      let _options = fromJS(columnTypeOptions[value]).toJS()
      if (value === 'text' || value === 'number') {
        _options.push('linkmenu')
      }
      let fieldlength = 50
      if (value !== 'text') {
        fieldlength = 512
      }
      this.setState({
        type: value,
        formlist: this.state.formlist.map(item => {
@@ -87,14 +84,44 @@
          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 (value === 'link' || value === 'textarea' || value === 'picture') {
          this.props.form.setFieldsValue({IsSort: 'false'})
        } else if (value === 'text' || value === 'number') {
          this.props.form.setFieldsValue({perspective: ''})
        } else if (value === 'action' || value === 'colspan') {
          this.props.form.setFieldsValue({Align: 'center'})
        }
      })
    } else if (key === 'field') {
      this.props.form.setFieldsValue({label: option.props.children})
      let values = {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 if (/nvarchar/ig.test(option.props.datatype)) {
        values.type = 'text'
      }
      if (values.type !== this.state.type) {
        values.perspective = ''
        let _options = fromJS(columnTypeOptions[values.type]).toJS()
        this.setState({
          type: values.type,
          formlist: this.state.formlist.map(item => {
            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: '%'})
    }
@@ -104,10 +131,10 @@
    if (key === 'perspective') {
      let _options = fromJS(columnTypeOptions[this.state.type]).toJS()
      if (value !== 'linkurl') {
        _options.push('linkmenu')
      } else {
        _options.push('linkurl')
      if (value === 'linkmenu') {
        _options.push('linkmenu', 'linkfields', 'open')
      } else if (value === 'linkurl') {
        _options.push('linkurl', 'linkfields', 'open')
      }
      this.setState({
@@ -128,7 +155,7 @@
    if (!formlist) return null
    formlist.forEach((item, index) => {
      if (item.hidden) return
      if (item.hidden || item.forbidden) return
      if (item.type === 'text') { // 文本搜索
        let rules = []
@@ -141,7 +168,7 @@
        fields.push(
          <Col span={12} key={index}>
            <Form.Item label={item.tooltip ?
              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
              <Tooltip placement="topLeft" title={item.tooltip}>
                <Icon type="question-circle" />
                {item.label}
              </Tooltip> : item.label
@@ -163,7 +190,7 @@
        fields.push(
          <Col span={12} key={index}>
            <Form.Item label={item.tooltip ?
              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
              <Tooltip placement="topLeft" title={item.tooltip}>
                <Icon type="question-circle" />
                {item.label}
              </Tooltip> : item.label
@@ -176,12 +203,12 @@
                    message: this.props.dict['form.required.input'] + 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}>
@@ -201,8 +228,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 || ''} value={(option.value || option.field || option.MenuID)}>
                      {(option.text || option.label || option.MenuName)}
                    </Select.Option>
                  )}
                </Select>
@@ -214,7 +241,7 @@
        fields.push(
          <Col span={12} key={index}>
            <Form.Item label={item.tooltip ?
              <Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
              <Tooltip placement="topLeft" title={item.tooltip}>
                <Icon type="question-circle" />
                {item.label}
              </Tooltip> : item.label
@@ -254,7 +281,7 @@
                  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>
              )}
@@ -266,7 +293,13 @@
          <Col span={12} key={index}>
            <Form.Item label={item.label}>
              {getFieldDecorator(item.key, {
                initialValue: item.initVal || []
                initialValue: item.initVal || [],
                rules: [
                  {
                    required: !!item.required,
                    message: this.props.dict['form.required.select'] + item.label + '!'
                  }
                ]
              })(
                <Cascader
                  options={item.options}
@@ -274,6 +307,27 @@
                  getPopupContainer={() => document.getElementById('columnwinter')}
                />
              )}
            </Form.Item>
          </Col>
        )
      } else if (item.type === 'textarea') { // 文本搜索
        fields.push(
          <Col span={24} key={index} className="textarea">
            <Form.Item label={item.tooltip ?
              <Tooltip placement="topLeft" title={item.tooltip}>
                <Icon type="question-circle" />
                {item.label}
              </Tooltip> : item.label
            }>
              {getFieldDecorator(item.key, {
                initialValue: item.initVal || '',
                rules: [
                  {
                    required: !!item.required,
                    message: this.props.dict['form.required.input'] + item.label + '!'
                  }
                ]
              })(<TextArea rows={2} disabled={item.readonly} onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col>
        )
@@ -287,7 +341,7 @@
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        this.setState({visible: false, formlist: null})
        this.props.updateCol(values)
        this.props.submitCol(values)
      }
    })
  }
@@ -295,9 +349,7 @@
  editModalCancel = () => {
    this.setState({visible: false, formlist: null})
    if (this.props.column.focus) {
      this.props.deleteCol()
    }
    this.props.cancelCol()
  }
  render() {
@@ -312,9 +364,9 @@
        sm: { span: 18 }
      }
    }
    return (
      <div style={{display: 'inline-block'}}>
        <Icon className="edit" title="编辑" type="edit" onClick={this.editColumn} />
        <Modal
          title="显示列编辑"
          visible={visible}