2023-08-28 17:36:23 +08:00
|
|
|
import React from 'react';
|
2023-10-11 13:55:20 +08:00
|
|
|
import { Button, Segmented, Space, Switch, Table, Typography } from 'antd';
|
2023-07-28 16:17:43 +08:00
|
|
|
import type { TableProps } from 'antd';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-08-28 17:36:23 +08:00
|
|
|
interface RecordType {
|
|
|
|
id: number;
|
|
|
|
firstName: string;
|
|
|
|
lastName: string;
|
|
|
|
age: number;
|
|
|
|
address1: string;
|
|
|
|
address2: string;
|
|
|
|
address3: string;
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-09-13 17:53:54 +08:00
|
|
|
const fixedColumns: TableProps<RecordType>['columns'] = [
|
2023-08-28 17:36:23 +08:00
|
|
|
{
|
|
|
|
title: 'ID',
|
|
|
|
dataIndex: 'id',
|
|
|
|
width: 100,
|
|
|
|
fixed: 'left',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'FistName',
|
|
|
|
dataIndex: 'firstName',
|
|
|
|
width: 120,
|
|
|
|
fixed: 'left',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'LastName',
|
|
|
|
dataIndex: 'lastName',
|
|
|
|
width: 120,
|
|
|
|
fixed: 'left',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Group',
|
|
|
|
width: 120,
|
|
|
|
render: (_, record) => `Group ${Math.floor(record.id / 4)}`,
|
|
|
|
onCell: (record) => ({
|
|
|
|
rowSpan: record.id % 4 === 0 ? 4 : 0,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
width: 100,
|
2023-08-30 21:18:09 +08:00
|
|
|
onCell: (record) => ({
|
|
|
|
colSpan: record.id % 4 === 0 ? 2 : 1,
|
|
|
|
}),
|
2023-08-28 17:36:23 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address 1',
|
|
|
|
dataIndex: 'address1',
|
2023-08-30 21:18:09 +08:00
|
|
|
onCell: (record) => ({
|
|
|
|
colSpan: record.id % 4 === 0 ? 0 : 1,
|
|
|
|
}),
|
2023-08-28 17:36:23 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address 2',
|
|
|
|
dataIndex: 'address2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address 3',
|
|
|
|
dataIndex: 'address3',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Action',
|
|
|
|
width: 150,
|
|
|
|
fixed: 'right',
|
|
|
|
render: () => (
|
|
|
|
<Space>
|
|
|
|
<Typography.Link>Action1</Typography.Link>
|
|
|
|
<Typography.Link>Action2</Typography.Link>
|
|
|
|
</Space>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-09-13 17:53:54 +08:00
|
|
|
const columns: TableProps<RecordType>['columns'] = [
|
|
|
|
{
|
|
|
|
title: 'ID',
|
|
|
|
dataIndex: 'id',
|
|
|
|
width: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'FistName',
|
|
|
|
dataIndex: 'firstName',
|
|
|
|
width: 120,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'LastName',
|
|
|
|
dataIndex: 'lastName',
|
|
|
|
width: 120,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const getData = (count: number) => {
|
|
|
|
const data: RecordType[] = new Array(count).fill(null).map((_, index) => ({
|
|
|
|
id: index,
|
|
|
|
firstName: `First_${index.toString(16)}`,
|
|
|
|
lastName: `Last_${index.toString(16)}`,
|
|
|
|
age: 25 + (index % 10),
|
|
|
|
address1: `New York No. ${index} Lake Park`,
|
|
|
|
address2: `London No. ${index} Lake Park`,
|
|
|
|
address3: `Sydney No. ${index} Lake Park`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-08-28 17:36:23 +08:00
|
|
|
const App = () => {
|
2023-09-13 17:53:54 +08:00
|
|
|
const [fixed, setFixed] = React.useState(true);
|
2023-08-28 17:36:23 +08:00
|
|
|
const [bordered, setBordered] = React.useState(true);
|
|
|
|
const [expanded, setExpanded] = React.useState(false);
|
|
|
|
const [empty, setEmpty] = React.useState(false);
|
2023-09-13 17:53:54 +08:00
|
|
|
const [count, setCount] = React.useState(10000);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-10-11 13:55:20 +08:00
|
|
|
const tblRef: Parameters<typeof Table>[0]['ref'] = React.useRef(null);
|
2023-09-13 17:53:54 +08:00
|
|
|
const data = React.useMemo(() => getData(count), [count]);
|
|
|
|
|
|
|
|
const mergedColumns = React.useMemo<typeof fixedColumns>(() => {
|
|
|
|
if (!fixed) {
|
2023-08-28 17:36:23 +08:00
|
|
|
return columns;
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-09-13 17:53:54 +08:00
|
|
|
if (!expanded) {
|
|
|
|
return fixedColumns;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedColumns.map((col) => ({ ...col, onCell: undefined }));
|
|
|
|
}, [expanded, fixed]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-08-28 17:36:23 +08:00
|
|
|
const expandableProps = React.useMemo<TableProps<RecordType>['expandable']>(() => {
|
|
|
|
if (!expanded) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-08-28 17:36:23 +08:00
|
|
|
return {
|
|
|
|
columnWidth: 48,
|
2023-08-30 21:18:09 +08:00
|
|
|
expandedRowRender: (record) => <p style={{ margin: 0 }}>🎉 Expanded {record.address1}</p>,
|
2023-08-28 17:36:23 +08:00
|
|
|
rowExpandable: (record) => record.id % 2 === 0,
|
|
|
|
};
|
|
|
|
}, [expanded]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return (
|
2023-08-30 21:18:09 +08:00
|
|
|
<div style={{ padding: 64 }}>
|
|
|
|
<Space direction="vertical" style={{ width: '100%' }}>
|
|
|
|
<Space>
|
|
|
|
<Switch
|
|
|
|
checked={bordered}
|
|
|
|
onChange={() => setBordered(!bordered)}
|
|
|
|
checkedChildren="Bordered"
|
|
|
|
unCheckedChildren="Bordered"
|
|
|
|
/>
|
2023-09-13 17:53:54 +08:00
|
|
|
<Switch
|
|
|
|
checked={fixed}
|
|
|
|
onChange={() => setFixed(!fixed)}
|
|
|
|
checkedChildren="Fixed"
|
|
|
|
unCheckedChildren="Fixed"
|
|
|
|
/>
|
2023-08-30 21:18:09 +08:00
|
|
|
<Switch
|
|
|
|
checked={expanded}
|
|
|
|
onChange={() => setExpanded(!expanded)}
|
|
|
|
checkedChildren="Expandable"
|
|
|
|
unCheckedChildren="Expandable"
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
checked={empty}
|
|
|
|
onChange={() => setEmpty(!empty)}
|
|
|
|
checkedChildren="Empty"
|
|
|
|
unCheckedChildren="Empty"
|
|
|
|
/>
|
2023-09-13 17:53:54 +08:00
|
|
|
<Segmented
|
|
|
|
value={count}
|
|
|
|
onChange={(value: number) => setCount(value)}
|
|
|
|
options={[
|
|
|
|
{
|
|
|
|
label: 'None',
|
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Less',
|
|
|
|
value: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Lot',
|
|
|
|
value: 10000,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2023-10-11 13:55:20 +08:00
|
|
|
|
|
|
|
{data.length >= 999 && (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
tblRef.current?.scrollTo({ index: 999 });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Scroll To index 999
|
|
|
|
</Button>
|
|
|
|
)}
|
2023-08-30 21:18:09 +08:00
|
|
|
</Space>
|
|
|
|
|
|
|
|
<Table
|
|
|
|
bordered={bordered}
|
|
|
|
virtual
|
|
|
|
columns={mergedColumns}
|
|
|
|
scroll={{ x: 2000, y: 400 }}
|
|
|
|
rowKey="id"
|
|
|
|
dataSource={empty ? [] : data}
|
|
|
|
pagination={false}
|
2023-10-11 13:55:20 +08:00
|
|
|
ref={tblRef}
|
2023-08-30 21:18:09 +08:00
|
|
|
rowSelection={
|
|
|
|
expanded
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
type: 'radio',
|
|
|
|
columnWidth: 48,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expandable={expandableProps}
|
2023-08-28 17:36:23 +08:00
|
|
|
/>
|
|
|
|
</Space>
|
2023-08-30 21:18:09 +08:00
|
|
|
</div>
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|