2021-12-13 10:28:10 +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'
|
2022-02-25 16:57:08 +05:00
|
|
|
|
import { useState, useEffect, memo, useCallback } from 'react'
|
2022-01-24 21:16:50 +05:00
|
|
|
|
|
|
|
|
|
import { invokeWebApiWrapperAsync } from '@components/factory'
|
|
|
|
|
|
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-12-30 10:25:42 +05:00
|
|
|
|
const actions = [
|
|
|
|
|
[['insert'], (data) => [data]],
|
|
|
|
|
[['insertRange'], (data) => [[data].flat(1)]],
|
2022-01-24 21:16:50 +05:00
|
|
|
|
[['edit', 'update', 'put'], (data) => data.id && [data.id, data]],
|
2021-12-30 10:25:42 +05:00
|
|
|
|
[['delete'], (data) => data.id && [data.id]],
|
|
|
|
|
]
|
|
|
|
|
|
2022-02-07 17:44:46 +05:00
|
|
|
|
export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg, onComplete }, recordParser, actionName) => service && action && (
|
2021-12-13 10:28:10 +05:00
|
|
|
|
(record) => invokeWebApiWrapperAsync(
|
|
|
|
|
async () => {
|
2021-12-27 15:18:20 +05:00
|
|
|
|
const addIdWell = (...params) => idWell ? [idWell, ...params] : params
|
|
|
|
|
if (typeof recordParser === 'function')
|
|
|
|
|
record = recordParser(record)
|
|
|
|
|
|
2021-12-30 10:25:42 +05:00
|
|
|
|
const actionId = actions.findIndex((elm) => elm[0].includes(action))
|
|
|
|
|
const params = actions[actionId]?.[1](record)
|
|
|
|
|
|
|
|
|
|
if (params) await service[action](...addIdWell(...params))
|
|
|
|
|
await onComplete?.()
|
2021-12-13 10:28:10 +05:00
|
|
|
|
},
|
|
|
|
|
setLoader,
|
2022-02-07 17:44:46 +05:00
|
|
|
|
errorMsg,
|
|
|
|
|
actionName
|
2021-12-13 10:28:10 +05:00
|
|
|
|
)
|
|
|
|
|
)
|
2021-12-07 19:36:04 +05:00
|
|
|
|
|
2021-10-15 16:03:09 +05:00
|
|
|
|
export const tryAddKeys = (items) => {
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (!items?.length || !items[0])
|
2021-08-19 11:40:26 +05:00
|
|
|
|
return []
|
2021-12-13 10:28:10 +05:00
|
|
|
|
if (items[0].key)
|
2021-08-19 11:40:26 +05:00
|
|
|
|
return items
|
|
|
|
|
return items.map((item, index) => ({...item, key: item.key ?? item.id ?? index }))
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
export const EditableTable = memo(({
|
2021-11-29 16:49:22 +05:00
|
|
|
|
columns,
|
|
|
|
|
dataSource,
|
|
|
|
|
onChange, // Метод вызывается со всем dataSource с измененными элементами после любого действия
|
|
|
|
|
onRowAdd, // Метод вызывается с новой добавленной записью. Если метод не определен, то кнопка добавления строки не показывается
|
2021-12-15 11:01:18 +05:00
|
|
|
|
onRowEdit, // Метод вызывается с новой отредактированной записью. Если метод не поределен, то кнопка редактирования строки не показывается
|
|
|
|
|
onRowDelete, // Метод вызывается с удаленной записью. Если метод не поределен, то кнопка удаления строки не показывается
|
2021-12-27 18:06:26 +05:00
|
|
|
|
additionalButtons,
|
|
|
|
|
buttonsWidth,
|
2021-11-29 16:49:22 +05:00
|
|
|
|
...otherTableProps
|
|
|
|
|
}) => {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
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-12-13 10:28:10 +05:00
|
|
|
|
useEffect(() => {
|
2021-08-19 11:40:26 +05:00
|
|
|
|
setData(tryAddKeys(dataSource))
|
2021-12-13 10:28:10 +05:00
|
|
|
|
}, [dataSource])
|
2021-08-11 11:44:20 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const isEditing = useCallback((record) => record?.key === editingKey, [editingKey])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const edit = useCallback((record) => {
|
2021-07-30 16:14:07 +05:00
|
|
|
|
form.setFieldsValue({...record})
|
|
|
|
|
setEditingKey(record.key)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [form])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const cancel = useCallback(() => {
|
2021-12-13 10:28:10 +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('')
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [data, editingKey])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const addNewRow = useCallback(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-12-13 10:28:10 +05:00
|
|
|
|
|
2021-08-12 17:47:16 +05:00
|
|
|
|
const newData = [newRow, ...data]
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
edit(newRow)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [data, edit, form.initialValues])
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const save = useCallback(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-12-13 10:28:10 +05:00
|
|
|
|
if (item.key === newRowKeyValue)
|
2021-08-02 11:09:55 +05:00
|
|
|
|
item.key = newRowKeyValue + newData.length
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-09-30 18:00:46 +05:00
|
|
|
|
const isAdding = editingKey === newRowKeyValue
|
2021-07-30 16:14:07 +05:00
|
|
|
|
setEditingKey('')
|
2021-08-02 11:09:55 +05:00
|
|
|
|
setData(newData)
|
|
|
|
|
|
2021-09-30 18:00:46 +05:00
|
|
|
|
if (isAdding)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
2021-11-29 16:49:22 +05:00
|
|
|
|
onRowAdd(newItem)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onRowAdd fault:', err)
|
|
|
|
|
}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
else
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
2021-11-29 16:49:22 +05:00
|
|
|
|
onRowEdit(newItem)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onRowEdit fault:', err)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 10:28:10 +05:00
|
|
|
|
try {
|
2022-02-25 16:57:08 +05:00
|
|
|
|
onChange?.(newData)
|
2021-12-13 10:28:10 +05:00
|
|
|
|
} catch (err) {
|
2021-09-30 18:00:46 +05:00
|
|
|
|
console.log('callback onChange fault:', err)
|
|
|
|
|
}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
} catch (errInfo) {
|
|
|
|
|
console.log('Validate Failed:', errInfo)
|
|
|
|
|
}
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [data, editingKey, form, onChange, onRowAdd, onRowEdit])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const deleteRow = useCallback((record) => {
|
2021-08-02 11:09:55 +05:00
|
|
|
|
const newData = [...data]
|
|
|
|
|
const index = newData.findIndex((item) => record.key === item.key)
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
newData.splice(index, 1)
|
|
|
|
|
setData(newData)
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-08-02 11:09:55 +05:00
|
|
|
|
onRowDelete(record)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
onChange?.(newData)
|
|
|
|
|
}, [data, onChange, onRowDelete])
|
2021-08-02 11:09:55 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
const operationColumn = {
|
2021-12-27 18:06:26 +05:00
|
|
|
|
width: buttonsWidth ?? 82,
|
2021-12-13 10:28:10 +05:00
|
|
|
|
title: !!onRowAdd && (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={addNewRow}
|
|
|
|
|
disabled={editingKey !== ''}
|
|
|
|
|
icon={<PlusOutlined/>}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2021-07-30 16:14:07 +05:00
|
|
|
|
dataIndex: 'operation',
|
2021-12-13 10:28:10 +05:00
|
|
|
|
render: (_, record) => isEditing(record) ? (
|
|
|
|
|
<span>
|
|
|
|
|
<Button onClick={() => save(record)} icon={<SaveOutlined/>}/>
|
|
|
|
|
<Button onClick={cancel} icon={<CloseCircleOutlined/>}/>
|
2021-12-27 18:06:26 +05:00
|
|
|
|
{additionalButtons?.(record, editingKey)}
|
2021-12-13 10:28:10 +05:00
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span>
|
|
|
|
|
{onRowEdit && (
|
2021-11-29 16:49:22 +05:00
|
|
|
|
<Button
|
|
|
|
|
disabled={editingKey !== ''}
|
2021-08-02 11:09:55 +05:00
|
|
|
|
onClick={() => edit(record)}
|
2021-12-13 10:28:10 +05:00
|
|
|
|
icon={<EditOutlined/>}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{onRowDelete && (
|
|
|
|
|
<Popconfirm title={'Удалить?'} onConfirm={() => deleteRow(record)}>
|
2021-08-02 11:09:55 +05:00
|
|
|
|
<Button icon={<DeleteOutlined/>}/>
|
2021-12-13 10:28:10 +05:00
|
|
|
|
</Popconfirm>
|
|
|
|
|
)}
|
2021-12-27 18:06:26 +05:00
|
|
|
|
{additionalButtons?.(record, editingKey)}
|
2021-12-13 10:28:10 +05:00
|
|
|
|
</span>
|
|
|
|
|
),
|
2021-07-30 16:14:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 16:57:08 +05:00
|
|
|
|
const handleColumn = useCallback((col) => {
|
2021-07-30 16:14:07 +05:00
|
|
|
|
if (col.children)
|
|
|
|
|
col.children = col.children.map(handleColumn)
|
|
|
|
|
|
2021-11-29 16:49:22 +05:00
|
|
|
|
if (!col.editable)
|
|
|
|
|
return col
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...col,
|
|
|
|
|
onCell: (record) => ({
|
2021-11-29 16:49:22 +05:00
|
|
|
|
editing: isEditing(record),
|
|
|
|
|
record,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
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,
|
2022-02-25 16:57:08 +05:00
|
|
|
|
datatype: col.datatype,
|
2021-07-30 16:14:07 +05:00
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
|
}
|
2022-02-25 16:57:08 +05:00
|
|
|
|
}, [isEditing])
|
2021-07-30 16:14:07 +05:00
|
|
|
|
|
|
|
|
|
const mergedColumns = [...columns.map(handleColumn), operationColumn]
|
2021-11-29 16:49:22 +05:00
|
|
|
|
|
2021-07-30 16:14:07 +05:00
|
|
|
|
return (
|
2021-12-30 10:25:42 +05:00
|
|
|
|
<Form form={form}>
|
2021-07-30 16:14:07 +05:00
|
|
|
|
<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>
|
|
|
|
|
)
|
2022-02-25 16:57:08 +05:00
|
|
|
|
})
|