|
| 1 | +import React from 'react'; |
| 2 | +import '../../assets/index.less'; |
| 3 | +import type { ColumnsType } from '../../src/interface'; |
| 4 | +import { VirtualTable } from '../../src'; |
| 5 | + |
| 6 | +interface RecordType { |
| 7 | + a: string; |
| 8 | + b?: string; |
| 9 | + c?: string; |
| 10 | + d: number; |
| 11 | + indexKey: string; |
| 12 | +} |
| 13 | + |
| 14 | +const columns: ColumnsType = [ |
| 15 | + { title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' }, |
| 16 | + { title: 'title2', dataIndex: 'b', key: 'b', width: 100, fixed: 'left', ellipsis: true }, |
| 17 | + { |
| 18 | + title: 'title3', |
| 19 | + dataIndex: 'c', |
| 20 | + key: 'c', |
| 21 | + onCell: (_, index) => { |
| 22 | + if (index % 4 === 0) { |
| 23 | + return { |
| 24 | + rowSpan: 3, |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + if (index % 4 === 3) { |
| 29 | + return { |
| 30 | + rowSpan: 1, |
| 31 | + colSpan: 3, |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + return { |
| 36 | + rowSpan: 0, |
| 37 | + }; |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + title: 'title4', |
| 42 | + key: 'd', |
| 43 | + children: [ |
| 44 | + // Children columns |
| 45 | + { |
| 46 | + title: 'title4-1', |
| 47 | + dataIndex: 'b', |
| 48 | + onCell: (_, index) => { |
| 49 | + if (index % 4 === 0) { |
| 50 | + return { |
| 51 | + colSpan: 3, |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + if (index % 4 === 3) { |
| 56 | + return { |
| 57 | + colSpan: 0, |
| 58 | + }; |
| 59 | + } |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + title: 'title4-2', |
| 64 | + dataIndex: 'b', |
| 65 | + onCell: (_, index) => { |
| 66 | + if (index % 4 === 0 || index % 4 === 3) { |
| 67 | + return { |
| 68 | + colSpan: 0, |
| 69 | + }; |
| 70 | + } |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + title: 'title6', |
| 77 | + dataIndex: 'b', |
| 78 | + key: 'f', |
| 79 | + onCell: (_, index) => { |
| 80 | + if (index % 4 === 0) { |
| 81 | + return { |
| 82 | + rowSpan: 0, |
| 83 | + colSpan: 0, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + if (index % 4 === 1) { |
| 88 | + return { |
| 89 | + rowSpan: 3, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + rowSpan: 0, |
| 95 | + }; |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + title: ( |
| 100 | + <div> |
| 101 | + title7 |
| 102 | + <br /> |
| 103 | + <br /> |
| 104 | + <br /> |
| 105 | + Hello world! |
| 106 | + </div> |
| 107 | + ), |
| 108 | + dataIndex: 'bk', |
| 109 | + key: 'g', |
| 110 | + }, |
| 111 | + { |
| 112 | + title: 'title8', |
| 113 | + dataIndex: 'b', |
| 114 | + onCell: (_, index) => { |
| 115 | + if (index % 2 === 0) { |
| 116 | + return { |
| 117 | + rowSpan: 2, |
| 118 | + colSpan: 2, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + return { |
| 123 | + rowSpan: 0, |
| 124 | + }; |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + title: 'title9 i', |
| 129 | + dataIndex: 'b', |
| 130 | + key: 'i', |
| 131 | + onCell: () => ({ |
| 132 | + colSpan: 0, |
| 133 | + }), |
| 134 | + }, |
| 135 | + { title: 'title10', dataIndex: 'b', key: 'j' }, |
| 136 | + { |
| 137 | + title: 'title11', |
| 138 | + dataIndex: 'b', |
| 139 | + key: 'k', |
| 140 | + width: 50, |
| 141 | + fixed: 'right', |
| 142 | + onCell: (_, index) => { |
| 143 | + return { |
| 144 | + rowSpan: index % 2 === 0 ? 2 : 0, |
| 145 | + // colSpan: 2, |
| 146 | + }; |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + title: 'title12', |
| 151 | + dataIndex: 'b', |
| 152 | + key: 'l', |
| 153 | + width: 100, |
| 154 | + fixed: 'right', |
| 155 | + onCell: () => { |
| 156 | + return { |
| 157 | + // colSpan: 0, |
| 158 | + }; |
| 159 | + }, |
| 160 | + }, |
| 161 | +]; |
| 162 | + |
| 163 | +export function cleanOnCell(cols: any = []) { |
| 164 | + cols.forEach(col => { |
| 165 | + delete (col as any).onCell; |
| 166 | + |
| 167 | + cleanOnCell((col as any).children); |
| 168 | + }); |
| 169 | +} |
| 170 | +cleanOnCell(columns); |
| 171 | + |
| 172 | +const data: RecordType[] = new Array(4 * 10000).fill(null).map((_, index) => ({ |
| 173 | + a: `a${index}`, |
| 174 | + b: `b${index}`, |
| 175 | + c: `c${index}`, |
| 176 | + d: index, |
| 177 | + bk: <strong>Hello</strong>, |
| 178 | + indexKey: `${index}`, |
| 179 | + // children: [ |
| 180 | + // { |
| 181 | + // indexKey: `${index}-1`, |
| 182 | + // }, |
| 183 | + // { |
| 184 | + // indexKey: `${index}-2`, |
| 185 | + // }, |
| 186 | + // ], |
| 187 | +})); |
| 188 | + |
| 189 | +const Demo = () => { |
| 190 | + const [scrollY, setScrollY] = React.useState(true); |
| 191 | + |
| 192 | + return ( |
| 193 | + <div style={{ width: 800, padding: `0 64px` }}> |
| 194 | + <label> |
| 195 | + <input type="checkbox" checked={scrollY} onChange={() => setScrollY(!scrollY)} /> |
| 196 | + Scroll Y |
| 197 | + </label> |
| 198 | + <VirtualTable |
| 199 | + columns={columns} |
| 200 | + // expandedRowRender={({ b, c }) => b || c} |
| 201 | + scroll={{ x: 1200, y: scrollY ? 200 : null }} |
| 202 | + data={data} |
| 203 | + // data={[]} |
| 204 | + rowKey="indexKey" |
| 205 | + expandable={{ |
| 206 | + expandedRowRender: () => 2333, |
| 207 | + columnWidth: 60, |
| 208 | + expandedRowClassName: () => 'good-one', |
| 209 | + }} |
| 210 | + // onRow={() => ({ className: 'rowed' })} |
| 211 | + rowClassName="nice-try" |
| 212 | + /> |
| 213 | + </div> |
| 214 | + ); |
| 215 | +}; |
| 216 | + |
| 217 | +export default Demo; |
0 commit comments