-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathHeaderRow.tsx
More file actions
107 lines (95 loc) · 2.96 KB
/
HeaderRow.tsx
File metadata and controls
107 lines (95 loc) · 2.96 KB
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
import { memo, useState } from 'react';
import { css } from 'ecij';
import { classnames, getColSpan } from './utils';
import type { CalculatedColumn, Direction, Maybe, Position, ResizedWidth } from './types';
import type { DataGridProps } from './DataGrid';
import HeaderCell from './HeaderCell';
import { cell, cellFrozen } from './style/cell';
type SharedDataGridProps<R, SR, K extends React.Key> = Pick<
DataGridProps<R, SR, K>,
'sortColumns' | 'onSortColumnsChange' | 'onColumnsReorder'
>;
export interface HeaderRowProps<R, SR, K extends React.Key> extends SharedDataGridProps<R, SR, K> {
rowIdx: number;
columns: readonly CalculatedColumn<R, SR>[];
onColumnResize: (column: CalculatedColumn<R, SR>, width: ResizedWidth) => void;
onColumnResizeEnd: () => void;
selectCell: (position: Position) => void;
lastFrozenColumnIndex: number;
selectedCellIdx: number | undefined;
shouldFocusGrid: boolean;
direction: Direction;
headerRowClass: Maybe<string>;
}
const headerRow = css`
@layer rdg.HeaderRow {
display: contents;
background-color: var(--rdg-header-background-color);
font-weight: bold;
& > .${cell} {
/* Should have a higher value than 1 to show up above regular cells and the focus sink */
z-index: 2;
position: sticky;
}
& > .${cellFrozen} {
z-index: 3;
}
}
`;
export const headerRowClassname = `rdg-header-row ${headerRow}`;
function HeaderRow<R, SR, K extends React.Key>({
headerRowClass,
rowIdx,
columns,
onColumnResize,
onColumnResizeEnd,
onColumnsReorder,
sortColumns,
onSortColumnsChange,
lastFrozenColumnIndex,
selectedCellIdx,
selectCell,
shouldFocusGrid,
direction
}: HeaderRowProps<R, SR, K>) {
const [draggedColumnKey, setDraggedColumnKey] = useState<string>();
const cells = [];
for (let index = 0; index < columns.length; index++) {
const column = columns[index];
const colSpan = getColSpan(column, lastFrozenColumnIndex, { type: 'HEADER' });
if (colSpan !== undefined) {
index += colSpan - 1;
}
cells.push(
<HeaderCell<R, SR>
key={column.key}
column={column}
colSpan={colSpan}
rowIdx={rowIdx}
isCellSelected={selectedCellIdx === column.idx}
onColumnResize={onColumnResize}
onColumnResizeEnd={onColumnResizeEnd}
onColumnsReorder={onColumnsReorder}
onSortColumnsChange={onSortColumnsChange}
sortColumns={sortColumns}
selectCell={selectCell}
shouldFocusGrid={shouldFocusGrid && index === 0}
direction={direction}
draggedColumnKey={draggedColumnKey}
setDraggedColumnKey={setDraggedColumnKey}
/>
);
}
return (
<div
role="row"
aria-rowindex={rowIdx} // aria-rowindex is 1 based
className={classnames(headerRowClassname, headerRowClass)}
>
{cells}
</div>
);
}
export default memo(HeaderRow) as <R, SR, K extends React.Key>(
props: HeaderRowProps<R, SR, K>
) => React.JSX.Element;