king
2025-05-13 6085a8cd81e5baacd3bc34604717cecbfb30bd01
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Table, Form, Select, Cascader } from 'antd'
import { EditOutlined } from '@ant-design/icons'
 
import './index.scss'
 
const EditableContext = React.createContext()
const shortkeycode = {
  65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I', 74: 'J', 75: 'K', 76: 'L', 77: 'M',
  78: 'N', 79: 'O', 80: 'P', 81: 'Q', 82: 'R', 83: 'S', 84: 'T', 85: 'U', 86: 'V', 87: 'W', 88: 'X', 89: 'Y', 90: 'Z'
}
 
class CustomEditableCell extends Component {
  getInput = () => {
    const { inputType, options, record } = this.props
 
    if (inputType === 'select') {
      let _options = []
      if (record.$port) {
        _options = window.GLOB.UserCacheMap.get(record.$port) || []
      }
      return (
        <Select allowClear>
          {_options.map((item, i) => (<Select.Option key={i} title={item.value} value={item.value}> {item.text} </Select.Option>))}
        </Select>
      )
    } else if (inputType === 'cascader') {
      return (
        <Cascader allowClear options={options} placeholder=""/>
      )
    }
  }
 
  renderCell = (form) => {
    const { getFieldDecorator } = form
    const { editing, editable, dataIndex, record, children, className } = this.props
 
    return (
      <td className={className}>
        {editing && editable ? (
          <Form.Item style={{ margin: 0 }}>
            {getFieldDecorator(dataIndex, {
              initialValue: record[dataIndex] || '',
            })(this.getInput())}
          </Form.Item>
        ) : (
          children
        )}
      </td>
    )
  }
 
  render() {
    return <EditableContext.Consumer>{this.renderCell}</EditableContext.Consumer>
  }
}
 
class CustomEditTable extends Component {
  static propTpyes = {
    data: PropTypes.any,            // 数据列表
    onChange: PropTypes.func        // 数据变化
  }
 
  state = {
    data: [],
    editingKey: '',
    visible: false,
    columns: [{
      title: window.GLOB.dict['name'] || '名称',
      dataIndex: 'label',
      width: '25%'
    }, {
      title: window.GLOB.dict['shortcut'] || '快捷键',
      dataIndex: 'shortcut',
      inputType: 'cascader',
      editable: true,
      options: [],
      width: '25%',
      render: (text) => {
        if (!text) return ''
        return text[0] + '+' + shortkeycode[text[1]]
      }
    }, {
      title: window.GLOB.dict['printer'] || '打印机',
      dataIndex: 'printer',
      inputType: 'select',
      editable: true,
      options: [],
      width: '25%'
    }, {
      title: window.GLOB.dict['operation'] || '操作',
      dataIndex: 'operation',
      width: '140px',
      render: (_, record) => {
        const { editingKey } = this.state
        const editable = this.isEditing(record)
        return editable ? (
          <div style={{textAlign: 'center', minWidth: '110px'}}>
            <EditableContext.Consumer>
              {form => (
                <span onClick={() => this.save(form, record.uuid)} style={{ marginRight: 8 , color: '#1890ff', cursor: 'pointer'}}>
                  {window.GLOB.dict['save'] || '保存'}
                </span>
              )}
            </EditableContext.Consumer>
            <span style={{ color: '#1890ff', cursor: 'pointer'}} onClick={() => this.cancel(record.uuid)}>{window.GLOB.dict['cancel'] || '取消'}</span>
          </div>
        ) : (
          <div className={'edit-operation-btn' + (editingKey !== '' ? ' disabled' : '')} style={{minWidth: '110px'}}>
            <span className="primary" onClick={() => {editingKey === '' && this.edit(record.uuid)}}><EditOutlined /></span>
          </div>
        )
      }
    }],
    printTypeColumns: [
      {
        title: window.GLOB.dict['print_type'] || '打印类型',
        dataIndex: 'Text',
        width: '26.1%'
      },
      {
        title: window.GLOB.dict['printer'] || '打印机',
        dataIndex: 'printer',
        inputType: 'select',
        editable: true,
        options: [],
      },
      {
        title: window.GLOB.dict['operation'] || '操作',
        dataIndex: 'operation',
        width: '153px',
        render: (_, record) => {
          const { editingKey } = this.state
          const editable = this.isEditing(record)
          return editable ? (
            <div style={{textAlign: 'center', minWidth: '110px'}}>
              <EditableContext.Consumer>
                {form => (
                  <span onClick={() => this.save(form, record.uuid, record.parentId)} style={{ marginRight: 8 , color: '#1890ff', cursor: 'pointer'}}>
                    {window.GLOB.dict['save'] || '保存'}
                  </span>
                )}
              </EditableContext.Consumer>
              <span style={{ color: '#1890ff', cursor: 'pointer'}} onClick={() => this.cancel(record.uuid)}>{window.GLOB.dict['cancel'] || '取消'}</span>
            </div>
          ) : (
            <div className={'edit-operation-btn' + (editingKey !== '' ? ' disabled' : '')} style={{minWidth: '110px'}}>
              <span className="primary" onClick={() => {editingKey === '' && this.edit(record.uuid)}}><EditOutlined /></span>
            </div>
          )
        }
      }
    ]
  }
 
  UNSAFE_componentWillMount () {
    const { data } = this.props
 
    let keys = ['shift', 'ctrl', 'alt']
    let _options = []
 
    keys.forEach(item => {
      let _op = {
        value: item,
        label: item,
        children: []
      }
      Object.keys(shortkeycode).forEach(key => {
        if (item === 'ctrl' && ['65', '67', '68', '69', '70', '71', '72', '74', '75', '76', '78', '79', '80', '82', '83', '84', '85', '86', '87', '88', '90'].includes(key)) return
        if (item === 'alt' && ['65', '68', '69', '70'].includes(key)) return
 
        _op.children.push({
          value: +key,
          label: shortkeycode[key]
        })
      })
 
      _options.push(_op)
    })
 
    this.setState({
      data: data,
      columns: this.state.columns.map(item => {
        if (item.dataIndex === 'shortcut') {
          item.options = _options
        }
        return item
      })
    })
  }
 
  isEditing = record => record.uuid === this.state.editingKey
 
  cancel = () => {
    this.setState({ editingKey: '' })
  }
 
  handleDelete = (uuid) => {
    const { data } = this.state
    let _data = data.filter(item => uuid !== item.uuid)
 
    this.setState({
      data: _data
    }, () => {
      this.props.onChange(_data)
    })
  }
 
  save(form, uuid, parentId) {
    form.validateFields((error, row) => {
      if (error) {
        return;
      }
 
      let newData = null
 
      if (parentId) {
        newData = this.state.data.map(item => {
          if (parentId === item.uuid) {
            item.verify.printerTypeList = item.verify.printerTypeList.map(cell => {
              if (uuid === cell.uuid) {
                cell = {...cell, ...row}
              }
              return cell
            })
          }
          return item
        })
      } else {
        newData = this.state.data.map(item => {
          if (uuid === item.uuid) {
            item = {...item, ...row}
          }
          return item
        })
      }
      
      this.setState({ data: newData, editingKey: '' }, () => {
        this.props.onChange(newData)
      })
    })
  }
 
  edit(uuid) {
    this.setState({ editingKey: uuid })
  }
 
  render() {
    let components = {
      body: {
        cell: CustomEditableCell
      }
    }
    
    const columns = this.state.columns.map(col => {
      if (!col.editable) return col
      return {
        ...col,
        onCell: record => ({
          record,
          inputType: col.inputType,
          dataIndex: col.dataIndex,
          options: col.options || [],
          title: col.title,
          editable: col.dataIndex === 'shortcut' || record.funcType === 'print',
          editing: this.isEditing(record),
        }),
      }
    })
 
    const printTypeColumns = this.state.printTypeColumns.map(col => {
      if (!col.editable) return col
      return {
        ...col,
        onCell: record => ({
          record,
          inputType: col.inputType,
          dataIndex: col.dataIndex,
          options: [],
          title: col.title,
          editable: true,
          editing: this.isEditing(record),
        }),
      }
    })
 
    return (
      <EditableContext.Provider value={this.props.form}>
        <div className="modal-custom-edit-table">
          <Table
            bordered
            rowKey="uuid"
            components={components}
            dataSource={this.state.data}
            columns={columns}
            rowClassName={(record) => 'editable-row' + (record.$expanded ? ' print' : '')}
            pagination={false}
            expandedRowRender={record => (
              record.$expanded ?
                <Table
                  bordered
                  rowKey="key"
                  size="small"
                  components={components}
                  rowClassName="editable-row"
                  dataSource={record.verify.printerTypeList}
                  columns={printTypeColumns}
                  pagination={false}
                /> : null
            )}
          />
        </div>
      </EditableContext.Provider>
    )
  }
}
 
export default Form.create()(CustomEditTable)