From 618edda299373887b987fdd2535a66c857923796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 2 Aug 2021 11:09:55 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=D0=BE=D0=B9=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8?= =?UTF-8?q?=D1=86=D1=8B.=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5,=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/EditableTable.jsx | 119 ++++++++++++++++++++++--------- src/pages/WellStat.jsx | 8 +++ 2 files changed, 92 insertions(+), 35 deletions(-) diff --git a/src/components/EditableTable.jsx b/src/components/EditableTable.jsx index 24b2ace..f0fee09 100644 --- a/src/components/EditableTable.jsx +++ b/src/components/EditableTable.jsx @@ -1,6 +1,9 @@ -import { Form, Input, Popconfirm, Typography, Table } from "antd" +import { Form, Input, Table, Button, Popconfirm } from "antd" +import { EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons' import { useState } from "react"; +const newRowKeyValue = 'newRow' + const EditableCell = ({ editing, record, @@ -32,8 +35,17 @@ const EditableCell = ({ ) } -export const EditableTable = ({columns, dataSource, onUpdateData: onChange, ...otherTableProps}) => { +export const EditableTable = ({ + columns, + dataSource, + onChange, // Метод вызывается со всем dataSource с измененными элементами после любого действия + onRowAdd, // Метод вызывается с новой добавленной записью. Если метод не поределен, то кнопка добавления строки не показывается + onRowEdit,// Метод вызывается с новой отредактированной записью. Если метод не поределен, то кнопка редактирования строки не показывается + onRowDelete,// Метод вызывается с удаленной записью. Если метод не поределен, то кнопка удаления строки не показывается + ...otherTableProps}) => { + const [form] = Form.useForm() + const [data, setData] = useState(dataSource?? []) const [editingKey, setEditingKey] = useState('') const isEditing = (record) => record.key === editingKey @@ -44,57 +56,94 @@ export const EditableTable = ({columns, dataSource, onUpdateData: onChange, ...o } const cancel = () => { + if(editingKey == newRowKeyValue) + { + const newData = [...data] + const index = newData.findIndex((item) => newRowKeyValue === item.key) + newData.splice(index, 1) + setData(newData) + } setEditingKey('') } - const save = async (key) => { + const addNewRow = () => { + let newRow = { + key:newRowKeyValue + } + const newData = [...data, newRow] + setData(newData) + edit(newRow) + } + + const save = async (record) => { try { const row = await form.validateFields() - const newData = [...dataSource] - const index = newData.findIndex((item) => key === item.key) + const newData = [...data] + const index = newData.findIndex((item) => record.key === item.key) + let item = newData[index] - if (index > -1) { - const item = newData[index] - newData.splice(index, 1, { ...item, ...row }) - } else { - newData.push(row) - } + newData.splice(index, 1, { ...item, ...row }) + if(item.key === newRowKeyValue) + item.key = newRowKeyValue + newData.length + setEditingKey('') + setData(newData) + + if (editingKey === newRowKeyValue) + onRowAdd(item) + else + onRowEdit(item) + if(onChange) onChange(newData) - else - console.error(`EditableTable.onChange doesn't connected`) + } catch (errInfo) { console.log('Validate Failed:', errInfo) } } + 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) + } + const operationColumn = { - title: '', + title: (!!onRowAdd) &&