From 3f589eb75ef279cfcbd8d7ddc94be7956a09d28e Mon Sep 17 00:00:00 2001 From: dengfuping Date: Mon, 23 Oct 2023 14:39:38 +0800 Subject: [PATCH 1/2] fix(design): Table columns maybe undefined --- packages/design/src/table/index.md | 2 ++ packages/design/src/table/index.tsx | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/design/src/table/index.md b/packages/design/src/table/index.md index 8f5803a73..77eb5ad05 100644 --- a/packages/design/src/table/index.md +++ b/packages/design/src/table/index.md @@ -24,6 +24,8 @@ nav: + + ## API diff --git a/packages/design/src/table/index.tsx b/packages/design/src/table/index.tsx index 4fd880b04..40b401042 100644 --- a/packages/design/src/table/index.tsx +++ b/packages/design/src/table/index.tsx @@ -68,7 +68,7 @@ function Table(props: TableProps) { const [currentSelectedRows, setCurrentSelectedRows] = useState([]); const [currentSelectedInfo, setCurrentSelectedInfo] = useState({}); - const newColumns = columns.map(item => { + const newColumns = columns?.map(item => { if (item.ellipsis) { return { ...item, From 9f1477b078bf1418160201ad95f997da8ee1675a Mon Sep 17 00:00:00 2001 From: dengfuping Date: Mon, 23 Oct 2023 14:40:55 +0800 Subject: [PATCH 2/2] improve(design): Add edit-row demo for Table --- packages/design/src/table/demo/edit-row.tsx | 182 ++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 packages/design/src/table/demo/edit-row.tsx diff --git a/packages/design/src/table/demo/edit-row.tsx b/packages/design/src/table/demo/edit-row.tsx new file mode 100644 index 000000000..ee09e7985 --- /dev/null +++ b/packages/design/src/table/demo/edit-row.tsx @@ -0,0 +1,182 @@ +import React, { useState } from 'react'; +import { Form, Input, InputNumber, Popconfirm, Table, Typography } from '@oceanbase/design'; + +interface Item { + key: string; + name: string; + age: number; + address: string; +} + +const originData: Item[] = []; +for (let i = 0; i < 100; i++) { + originData.push({ + key: i.toString(), + name: `Edward ${i}`, + age: 32, + address: `London Park no. ${i}`, + }); +} +interface EditableCellProps extends React.HTMLAttributes { + editing: boolean; + dataIndex: string; + title: any; + inputType: 'number' | 'text'; + record: Item; + index: number; + children: React.ReactNode; +} + +const EditableCell: React.FC = ({ + editing, + dataIndex, + title, + inputType, + record, + index, + children, + ...restProps +}) => { + const inputNode = inputType === 'number' ? : ; + + return ( + + {editing ? ( + + {inputNode} + + ) : ( + children + )} + + ); +}; + +const App: React.FC = () => { + const [form] = Form.useForm(); + const [data, setData] = useState(originData); + const [editingKey, setEditingKey] = useState(''); + + const isEditing = (record: Item) => record.key === editingKey; + + const edit = (record: Partial & { key: React.Key }) => { + form.setFieldsValue({ name: '', age: '', address: '', ...record }); + setEditingKey(record.key); + }; + + const cancel = () => { + setEditingKey(''); + }; + + const save = async (key: React.Key) => { + try { + const row = (await form.validateFields()) as Item; + + const newData = [...data]; + const index = newData.findIndex(item => key === item.key); + if (index > -1) { + const item = newData[index]; + newData.splice(index, 1, { + ...item, + ...row, + }); + setData(newData); + setEditingKey(''); + } else { + newData.push(row); + setData(newData); + setEditingKey(''); + } + } catch (errInfo) { + console.log('Validate Failed:', errInfo); + } + }; + + const columns = [ + { + title: 'name', + dataIndex: 'name', + width: '25%', + editable: true, + }, + { + title: 'age', + dataIndex: 'age', + width: '15%', + editable: true, + }, + { + title: 'address', + dataIndex: 'address', + width: '40%', + editable: true, + }, + { + title: 'operation', + dataIndex: 'operation', + render: (_: any, record: Item) => { + const editable = isEditing(record); + return editable ? ( + + save(record.key)} style={{ marginRight: 8 }}> + Save + + + Cancel + + + ) : ( + edit(record)}> + Edit + + ); + }, + }, + ]; + + const mergedColumns = columns.map(col => { + if (!col.editable) { + return col; + } + return { + ...col, + onCell: (record: Item) => ({ + record, + inputType: col.dataIndex === 'age' ? 'number' : 'text', + dataIndex: col.dataIndex, + title: col.title, + editing: isEditing(record), + }), + }; + }); + + return ( +
+ + + ); +}; + +export default App;