Skip to content

Commit

Permalink
fix: dynamic columns & table blink
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Jan 11, 2023
1 parent 3863504 commit ce4ab41
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 203 deletions.
4 changes: 3 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"@minko-fe/use-antd-resizable-header": "link:..",
"antd": "5.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-router": "^6.6.2",
"react-router-dom": "^6.6.2"
},
"devDependencies": {
"@types/react": "^18.0.26",
Expand Down
32 changes: 32 additions & 0 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react'
import React from 'react'
import { Table } from 'antd'
import useATRH from '@minko-fe/use-antd-resizable-header'
import '@minko-fe/use-antd-resizable-header/dist/style.css'
Expand All @@ -9,7 +9,7 @@ interface IProps {
}
const TableComponent: React.FC<IProps> = (props) => {
const { components, resizableColumns, tableWidth } = useATRH({
columns: useMemo(() => props.columns, [props.columns]),
columns: props.columns,
minConstraints: 70,
defaultWidth: 222,
columnsState: {
Expand Down
6 changes: 1 addition & 5 deletions example/src/CodeBox/index.tsx → example/src/36/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ const Hello: React.FC = () => {
}, 300)
}, [])

return (
<div style={{ width: 500 }}>
<TableComponent dataSource={data} columns={columns} />
</div>
)
return <TableComponent dataSource={data} columns={columns} />
}

export default Hello
File renamed without changes.
217 changes: 33 additions & 184 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,193 +1,42 @@
import React, { useMemo, useReducer } from 'react'
import { Table } from 'antd'
import ProTable from '@ant-design/pro-table'
import React, { Suspense, lazy } from 'react'
import { useRoutes } from 'react-router-dom'
import './App.css'
import { useAntdResizableHeader } from '@minko-fe/use-antd-resizable-header'
import '@minko-fe/use-antd-resizable-header/dist/style.css'

const tcls: any[] = [
{
title: '333',
dataIndex: 'name',
key: 'name',
width: 100,
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',
})
interface FunctionalImportType {
(): any
}

function App() {
const [x, setX] = useReducer((s) => s + 1, 0)

const { components, resizableColumns, tableWidth } = useAntdResizableHeader({
columns: tcls,
columnsState: {
persistenceType: 'localStorage',
persistenceKey: 'test',
// eslint-disable-next-line no-restricted-syntax
export default function App() {
const comp = (C) => {
return (
<Suspense fallback={<div />}>
<C />
</Suspense>
)
}

const routes = [
{
path: '/',
component: () => import('./basic'),
},
})

const proColumns = useMemo(
() => [
{
title: 'Pro-Name',
width: 100,
dataIndex: 'name',
key: 'name',
fixed: 'left',
},
{
title: 'Age',
width: 100,
dataIndex: 'age',
key: 'age',
},
{
title: 'x',
width: 100,
dataIndex: 'x',
key: 'x',
},
{
title: 'y',
width: 100,
dataIndex: 'y',
key: 'y',
},
{
title: 'Column 1',
dataIndex: 'address',
},
{
title: 'test render',
dataIndex: 'testRender',
width: 111,
render: () => {
return <div onClick={() => setX()}>{x}</div>
},
},
{
title: 'Action',
key: 'operation',
fixed: 'right',
render: (_, record) => {
return <span>{record?.age}</span>
},
width: 100,
},
],
[x],
)

const {
components: cp,
resizableColumns: rp,
tableWidth: tp,
} = useAntdResizableHeader({
columns: proColumns,
defaultWidth: 444,
columnsState: {
persistenceType: 'localStorage',
persistenceKey: 'fds',
{
path: '/36',
component: () => import('./36'),
},
})

return (
<div className='App'>
<Table columns={resizableColumns} components={components} dataSource={data} scroll={{ x: tableWidth }} />
{
path: '/37',
component: () => import('./37'),
},
]

<ProTable columns={rp} components={cp} dataSource={data} scroll={{ x: tp }} />
</div>
const element = useRoutes(
routes.map((t) => ({
element: comp(lazy(t.component as FunctionalImportType)),
...t,
})),
)
}

// eslint-disable-next-line no-restricted-syntax
export default App
return <div>{element}</div>
}
Loading

0 comments on commit ce4ab41

Please sign in to comment.