-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathcolumn-resize.tsx
More file actions
71 lines (65 loc) · 1.94 KB
/
column-resize.tsx
File metadata and controls
71 lines (65 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { useMemo, useState } from 'react';
import type { ColumnType, ColumnsType } from 'rc-table';
import Table from 'rc-table';
import { Resizable } from 'react-resizable';
import '../../assets/index.less';
import 'react-resizable/css/styles.css';
const ResizableTitle = props => {
const { onResize, width, ...restProps } = props;
if (!width) {
return <th {...restProps} />;
}
return (
<Resizable width={width} onResize={onResize}>
<th {...restProps} />
</Resizable>
);
};
const data = [
{ a: '123', key: '1' },
{ a: 'cdd', b: 'edd', key: '2' },
{ a: '1333', c: 'eee', d: 2, key: '3' },
] as const;
type RecordType = (typeof data)[number];
const Demo = () => {
const [columns, setColumns] = useState<ColumnsType<RecordType>>([
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
{
title: 'Operations',
dataIndex: '',
key: 'd',
render() {
return <a href="#">Operations</a>;
},
},
]);
const handleResize = useMemo(() => {
return (index: number) => {
return (_: React.MouseEvent<HTMLTableCellElement>, { size }: { size: { width: number } }) => {
setColumns(prevColumns =>
prevColumns.map((col, i) => (i === index ? { ...col, width: size.width } : col)),
);
};
};
}, []);
const columnsWithResizable = useMemo(() => {
return columns.map((col, index) => ({
...col,
onHeaderCell: (column: ColumnType<RecordType>) => ({
width: column.width,
onResize: handleResize(index),
}),
}));
}, [columns, handleResize]);
const tableProps = useMemo(() => {
return {
components: { header: { cell: ResizableTitle } },
columns: columnsWithResizable,
data,
};
}, [columnsWithResizable]);
return <Table {...tableProps} />;
};
export default Demo;