king
2023-06-22 79e4981aa6cc9354276fc54cdf6d14eb08ab7fee
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { notification } from 'antd'
import { PlusOutlined } from '@ant-design/icons'
 
import asyncComponent from '@/utils/asyncComponent'
import Utils from '@/utils/utils.js'
import './index.scss'
 
const EditTable = asyncComponent(() => import('@/templates/zshare/editTable'))
 
class EdiFieldsTable extends Component {
  static propTpyes = {
    onChange: PropTypes.func        // 数据变化
  }
 
  UNSAFE_componentWillMount () {
    let data = this.props['data-__meta'].initialValue || []
 
    this.setState({
      loading: false,
      data: data.map(item => {
        item.uuid = item.uuid || item.key
        return item
      })
    })
  }
 
  state = {
    data: [],
    columns: [
      {
        title: '字段名',
        dataIndex: 'field',
        inputType: 'input',
        editable: true,
        width: '20%',
      },
      {
        title: '字体颜色',
        dataIndex: 'color',
        inputType: 'color',
        editable: true,
        width: '20%',
        render: (text, record) => {
          return <span style={{color: text}}>示例</span>
        }
      },
      {
        title: '字体大小',
        dataIndex: 'fontSize',
        inputType: 'number',
        min: 12,
        max: 50,
        editable: true,
        width: '20%',
      },
      {
        title: '对齐方式',
        dataIndex: 'align',
        inputType: 'select',
        editable: true,
        width: '20%',
        options: [
          {value: 'left', text: '居左'},
          {value: 'center', text: '居中'},
          {value: 'right', text: '居右'},
          // {value: 'justify', text: 'justify'}
        ],
        render: (text, record) => {
          if (text === 'center') {
            return '居中'
          } else if (text === 'right') {
            return '居右'
          } else {
            return '居左'
          }
        }
      }
    ]
  }
 
  handleAdd = () => {
    let _index = this.state.data.length + 1
    let item = {
      key: Utils.getuuid(),
      field: `field${_index}`,
      color: 'rgba(0, 0, 0, 0.85)',
      align: 'left',
      fontSize: 14,
    }
 
    item.uuid = item.key
 
    while (this.state.data.filter(cell => cell.field === item.field).length > 0) {
      _index++
      item.field = `field${_index}`
    }
 
    let data = [...this.state.data, item]
 
    this.setState({ data }, () => {
      this.props.onChange(data)
    })
  }
 
  changeData = (data) => {
    let fields = data.map(cell => cell.field)
    fields = Array.from(new Set(fields))
    if (data.length > 1 && data.length > fields.length) {
      notification.warning({
        top: 92,
        message: '字段名不可重复!',
        duration: 5
      })
      this.setState({loading: true}, () => {
        this.setState({loading: false})
      })
      return
    } else if (fields.filter(f => f.toLowerCase() === 'value').length > 0) {
      notification.warning({
        top: 92,
        message: '字段名不可使用value!',
        duration: 5
      })
      this.setState({loading: true}, () => {
        this.setState({loading: false})
      })
      return
    } else if (fields.filter(f => f.toLowerCase() === 'parentid').length > 0) {
      notification.warning({
        top: 92,
        message: '字段名不可使用parentid!',
        duration: 5
      })
      this.setState({loading: true}, () => {
        this.setState({loading: false})
      })
      return
    } else if (fields.filter(f => f.toLowerCase() === 'pid').length > 0) {
      notification.warning({
        top: 92,
        message: '字段名不可使用pid!',
        duration: 5
      })
      this.setState({loading: true}, () => {
        this.setState({loading: false})
      })
      return
    }
 
    this.setState({ data }, () => {
      this.props.onChange(data)
    })
  }
 
  render() {
    const { data, columns, loading } = this.state
 
    return (
      <div className="modal-card-field-table">
        {data.length < 6 ? <PlusOutlined className="add-row" onClick={this.handleAdd} /> : null}
        {!loading ? <EditTable indexShow={false} actions={['edit', 'move', 'del']} data={data} columns={columns} onChange={this.changeData}/> : null}
      </div>
    )
  }
}
 
export default EdiFieldsTable