|
1 |
| -import React from 'react'; |
2 |
| -import { Resizable } from 'react-resizable'; |
3 |
| -import Table from 'rc-table'; |
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Table, { INTERNAL_HOOKS } from 'rc-table'; |
| 3 | +import type { ColumnType } from 'rc-table'; |
4 | 4 | import '../../assets/index.less';
|
5 |
| -import 'react-resizable/css/styles.css'; |
6 |
| -import type { ColumnType } from '@/interface'; |
7 | 5 |
|
8 |
| -const ResizableTitle = props => { |
9 |
| - const { onResize, width, ...restProps } = props; |
| 6 | +const data = [ |
| 7 | + { a: '123', b: 'xxxxxxxx xxxxxxxx', d: 3, key: '1' }, |
| 8 | + { a: 'cdd', b: 'edd12221 edd12221', d: 3, key: '2' }, |
| 9 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '3' }, |
| 10 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '4' }, |
| 11 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '5' }, |
| 12 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '6' }, |
| 13 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '7' }, |
| 14 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '8' }, |
| 15 | + { a: '133', c: 'edd12221 edd12221', d: 2, key: '9' }, |
| 16 | +]; |
10 | 17 |
|
11 |
| - if (!width) { |
12 |
| - return <th {...restProps} />; |
13 |
| - } |
| 18 | +const Demo = () => { |
| 19 | + const [widthMap, setWidthMap] = useState<Map<React.Key, number>>(new Map()); |
| 20 | + |
| 21 | + const columns1 = [ |
| 22 | + { title: 'title1', dataIndex: 'aaa', key: 'aaa', width: 100 }, |
| 23 | + { title: 'title2', dataIndex: 'bbb', key: 'bbb', width: 100 }, |
| 24 | + ].map(i => ({ |
| 25 | + ...i, |
| 26 | + resizable: true, |
| 27 | + width: widthMap.get(i.key ?? i.dataIndex) ?? i.width, |
| 28 | + })) as ColumnType<any>[]; |
| 29 | + |
| 30 | + const columns2 = [ |
| 31 | + { title: 'title1', dataIndex: 'a', key: 'a', fixed: 'left' }, |
| 32 | + { title: 'title2', dataIndex: 'b', key: 'b', fixed: 'left' }, |
| 33 | + { title: 'title3', dataIndex: 'c', key: 'c' }, |
| 34 | + { title: 'title4', dataIndex: 'b', key: 'd' }, |
| 35 | + { title: 'title5', dataIndex: 'b', key: 'e' }, |
| 36 | + { title: 'title6', dataIndex: 'b', key: 'f' }, |
| 37 | + { title: 'title7', dataIndex: 'b', key: 'g' }, |
| 38 | + { title: 'title8', dataIndex: 'b', key: 'h' }, |
| 39 | + { title: 'title9', dataIndex: 'b', key: 'i' }, |
| 40 | + { title: 'title10', dataIndex: 'b', key: 'j' }, |
| 41 | + { title: 'title11', dataIndex: 'b', key: 'k', fixed: 'right' }, |
| 42 | + { title: 'title12', dataIndex: 'b', key: 'l', fixed: 'right' }, |
| 43 | + ].map(i => ({ |
| 44 | + ...i, |
| 45 | + resizable: true, |
| 46 | + width: widthMap.get(i.key ?? i.dataIndex) ?? 150, |
| 47 | + })) as ColumnType<any>[]; |
14 | 48 |
|
15 | 49 | return (
|
16 |
| - <Resizable width={width} height={0} onResize={onResize}> |
17 |
| - <th {...restProps} /> |
18 |
| - </Resizable> |
| 50 | + <div> |
| 51 | + table width: 800px {'columns=[{width: 100, width: 100}]'} 情况 |
| 52 | + <Table |
| 53 | + style={{ width: 800 }} |
| 54 | + scroll={{ y: 300, x: columns1.reduce((t, c) => t + (c.width as number), 0) }} |
| 55 | + columns={columns1} |
| 56 | + data={data} |
| 57 | + onColumnResizeComplete={({ columnWidths }) => { |
| 58 | + setWidthMap(prev => { |
| 59 | + const result = new Map(prev); |
| 60 | + columnWidths.forEach(i => { |
| 61 | + result.set(i.columnKey, i.width); |
| 62 | + }); |
| 63 | + return result; |
| 64 | + }); |
| 65 | + }} |
| 66 | + internalHooks={INTERNAL_HOOKS} |
| 67 | + getContainerWidth={(ele, width) => { |
| 68 | + // Minus border |
| 69 | + const borderWidth = getComputedStyle( |
| 70 | + ele.querySelector('.rc-table-body'), |
| 71 | + ).borderInlineStartWidth; |
| 72 | + const mergedWidth = width - parseInt(borderWidth, 10); |
| 73 | + return mergedWidth; |
| 74 | + }} |
| 75 | + /> |
| 76 | + <br /> |
| 77 | + 大多数情况 |
| 78 | + <Table |
| 79 | + style={{ width: 800 }} |
| 80 | + scroll={{ y: 300, x: columns2.reduce((t, c) => t + (c.width as number), 0) }} |
| 81 | + columns={columns2} |
| 82 | + data={data} |
| 83 | + onColumnResizeComplete={({ columnWidths }) => { |
| 84 | + setWidthMap(prev => { |
| 85 | + const result = new Map(prev); |
| 86 | + columnWidths.forEach(i => { |
| 87 | + result.set(i.columnKey, i.width); |
| 88 | + }); |
| 89 | + return result; |
| 90 | + }); |
| 91 | + }} |
| 92 | + internalHooks={INTERNAL_HOOKS} |
| 93 | + getContainerWidth={(ele, width) => { |
| 94 | + // Minus border |
| 95 | + const borderWidth = getComputedStyle( |
| 96 | + ele.querySelector('.rc-table-body'), |
| 97 | + ).borderInlineStartWidth; |
| 98 | + const mergedWidth = width - parseInt(borderWidth, 10); |
| 99 | + return mergedWidth; |
| 100 | + }} |
| 101 | + /> |
| 102 | + {/* <Table resizable style={{ width: 800 }} columns={columns} data={data} /> */} |
| 103 | + </div> |
19 | 104 | );
|
20 | 105 | };
|
21 | 106 |
|
22 |
| -interface RecordType { |
23 |
| - a: string; |
24 |
| - b?: string; |
25 |
| - c?: string; |
26 |
| - d?: number; |
27 |
| - key: string; |
28 |
| -} |
29 |
| - |
30 |
| -interface DemoState { |
31 |
| - columns: ColumnType<RecordType>[]; |
32 |
| -} |
33 |
| - |
34 |
| -class Demo extends React.Component<{}, DemoState> { |
35 |
| - state: DemoState = { |
36 |
| - columns: [ |
37 |
| - { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, |
38 |
| - { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, |
39 |
| - { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, |
40 |
| - { |
41 |
| - title: 'Operations', |
42 |
| - dataIndex: '', |
43 |
| - key: 'd', |
44 |
| - render() { |
45 |
| - return <a href="#">Operations</a>; |
46 |
| - }, |
47 |
| - }, |
48 |
| - ], |
49 |
| - }; |
50 |
| - |
51 |
| - components = { |
52 |
| - header: { |
53 |
| - cell: ResizableTitle, |
54 |
| - }, |
55 |
| - }; |
56 |
| - |
57 |
| - data = [ |
58 |
| - { a: '123', key: '1' }, |
59 |
| - { a: 'cdd', b: 'edd', key: '2' }, |
60 |
| - { a: '1333', c: 'eee', d: 2, key: '3' }, |
61 |
| - ]; |
62 |
| - |
63 |
| - handleResize = |
64 |
| - index => |
65 |
| - (e, { size }) => { |
66 |
| - this.setState(({ columns }) => { |
67 |
| - const nextColumns = [...columns]; |
68 |
| - nextColumns[index] = { |
69 |
| - ...nextColumns[index], |
70 |
| - width: size.width, |
71 |
| - }; |
72 |
| - return { columns: nextColumns }; |
73 |
| - }); |
74 |
| - }; |
75 |
| - |
76 |
| - render() { |
77 |
| - const columns = this.state.columns.map((col, index) => ({ |
78 |
| - ...col, |
79 |
| - onHeaderCell: (column: ColumnType<RecordType>) => |
80 |
| - ({ |
81 |
| - width: column.width, |
82 |
| - onResize: this.handleResize(index), |
83 |
| - }) as any, |
84 |
| - })); |
85 |
| - |
86 |
| - return ( |
87 |
| - <div> |
88 |
| - <h2>Integrate with react-resizable</h2> |
89 |
| - <Table components={this.components} columns={columns} data={this.data} /> |
90 |
| - </div> |
91 |
| - ); |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 | 107 | export default Demo;
|
0 commit comments