|
| 1 | +import React from 'react'; |
| 2 | +import Table from '../src'; |
| 3 | +import '../assets/index.less'; |
| 4 | +import { ColumnsType } from '../src/interface'; |
| 5 | +import { useCheckbox } from './utils/useInput'; |
| 6 | + |
| 7 | +interface RecordType { |
| 8 | + a: string; |
| 9 | + b?: string; |
| 10 | + c: string; |
| 11 | + d: number; |
| 12 | + key: string; |
| 13 | +} |
| 14 | + |
| 15 | +const originData: RecordType[] = [ |
| 16 | + { a: 'aaa', b: 'bbb', c: '内容内容内容内容内容', d: 3, key: '1' }, |
| 17 | + { a: 'aaa', b: 'bbb', c: '内容内容内容内容内容', d: 3, key: '2' }, |
| 18 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '3' }, |
| 19 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '4' }, |
| 20 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '5' }, |
| 21 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '6' }, |
| 22 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '7' }, |
| 23 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '8' }, |
| 24 | + { a: 'aaa', c: '内容内容内容内容内容', d: 2, key: '9' }, |
| 25 | +]; |
| 26 | + |
| 27 | +const longTextData: RecordType[] = [...originData]; |
| 28 | +longTextData[0] = { |
| 29 | + ...longTextData[0], |
| 30 | + a: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
| 31 | +}; |
| 32 | + |
| 33 | +const useColumn = ( |
| 34 | + fixLeft: boolean, |
| 35 | + fixTitle: boolean, |
| 36 | + fixRight: boolean, |
| 37 | + ellipsis: boolean, |
| 38 | + percentage: boolean, |
| 39 | +) => { |
| 40 | + const columns: ColumnsType<RecordType> = React.useMemo( |
| 41 | + () => [ |
| 42 | + { |
| 43 | + title: 'title1', |
| 44 | + dataIndex: 'a', |
| 45 | + key: 'a', |
| 46 | + width: percentage ? '10%' : 80, |
| 47 | + fixed: fixLeft ? 'left' : null, |
| 48 | + ellipsis, |
| 49 | + }, |
| 50 | + { title: 'title2', dataIndex: 'b', key: 'b', width: 80, fixed: fixLeft ? 'left' : null }, |
| 51 | + { |
| 52 | + title: 'title3', |
| 53 | + fixed: fixLeft && fixTitle ? 'left' : null, |
| 54 | + children: [ |
| 55 | + { title: 'title4', dataIndex: 'c', key: 'd', width: 100 }, |
| 56 | + { title: 'title5', dataIndex: 'c', key: 'e', width: 100 }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { title: 'title6', dataIndex: 'c', key: 'f' }, |
| 60 | + { title: 'title7', dataIndex: 'c', key: 'g' }, |
| 61 | + { title: 'title8', dataIndex: 'c', key: 'h' }, |
| 62 | + { title: 'title9', dataIndex: 'b', key: 'i' }, |
| 63 | + { title: 'title10', dataIndex: 'b', key: 'j' }, |
| 64 | + { title: 'title11', dataIndex: 'b', key: 'k', width: 100, fixed: fixRight ? 'right' : null }, |
| 65 | + { title: 'title12', dataIndex: 'b', key: 'l', width: 80, fixed: fixRight ? 'right' : null }, |
| 66 | + ], |
| 67 | + [fixLeft, fixTitle, fixRight, ellipsis, percentage], |
| 68 | + ); |
| 69 | + |
| 70 | + return columns; |
| 71 | +}; |
| 72 | + |
| 73 | +const Demo = () => { |
| 74 | + const [autoWidth, autoWidthProps] = useCheckbox(false); |
| 75 | + const [isRtl, isRtlProps] = useCheckbox(true); |
| 76 | + const [longText, longTextProps] = useCheckbox(false); |
| 77 | + const [fixHeader, fixHeaderProps] = useCheckbox(true); |
| 78 | + const [fixLeft, fixLeftProps] = useCheckbox(true); |
| 79 | + const [fixRight, fixRightProps] = useCheckbox(true); |
| 80 | + const [fixTitle3, fixTitle3Props] = useCheckbox(false); |
| 81 | + const [ellipsis, ellipsisProps] = useCheckbox(false); |
| 82 | + const [percentage, percentageProps] = useCheckbox(false); |
| 83 | + const [empty, emptyProps] = useCheckbox(false); |
| 84 | + const columns = useColumn(fixLeft, fixTitle3, fixRight, ellipsis, percentage); |
| 85 | + |
| 86 | + let mergedData: RecordType[]; |
| 87 | + if (empty) { |
| 88 | + mergedData = null; |
| 89 | + } else if (longText) { |
| 90 | + mergedData = longTextData; |
| 91 | + } else { |
| 92 | + mergedData = originData; |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <React.StrictMode> |
| 97 | + <div> |
| 98 | + <h2>Fixed columns and header in RTL direction</h2> |
| 99 | + |
| 100 | + <label> |
| 101 | + <input {...isRtlProps} /> |
| 102 | + IsRtl |
| 103 | + </label> |
| 104 | + <label> |
| 105 | + <input {...autoWidthProps} /> |
| 106 | + Auto Width |
| 107 | + </label> |
| 108 | + <label> |
| 109 | + <input {...longTextProps} /> |
| 110 | + Long Text |
| 111 | + </label> |
| 112 | + <label> |
| 113 | + <input {...fixHeaderProps} /> |
| 114 | + Fix Header |
| 115 | + </label> |
| 116 | + <label> |
| 117 | + <input {...fixLeftProps} /> |
| 118 | + Fix Left |
| 119 | + </label> |
| 120 | + <label> |
| 121 | + <input {...fixTitle3Props} /> |
| 122 | + Fix title3 |
| 123 | + </label> |
| 124 | + <label> |
| 125 | + <input {...fixRightProps} /> |
| 126 | + Fix Right |
| 127 | + </label> |
| 128 | + <label> |
| 129 | + <input {...ellipsisProps} /> |
| 130 | + Ellipsis First Column |
| 131 | + </label> |
| 132 | + <label> |
| 133 | + <input {...percentageProps} /> |
| 134 | + Percentage Width |
| 135 | + </label> |
| 136 | + <label> |
| 137 | + <input {...emptyProps} /> |
| 138 | + Empty |
| 139 | + </label> |
| 140 | + |
| 141 | + <Table<RecordType> |
| 142 | + columns={columns} |
| 143 | + scroll={{ x: 1650, y: fixHeader ? 300 : null }} |
| 144 | + data={mergedData} |
| 145 | + style={{ width: autoWidth ? null : 800 }} |
| 146 | + direction={isRtl ? 'rtl' : 'ltr'} |
| 147 | + /> |
| 148 | + </div> |
| 149 | + </React.StrictMode> |
| 150 | + ); |
| 151 | +}; |
| 152 | + |
| 153 | +export default Demo; |
0 commit comments