From 36a92f07560723c942ff280829a337a843f7c0d1 Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 8 Jul 2025 18:15:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(table):=20=E5=AE=9E=E9=99=85=E8=90=BD?= =?UTF-8?q?=E5=9C=B0=20useTableRequest/useModalForm=20=E5=B9=B6=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E6=89=B9=E9=87=8F=E5=88=A0=E9=99=A4=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useTableRequest.ts | 4 +- src/pages/TableList/index.tsx | 110 ++++++++++++++-------------------- 2 files changed, 46 insertions(+), 68 deletions(-) diff --git a/src/hooks/useTableRequest.ts b/src/hooks/useTableRequest.ts index acb4db84..de353ff2 100644 --- a/src/hooks/useTableRequest.ts +++ b/src/hooks/useTableRequest.ts @@ -35,9 +35,9 @@ export function useTableRequest

({ return result; }; - const handleRemove = async (id: string) => { + const handleRemove = async (id: string | number | (string | number)[]) => { if (!remove) return; - const result = await remove({ id }); + const result = await remove(id); refresh(); return result; }; diff --git a/src/pages/TableList/index.tsx b/src/pages/TableList/index.tsx index e2f10474..860bb59f 100644 --- a/src/pages/TableList/index.tsx +++ b/src/pages/TableList/index.tsx @@ -95,27 +95,25 @@ const handleRemove = async (selectedRows: API.RuleListItem[]) => { }; const TableList: React.FC = () => { - /** - * @en-US Pop-up window of new window - * @zh-CN 新建窗口的弹窗 - * */ - const [createModalOpen, handleModalOpen] = useState(false); - /** - * @en-US The pop-up window of the distribution update window - * @zh-CN 分布更新窗口的弹窗 - * */ - const [updateModalOpen, handleUpdateModalOpen] = useState(false); - - const [showDetail, setShowDetail] = useState(false); + // 表格数据与增删改查 + const { data, loading, refresh, handleAdd, handleUpdate, handleRemove } = + useTableRequest({ + query: (params) => rule(params || {}), + add: addRule, + update: updateRule, + remove: (params) => + removeRule({ key: Array.isArray(params) ? params : [params] }), + }); - const actionRef = useRef(null); - const [currentRow, setCurrentRow] = useState(); + // 新建弹窗 + const createModal = useModalForm(); + // 编辑弹窗 + const updateModal = useModalForm(); + // 详情抽屉 + const [showDetail, setShowDetail] = useState(false); + // 选中行 const [selectedRowsState, setSelectedRows] = useState([]); - - /** - * @en-US International configuration - * @zh-CN 国际化配置 - * */ + const actionRef = useRef(null); const intl = useIntl(); const columns: ProColumns[] = [ @@ -131,7 +129,7 @@ const TableList: React.FC = () => { return ( { - setCurrentRow(entity); + updateModal.setCurrent(entity); setShowDetail(true); }} > @@ -256,8 +254,8 @@ const TableList: React.FC = () => { { - handleUpdateModalOpen(true); - setCurrentRow(record); + updateModal.show(); + updateModal.setCurrent(record); }} > { })} actionRef={actionRef} rowKey="key" - search={{ - labelWidth: 120, - }} + search={{ labelWidth: 120 }} toolBarRender={() => [ , ]} - request={rule} + dataSource={data?.data || []} + loading={loading} columns={columns} rowSelection={{ - onChange: (_, selectedRows) => { - setSelectedRows(selectedRows); - }, + onChange: (_, selectedRows) => setSelectedRows(selectedRows), }} + pagination={data?.total ? { total: data.total } : undefined} + onChange={() => refresh()} /> {selectedRowsState?.length > 0 && ( { >