-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathHeaderRow.tsx
97 lines (88 loc) · 2.63 KB
/
HeaderRow.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
import * as React from 'react';
import Cell from '../Cell';
import TableContext from '../context/TableContext';
import { useContext } from '@rc-component/context';
import type {
CellType,
ColumnType,
CustomizeComponent,
GetComponentProps,
StickyOffsets,
} from '../interface';
import { getCellFixedInfo } from '../utils/fixUtil';
import { getColumnsKey } from '../utils/valueUtil';
export interface RowProps<RecordType> {
cells: readonly CellType<RecordType>[];
stickyOffsets: StickyOffsets;
flattenColumns: readonly ColumnType<RecordType>[];
rowComponent: CustomizeComponent;
cellComponent: CustomizeComponent;
onHeaderRow: GetComponentProps<readonly ColumnType<RecordType>[]>;
index: number;
}
const HeaderRow = <RecordType extends any>(props: RowProps<RecordType>) => {
const {
cells,
stickyOffsets,
flattenColumns,
rowComponent: RowComponent,
cellComponent: CellComponent,
onHeaderRow,
index,
} = props;
const { prefixCls, direction, headerCellRefs } = useContext(TableContext, [
'prefixCls',
'direction',
'headerCellRefs',
]);
let rowProps: React.HTMLAttributes<HTMLElement>;
if (onHeaderRow) {
rowProps = onHeaderRow(
cells.map(cell => cell.column),
index,
);
}
const columnsKey = getColumnsKey(cells.map(cell => cell.column));
const handleHeaderCellRef =
(cellIndex: number) => (headerCellRef: HTMLTableCellElement | null) => {
headerCellRefs.current[cellIndex] = headerCellRef;
};
return (
<RowComponent {...rowProps}>
{cells.map((cell: CellType<RecordType>, cellIndex) => {
const { column } = cell;
const fixedInfo = getCellFixedInfo(
cell.colStart,
cell.colEnd,
flattenColumns,
stickyOffsets,
direction,
);
let additionalProps: React.HTMLAttributes<HTMLElement>;
if (column && column.onHeaderCell) {
additionalProps = cell.column.onHeaderCell(column);
}
return (
<Cell
colIndex={cellIndex}
ref={handleHeaderCellRef(cellIndex)}
{...cell}
scope={column.title ? (cell.colSpan > 1 ? 'colgroup' : 'col') : null}
ellipsis={column.ellipsis}
align={column.align}
component={CellComponent}
prefixCls={prefixCls}
key={columnsKey[cellIndex]}
{...fixedInfo}
additionalProps={additionalProps}
rowType="header"
/>
);
})}
</RowComponent>
);
};
if (process.env.NODE_ENV !== 'production') {
HeaderRow.displayName = 'HeaderRow';
}
export default HeaderRow;