import React, { Component } from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Table, Input, InputNumber, Popconfirm, Form, Icon, Select } from 'antd'
|
|
import ColorSketch from '@/mob/colorsketch'
|
// import Utils from '@/utils/utils.js'
|
import zhCN from '@/locales/zh-CN/model.js'
|
import enUS from '@/locales/en-US/model.js'
|
import './index.scss'
|
|
const EditableContext = React.createContext()
|
|
class EditableCell extends Component {
|
getInput = (form) => {
|
const { inputType, options } = this.props
|
|
if (inputType === 'number') {
|
return <InputNumber min={12} max={50} precision={0} onPressEnter={() => this.getValue(form)} />
|
} else if (inputType === 'color') {
|
return <ColorSketch />
|
} else if (inputType === 'select') {
|
return <Select>
|
{options.map((item, i) => (<Select.Option key={i} value={item.field || item.value}> {item.label || item.text} </Select.Option>))}
|
</Select>
|
} else {
|
return <Input onPressEnter={() => this.getValue(form)} />
|
}
|
}
|
|
getValue = (form) => {
|
const { record } = this.props
|
form.validateFields((error, row) => {
|
if (error) {
|
return
|
}
|
this.props.onSave({...record, ...row})
|
})
|
}
|
|
renderCell = (form) => {
|
const { getFieldDecorator } = form
|
const { editing, dataIndex, title, record, children, className, required } = this.props
|
|
return (
|
<td className={className}>
|
{editing ? (
|
<Form.Item style={{ margin: 0 }}>
|
{getFieldDecorator(dataIndex, {
|
rules: [
|
{
|
required: required,
|
message: `Please Input ${title}!`,
|
}
|
],
|
initialValue: record[dataIndex],
|
})(this.getInput(form))}
|
</Form.Item>
|
) : (
|
children
|
)}
|
</td>
|
)
|
}
|
|
render() {
|
return <EditableContext.Consumer>{this.renderCell}</EditableContext.Consumer>
|
}
|
}
|
|
class EditTable extends Component {
|
static propTpyes = {
|
onChange: PropTypes.func // 数据变化
|
}
|
|
state = {
|
data: [],
|
editingKey: '',
|
columns: []
|
}
|
|
UNSAFE_componentWillMount () {
|
const { data } = this.props
|
let columns = fromJS(this.props.columns).toJS()
|
let dict = localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS
|
|
columns.push({
|
title: dict['model.operation'],
|
dataIndex: 'operation',
|
width: '140px',
|
render: (text, record) => {
|
const { editingKey } = this.state
|
const editable = this.isEditing(record)
|
return editable ? (
|
<span>
|
<EditableContext.Consumer>
|
{form => (
|
<span onClick={() => this.save(form, record.uuid)} style={{ marginRight: 8 , color: '#1890ff', cursor: 'pointer'}}>
|
{dict['model.save']}
|
</span>
|
)}
|
</EditableContext.Consumer>
|
<span style={{ color: '#1890ff', cursor: 'pointer'}} onClick={() => this.cancel(record.uuid)}>{dict['model.cancel']}</span>
|
</span>
|
) : (
|
<div className={'operation-btn' + (editingKey !== '' ? ' disabled' : '')}>
|
<span className="primary" onClick={() => {editingKey === '' && this.edit(record.uuid)}}><Icon type="edit" /></span>
|
<span className="primary" onClick={() => {editingKey === '' && this.handleUpDown(record.uuid, 'up')}}><Icon type="arrow-up" /></span>
|
<span className="danger" onClick={() => {editingKey === '' && this.handleUpDown(record.uuid, 'down')}}><Icon type="arrow-down" /></span>
|
{editingKey === '' ? <Popconfirm
|
overlayClassName="popover-confirm"
|
title={dict['model.query.delete']}
|
onConfirm={() => this.handleDelete(record.uuid)
|
}>
|
<span className="danger"><Icon type="delete" /></span>
|
</Popconfirm> : null}
|
{editingKey !== '' ? <span className="danger"><Icon type="delete" /></span> : null}
|
</div>
|
)
|
}
|
})
|
|
this.setState({
|
data: data || [],
|
columns,
|
dict
|
})
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
if (!is(fromJS(this.state.data), fromJS(nextProps.data))) {
|
this.setState({data: nextProps.data, editingKey: ''})
|
}
|
}
|
|
isEditing = record => record.uuid === this.state.editingKey
|
|
cancel = () => {
|
this.setState({ editingKey: '' })
|
}
|
|
onSave = (record) => {
|
const newData = [...this.state.data]
|
const index = newData.findIndex(item => record.uuid === item.uuid)
|
|
if (index === -1) return
|
|
newData.splice(index, 1, record)
|
this.setState({ data: newData, editingKey: '' }, () => {
|
this.props.onChange(newData)
|
})
|
}
|
|
handleDelete = (uuid) => {
|
const { data } = this.state
|
let _data = data.filter(item => uuid !== item.uuid)
|
|
this.setState({
|
data: _data
|
}, () => {
|
this.props.onChange(_data)
|
})
|
}
|
|
handleUpDown = (uuid, direction) => {
|
let _data = fromJS(this.state.data).toJS()
|
const index = _data.findIndex(item => uuid === item.uuid)
|
|
if ((index === 0 && direction === 'up') || (index === _data.length - 1 && direction === 'down')) {
|
return
|
}
|
|
if (direction === 'up') {
|
_data.splice(index - 1, 0, ..._data.splice(index, 1))
|
} else {
|
_data.splice(index + 1, 0, ..._data.splice(index, 1))
|
}
|
|
this.setState({
|
data: _data
|
}, () => {
|
this.props.onChange(_data)
|
})
|
}
|
|
save(form, uuid) {
|
form.validateFields((error, row) => {
|
if (error) {
|
return;
|
}
|
const newData = [...this.state.data]
|
const index = newData.findIndex(item => uuid === item.uuid)
|
if (index > -1) {
|
const item = newData[index]
|
newData.splice(index, 1, {
|
...item,
|
...row,
|
})
|
this.setState({ data: newData, editingKey: '' }, () => {
|
this.props.onChange(newData)
|
})
|
} else {
|
newData.push(row);
|
this.setState({ data: newData, editingKey: '' }, () => {
|
this.props.onChange(newData)
|
})
|
}
|
})
|
}
|
|
edit(uuid) {
|
this.setState({ editingKey: uuid })
|
}
|
|
render() {
|
const components = {
|
body: {
|
cell: EditableCell,
|
}
|
}
|
|
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 || [],
|
required: col.required !== false ? true : false,
|
title: col.title,
|
editing: this.isEditing(record),
|
onSave: this.onSave,
|
}),
|
}
|
})
|
|
return (
|
<EditableContext.Provider value={this.props.form}>
|
<div className="modal-edit-table">
|
<Table
|
bordered
|
rowKey="uuid"
|
components={components}
|
dataSource={this.state.data}
|
columns={columns}
|
rowClassName="editable-row"
|
pagination={false}
|
/>
|
</div>
|
</EditableContext.Provider>
|
)
|
}
|
}
|
|
export default Form.create()(EditTable)
|