2021-08-20 10:49:20 +05:00
|
|
|
|
import { Form, Table, Button, Popconfirm } from "antd"
|
2021-08-02 11:09:55 +05:00
|
|
|
|
import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons'
|
2021-08-11 11:44:20 +05:00
|
|
|
|
import { useState, useEffect } from "react";
|
2021-08-20 10:49:20 +05:00
|
|
|
|
import { EditableCell } from './EditableCell'
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newRowKeyValue = 'newRow'
|
|
|
|
|
|
2021-08-19 11:40:26 +05:00
|
|
|
|
const tryAddKeys = (items) => {
|
2021-08-30 11:25:24 +05:00
|
|
|
|
if(!items?.length || !items[0])
|
2021-08-19 11:40:26 +05:00
|
|
|
|
return []
|
|
|
|
|
if(items[0].key)
|
|
|
|
|
return items
|
|
|
|
|
return items.map((item, index) => ({...item, key: item.key ?? item.id ?? index }))
|
|
|
|
|
}
|
|
|
|
|
|
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-19 11:40:26 +05:00
|
|
|
|
const [data, setData] = useState(tryAddKeys(dataSource))
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const [editingKey, setEditingKey] = useState('')
|
|
|
|
|
|
2021-08-11 11:44:20 +05:00
|
|
|
|
useEffect(()=>{
|
2021-08-19 11:40:26 +05:00
|
|
|
|
setData(tryAddKeys(dataSource))
|
2021-08-11 11:44:20 +05:00
|
|
|
|
},[dataSource])
|
|
|
|
|
|
2021-08-30 15:11:21 +05:00
|
|
|
|
const isEditing = (record) => record?.key === editingKey
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
|
|
|
|
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-20 09:20:33 +05:00
|
|
|
|
width: 82,
|
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,
|
2021-08-19 11:40:26 +05:00
|
|
|
|
key: col.key ?? col.dataIndex,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
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]
|
2021-08-19 11:40:26 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
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>
|
|
|
|
|
)
|
|
|
|
|
}
|