|
1 | 1 | /* eslint-disable no-console,func-names,react/no-multi-comp */
|
2 | 2 | import React from 'react';
|
3 |
| -import Table, { Column, ColumnGroup } from '../src'; |
| 3 | +import Table from '../src'; |
4 | 4 | import '../assets/index.less';
|
5 | 5 | import { ColumnsType } from '../src/interface';
|
6 | 6 |
|
7 |
| -interface RecordType { |
8 |
| - key: React.Key; |
9 |
| - id: number; |
10 |
| - date: number; |
11 |
| -} |
12 |
| - |
13 |
| -let UUID = 0; |
14 |
| - |
15 |
| -function renderDate(timestamp: number, record: RecordType) { |
16 |
| - return ( |
17 |
| - <span style={{ color: 'pink' }}> |
18 |
| - {record.id |
19 |
| - .toString(36) |
20 |
| - .substr(4) |
21 |
| - .toUpperCase()}{' '} |
22 |
| - - {new Date(timestamp).toString()} |
23 |
| - </span> |
24 |
| - ); |
25 |
| -} |
| 7 | +const columns: ColumnsType = [ |
| 8 | + { dataIndex: 'a', title: 'a' }, |
| 9 | + { dataIndex: 'b', title: 'b' }, |
| 10 | + { dataIndex: 'c', title: 'c', fixed: 'right', width: 200 }, |
| 11 | +]; |
26 | 12 |
|
27 | 13 | const Demo = () => {
|
28 |
| - const [data, setData] = React.useState([]); |
29 |
| - |
30 |
| - function offsetIndex(record: RecordType, offset: number) { |
31 |
| - const index = data.indexOf(record); |
32 |
| - const targetIndex = index + offset; |
33 |
| - const target = data[targetIndex]; |
34 |
| - const newData = [...data]; |
35 |
| - newData[index] = target; |
36 |
| - newData[targetIndex] = record; |
37 |
| - |
38 |
| - setData(newData); |
39 |
| - } |
40 |
| - |
41 |
| - const columns: ColumnsType<RecordType> = [ |
42 |
| - { title: 'ID', key: 'id', dataIndex: 'id', width: 100 }, |
43 |
| - { |
44 |
| - title: 'Date', |
45 |
| - dataIndex: 'date', |
46 |
| - width: 200, |
47 |
| - render: renderDate, |
48 |
| - }, |
49 |
| - { |
50 |
| - title: 'Merged Title', |
51 |
| - children: [ |
52 |
| - { |
53 |
| - title: '0 - ID', |
54 |
| - dataIndex: 'id', |
55 |
| - }, |
56 |
| - { |
57 |
| - title: '1 - Merge Date', |
58 |
| - children: [ |
59 |
| - { |
60 |
| - title: '1 - 0 - ID', |
61 |
| - dataIndex: 'id', |
62 |
| - }, |
63 |
| - { |
64 |
| - title: '1 - 1 - Date', |
65 |
| - dataIndex: 'date', |
66 |
| - }, |
67 |
| - ], |
68 |
| - }, |
69 |
| - ], |
70 |
| - }, |
71 |
| - { |
72 |
| - title: 'Operations', |
73 |
| - render(_: null, record: RecordType, index: number) { |
74 |
| - return ( |
75 |
| - <div> |
76 |
| - <button |
77 |
| - type="button" |
78 |
| - onClick={() => { |
79 |
| - offsetIndex(record, 1); |
80 |
| - }} |
81 |
| - disabled={index === data.length - 1} |
82 |
| - > |
83 |
| - ⬇ |
84 |
| - </button> |
85 |
| - <button |
86 |
| - type="button" |
87 |
| - onClick={() => { |
88 |
| - offsetIndex(record, -1); |
89 |
| - }} |
90 |
| - disabled={index === 0} |
91 |
| - > |
92 |
| - ⬆ |
93 |
| - </button> |
94 |
| - </div> |
95 |
| - ); |
96 |
| - }, |
97 |
| - }, |
98 |
| - ]; |
99 |
| - |
100 |
| - const addData = () => { |
101 |
| - setData(originData => { |
102 |
| - UUID += 1000 + Math.floor(Math.random() * 100000); |
103 |
| - |
104 |
| - const id = Date.now() + UUID; |
105 |
| - |
106 |
| - return [ |
107 |
| - ...originData, |
108 |
| - { |
109 |
| - key: id, |
110 |
| - id, |
111 |
| - date: id, |
112 |
| - }, |
113 |
| - ]; |
114 |
| - }); |
115 |
| - }; |
116 |
| - |
117 |
| - React.useEffect(() => { |
118 |
| - for (let i = 0; i < 3; i += 1) { |
119 |
| - addData(); |
120 |
| - } |
121 |
| - }, []); |
122 |
| - |
123 |
| - const [, forceUpdate] = React.useState(); |
| 14 | + const [showB, setShowB] = React.useState(true); |
| 15 | + const myColumns = showB ? columns : columns.filter(({ title }) => title !== 'b'); |
124 | 16 |
|
125 | 17 | return (
|
126 | 18 | <div>
|
127 |
| - <h2>Debug Usage, remove after stable released</h2> |
128 |
| - <button type="button" onClick={addData}> |
129 |
| - Add Row |
130 |
| - </button> |
131 |
| - <Table<RecordType> columns={columns} data={data} /> |
132 |
| - |
133 |
| - <br /> |
134 |
| - |
135 |
| - <Table<RecordType> data={data}> |
136 |
| - <Column dataIndex="id" title="ID" /> |
137 |
| - <ColumnGroup title="Merged Title"> |
138 |
| - <Column dataIndex="id" title="0 - ID" /> |
139 |
| - <Column dataIndex="date" title="1 - Date" /> |
140 |
| - </ColumnGroup> |
141 |
| - </Table> |
142 |
| - |
143 |
| - <br /> |
144 |
| - |
145 |
| - <Table<RecordType> data={data}> |
146 |
| - <Column dataIndex="id" title="ID" /> |
147 |
| - <Column dataIndex="id" title="Merged Title" colSpan={2} /> |
148 |
| - <Column dataIndex="date" colSpan={0} /> |
149 |
| - </Table> |
150 |
| - |
151 |
| - <Table<RecordType> /> |
152 |
| - |
| 19 | + <Table<any> |
| 20 | + scroll={{ x: 2000 }} |
| 21 | + tableLayout="auto" |
| 22 | + columns={myColumns} |
| 23 | + data={[{ a: 1, b: 2, c: 3, key: 1 }]} |
| 24 | + /> |
153 | 25 | <button
|
154 | 26 | type="button"
|
155 | 27 | onClick={() => {
|
156 |
| - forceUpdate(Math.random()); |
| 28 | + setShowB(!showB); |
157 | 29 | }}
|
158 | 30 | >
|
159 |
| - Update |
| 31 | + Trigger {showB.toString()} |
160 | 32 | </button>
|
161 | 33 | </div>
|
162 | 34 | );
|
|
0 commit comments