-
Notifications
You must be signed in to change notification settings - Fork 572
/
Header.tsx
126 lines (106 loc) · 3.24 KB
/
Header.tsx
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import * as React from 'react';
import {
ColumnsType,
CellType,
StickyOffsets,
ColumnType,
GetComponentProps,
ColumnGroupType,
} from '../interface';
import HeaderRow from './HeaderRow';
import TableContext from '../context/TableContext';
function parseHeaderRows<RecordType>(
rootColumns: ColumnsType<RecordType>,
): CellType<RecordType>[][] {
const rows: CellType<RecordType>[][] = [];
function fillRowCells(
columns: ColumnsType<RecordType>,
colIndex: number,
rowIndex: number = 0,
): number[] {
// Init rows
rows[rowIndex] = rows[rowIndex] || [];
let currentColIndex = colIndex;
const colSpans: number[] = columns.filter(Boolean).map(column => {
const cell: CellType<RecordType> = {
key: column.key,
className: column.className || '',
children: column.title,
column,
colStart: currentColIndex,
};
let colSpan: number = 1;
const subColumns = (column as ColumnGroupType<RecordType>).children;
if (subColumns && subColumns.length > 0) {
colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(
(total, count) => total + count,
0,
);
cell.hasSubColumns = true;
}
if ('colSpan' in column) {
({ colSpan } = column);
}
if ('rowSpan' in column) {
cell.rowSpan = column.rowSpan;
}
cell.colSpan = colSpan;
cell.colEnd = cell.colStart + colSpan - 1;
rows[rowIndex].push(cell);
currentColIndex += colSpan;
return colSpan;
});
return colSpans;
}
// Generate `rows` cell data
fillRowCells(rootColumns, 0);
// Handle `rowSpan`
const rowCount = rows.length;
for (let rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
rows[rowIndex].forEach(cell => {
if (!('rowSpan' in cell) && !cell.hasSubColumns) {
// eslint-disable-next-line no-param-reassign
cell.rowSpan = rowCount - rowIndex;
}
});
}
return rows;
}
export interface HeaderProps<RecordType> {
columns: ColumnsType<RecordType>;
flattenColumns: readonly ColumnType<RecordType>[];
stickyOffsets: StickyOffsets;
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
}
function Header<RecordType>({
stickyOffsets,
columns,
flattenColumns,
onHeaderRow,
}: HeaderProps<RecordType>): React.ReactElement {
const { prefixCls, getComponent } = React.useContext(TableContext);
const rows: CellType<RecordType>[][] = React.useMemo(() => parseHeaderRows(columns), [columns]);
const WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
const trComponent = getComponent(['header', 'row'], 'tr');
const thComponent = getComponent(['header', 'cell'], 'th');
return (
<WrapperComponent className={`${prefixCls}-thead`}>
{rows.map((row, rowIndex) => {
const rowNode = (
<HeaderRow
key={rowIndex}
flattenColumns={flattenColumns}
cells={row}
stickyOffsets={stickyOffsets}
rowComponent={trComponent}
cellComponent={thComponent}
onHeaderRow={onHeaderRow}
index={rowIndex}
/>
);
return rowNode;
})}
</WrapperComponent>
);
}
export default Header;