2021-08-02 11:09:55 +05:00
|
|
|
|
import { Form, Input, Table, Button, Popconfirm } from "antd"
|
|
|
|
|
import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons'
|
2021-08-11 11:44:20 +05:00
|
|
|
|
import { useState, useEffect } from "react";
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newRowKeyValue = 'newRow'
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const EditableCell = ({
|
|
|
|
|
editing,
|
|
|
|
|
record,
|
|
|
|
|
dataIndex,
|
|
|
|
|
input,
|
|
|
|
|
isRequired,
|
|
|
|
|
title,
|
|
|
|
|
formItemClass,
|
|
|
|
|
formItemRules,
|
|
|
|
|
children,
|
2021-08-12 15:41:11 +05:00
|
|
|
|
initialValue,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
}) => {
|
|
|
|
|
|
|
|
|
|
const inputNode = input ?? <Input/>
|
|
|
|
|
const rules = formItemRules ?? [{
|
|
|
|
|
required: isRequired,
|
|
|
|
|
message: `Please Input ${title}!`,
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
const editor = <Form.Item
|
|
|
|
|
name={dataIndex}
|
|
|
|
|
style={{margin:0}}
|
|
|
|
|
className={formItemClass}
|
2021-08-12 15:41:11 +05:00
|
|
|
|
rules={rules}
|
|
|
|
|
initialValue={initialValue}>
|
2021-07-30 16:14:07 +05:00
|
|
|
|
{inputNode}
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
|
|
|
return (<td>
|
|
|
|
|
{editing ? editor: children}
|
|
|
|
|
</td>)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
export const EditableTable = ({
|
|
|
|
|
columns,
|
|
|
|
|
dataSource,
|
|
|
|
|
onChange, // Метод вызывается со всем dataSource с измененными элементами после любого действия
|
|
|
|
|
onRowAdd, // Метод вызывается с новой добавленной записью. Если метод не поределен, то кнопка добавления строки не показывается
|
|
|
|
|
onRowEdit,// Метод вызывается с новой отредактированной записью. Если метод не поределен, то кнопка редактирования строки не показывается
|
|
|
|
|
onRowDelete,// Метод вызывается с удаленной записью. Если метод не поределен, то кнопка удаления строки не показывается
|
|
|
|
|
...otherTableProps}) => {
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const [form] = Form.useForm()
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const [data, setData] = useState(dataSource?? [])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const [editingKey, setEditingKey] = useState('')
|
|
|
|
|
|
2021-08-11 11:44:20 +05:00
|
|
|
|
useEffect(()=>{
|
|
|
|
|
setData(dataSource??[])
|
|
|
|
|
},[dataSource])
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const isEditing = (record) => record.key === editingKey
|
|
|
|
|
|
|
|
|
|
const edit = (record) => {
|
|
|
|
|
form.setFieldsValue({...record})
|
|
|
|
|
setEditingKey(record.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cancel = () => {
|
2021-08-11 11:44:20 +05:00
|
|
|
|
if(editingKey === newRowKeyValue)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
{
|
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => newRowKeyValue === item.key)
|
|
|
|
|
newData.splice(index, 1)
|
|
|
|
|
setData(newData)
|
|
|
|
|
}
|
2021-07-30 16:14:07 +05:00
|
|
|
|
setEditingKey('')
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 15:41:11 +05:00
|
|
|
|
const addNewRow = async () => {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
let newRow = {
|
2021-08-12 15:41:11 +05:00
|
|
|
|
...form.initialValues,
|
2021-08-02 11:09:55 +05:00
|
|
|
|
key:newRowKeyValue
|
|
|
|
|
}
|
2021-08-12 17:47:16 +05:00
|
|
|
|
const newData = [newRow, ...data]
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
edit(newRow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const save = async (record) => {
|
2021-07-30 16:14:07 +05:00
|
|
|
|
try {
|
|
|
|
|
const row = await form.validateFields()
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => record.key === item.key)
|
2021-08-12 15:41:11 +05:00
|
|
|
|
const item = newData[index]
|
|
|
|
|
const newItem = { ...item, ...row }
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-12 15:41:11 +05:00
|
|
|
|
newData.splice(index, 1, newItem)
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
if(item.key === newRowKeyValue)
|
|
|
|
|
item.key = newRowKeyValue + newData.length
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
setEditingKey('')
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
|
|
|
|
|
if (editingKey === newRowKeyValue)
|
2021-08-12 15:41:11 +05:00
|
|
|
|
onRowAdd(newItem)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
else
|
2021-08-12 15:41:11 +05:00
|
|
|
|
onRowEdit(newItem)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
if(onChange)
|
|
|
|
|
onChange(newData)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
} catch (errInfo) {
|
|
|
|
|
console.log('Validate Failed:', errInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const deleteRow = (record) =>{
|
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => record.key === item.key)
|
|
|
|
|
|
|
|
|
|
newData.splice(index, 1)
|
|
|
|
|
setData(newData)
|
|
|
|
|
|
|
|
|
|
onRowDelete(record)
|
|
|
|
|
|
|
|
|
|
if(onChange)
|
|
|
|
|
onChange(newData)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const operationColumn = {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
title: (!!onRowAdd) && <Button
|
|
|
|
|
onClick={addNewRow}
|
|
|
|
|
disabled={editingKey !== ''}
|
|
|
|
|
icon={<PlusOutlined/>}
|
|
|
|
|
/>,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
dataIndex: 'operation',
|
|
|
|
|
render: (_, record) => {
|
|
|
|
|
const editable = isEditing(record)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
return editable
|
|
|
|
|
?(<span>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => save(record)}
|
|
|
|
|
icon={<SaveOutlined/>}/>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={cancel}
|
|
|
|
|
icon={<CloseCircleOutlined/>}/>
|
|
|
|
|
</span>)
|
|
|
|
|
:(<span>
|
2021-08-17 15:24:58 +05:00
|
|
|
|
{onRowEdit&&<Button
|
2021-08-02 11:09:55 +05:00
|
|
|
|
disabled={editingKey !== ''}
|
|
|
|
|
onClick={() => edit(record)}
|
2021-08-17 15:24:58 +05:00
|
|
|
|
icon={<EditOutlined/>}/>}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
{onRowDelete&&
|
|
|
|
|
<Popconfirm title="Удалить?" onConfirm={()=>deleteRow(record)}>
|
|
|
|
|
<Button icon={<DeleteOutlined/>}/>
|
|
|
|
|
</Popconfirm>}
|
|
|
|
|
</span>)
|
2021-07-30 16:14:07 +05:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleColumn = (col) => {
|
|
|
|
|
if (col.children)
|
|
|
|
|
col.children = col.children.map(handleColumn)
|
|
|
|
|
|
|
|
|
|
if (!col.editable)
|
|
|
|
|
return col
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...col,
|
|
|
|
|
onCell: (record) => ({
|
|
|
|
|
editing: isEditing(record),
|
|
|
|
|
record,
|
|
|
|
|
dataIndex: col.dataIndex ?? col.key,
|
|
|
|
|
input: col.input,
|
|
|
|
|
isRequired: col.isRequired,
|
|
|
|
|
title: col.title,
|
|
|
|
|
dataType: col.dataType,
|
|
|
|
|
formItemClass: col.formItemClass,
|
|
|
|
|
formItemRules: col.formItemRules,
|
2021-08-12 15:41:11 +05:00
|
|
|
|
initialValue: col.initialValue,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mergedColumns = [...columns.map(handleColumn), operationColumn]
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form form={form} component={false}>
|
|
|
|
|
<Table
|
|
|
|
|
components={{
|
|
|
|
|
body: {
|
|
|
|
|
cell: EditableCell,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
columns={mergedColumns}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
dataSource={data}
|
2021-07-30 16:14:07 +05:00
|
|
|
|
{...otherTableProps}
|
|
|
|
|
/>
|
|
|
|
|
</Form>
|
|
|
|
|
)
|
|
|
|
|
}
|