From aaec7643603699560deaca1e69ea321da6ad6cad Mon Sep 17 00:00:00 2001 From: hemengke1997 <49073282+hemengke1997@users.noreply.github.com> Date: Tue, 10 Jan 2023 11:10:53 +0800 Subject: [PATCH] fix: `mergeColumns` edge case #37 --- example/src/2023-1-10/index.tsx | 131 ++++++++++++++++++++++++++++++++ example/src/main.tsx | 5 +- src/utils/useLocalColumns.ts | 9 +-- 3 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 example/src/2023-1-10/index.tsx diff --git a/example/src/2023-1-10/index.tsx b/example/src/2023-1-10/index.tsx new file mode 100644 index 0000000..a47e7d9 --- /dev/null +++ b/example/src/2023-1-10/index.tsx @@ -0,0 +1,131 @@ +import { Table } from 'antd' +import useARH from '@minko-fe/use-antd-resizable-header' +import '@minko-fe/use-antd-resizable-header/dist/style.css' +import React, { useEffect, useMemo, useState } from 'react' + +const columns = [ + { + title: 'Name', + dataIndex: 'name', + key: 'name', + width: 100, + ellipsis: true, + fixed: 'left', + filters: [ + { + text: 'Joe', + value: 'Joe', + }, + { + text: 'John', + value: 'John', + }, + ], + onFilter: (value, record) => record.name.indexOf(value) === 0, + }, + { + title: 'Other', + children: [ + { + title: 'Age', + dataIndex: 'age', + key: 'age', + width: 150, + sorter: (a, b) => a.age - b.age, + }, + { + title: 'Address', + children: [ + { + title: 'Street', + dataIndex: 'street', + key: 'street', + width: 150, + }, + { + title: 'Block', + children: [ + { + title: 'Building', + dataIndex: 'building', + key: 'building', + width: 100, + }, + { + title: 'Door No.', + dataIndex: 'number', + key: 'number', + width: 100, + }, + ], + }, + ], + }, + ], + }, + { + title: 'Company', + children: [ + { + title: 'Company Address', + dataIndex: 'companyAddress', + key: 'companyAddress', + width: 200, + }, + { + title: 'Company Name', + dataIndex: 'companyName', + key: 'companyName', + }, + ], + }, + { + title: 'Gender', + dataIndex: 'gender', + key: 'gender', + width: 80, + fixed: 'right', + }, +] + +const data: any[] = [] +for (let i = 0; i < 100; i++) { + data.push({ + key: i, + name: 'John Brown', + age: i + 1, + street: 'Lake Park', + building: 'C', + number: 2035, + companyAddress: 'Lake Street 42', + companyName: 'SoftLake Co', + gender: 'M', + }) +} + +const ResizableTable = () => { + const [col, setCol] = useState([]) + useEffect(() => { + setTimeout(() => { + setCol(columns) + }, 200) + }, []) + const { components, resizableColumns, tableWidth } = useARH({ + columns: useMemo(() => { + console.log(col) + return col + }, [col]), + columnsState: { + persistenceKey: '2023-1-10', + persistenceType: 'localStorage', + }, + }) + + return ( + <> + + + ) +} + +export default ResizableTable diff --git a/example/src/main.tsx b/example/src/main.tsx index 965870e..a2a4338 100644 --- a/example/src/main.tsx +++ b/example/src/main.tsx @@ -2,8 +2,9 @@ import React from 'react' import ReactDOM from 'react-dom/client' import './index.css' // import App from './App' -import CodeBox from './CodeBox' +// import CodeBox from './CodeBox' +import ResizableTable from './2023-1-10' const root = ReactDOM.createRoot(document.getElementById('root')!) -root.render() +root.render() diff --git a/src/utils/useLocalColumns.ts b/src/utils/useLocalColumns.ts index 03415ba..3533231 100644 --- a/src/utils/useLocalColumns.ts +++ b/src/utils/useLocalColumns.ts @@ -11,9 +11,9 @@ interface LocalColumnsProp { function mergeColumns(src: T, target: T, mergeKey: string): T { const res = src - if (Array.isArray(res)) { - res.forEach((t, i) => { - if (t.children) { + if (Array.isArray(res) && Array.isArray(target)) { + res.forEach((t?, i?) => { + if (t?.children) { mergeColumns(t.children, target[i]?.children, mergeKey) } else { res[i][mergeKey] = target[i]?.[mergeKey] @@ -47,8 +47,7 @@ function useLocalColumns>({ try { const localResizableColumns = JSON.parse(storage?.getItem(persistenceKey) || '{}')?.resizableColumns - const x = mergeColumns(columnsProp || [], localResizableColumns, 'width') - return x + return mergeColumns(columnsProp || [], localResizableColumns, 'width') } catch (error) { console.error(error) }