-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridWithStickyCells.tsx
163 lines (144 loc) · 5.71 KB
/
GridWithStickyCells.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { ForwardRefRenderFunction } from "react";
import { FixedSizeGrid, FixedSizeGrid as Grid, FixedSizeGridProps } from "react-window";
import CopyIndicator from "./CopyIndicator";
import DragIndicator from "./DragIndicator";
function getCellIndices(child: any) {
return { row: child.props.rowIndex, column: child.props.columnIndex };
}
function getShownIndices(children: React.ReactNode) {
let minRow = Infinity;
let maxRow = -Infinity;
let minColumn = Infinity;
let maxColumn = -Infinity;
React.Children.forEach(children, (child) => {
const { row, column } = getCellIndices(child);
minRow = Math.min(minRow, row);
maxRow = Math.max(maxRow, row);
minColumn = Math.min(minColumn, column);
maxColumn = Math.max(maxColumn, column);
});
return {
from: {
row: minRow,
column: minColumn
},
to: {
row: maxRow,
column: maxColumn
}
};
}
function useInnerElementType(Cell: any, columnWidth: number, rowHeight: number, data: number) {
return React.useMemo(
() =>
React.forwardRef((props, ref) => {
const sumWidthOrHeight = (index: number, multiplier: number) => Math.max(0, (index - 1) * multiplier);
const shownIndices = getShownIndices(props.children);
const children = React.Children.map(props.children, (child) => {
const { column, row } = getCellIndices(child);
// do not show non-sticky cell
if (column === 0 || row === 0) {
return null;
}
return child;
});
// @ts-ignore
children.push(
React.createElement(Cell, {
key: "0:0",
rowIndex: 0,
columnIndex: 0,
style: {
display: "inline-flex",
width: columnWidth,
height: rowHeight,
position: "sticky",
top: 0,
left: 0,
zIndex: 100000
}
})
);
const shownColumnsCount =
shownIndices.to.column - shownIndices.from.column;
for (let i = 1; i <= shownColumnsCount; i += 1) {
const columnIndex = i + shownIndices.from.column;
const rowIndex = 0;
const marginLeft =
i === 1 ? sumWidthOrHeight(columnIndex, columnWidth) : undefined;
// @ts-ignore
children.push(
React.createElement(Cell, {
key: `${rowIndex}:${columnIndex}`,
rowIndex,
columnIndex,
style: {
marginLeft,
display: "inline-flex",
justifyContent: "center",
width: columnWidth,
height: rowHeight,
position: "sticky",
top: 0,
zIndex: 99999
}
})
);
}
const shownRowsCount =
shownIndices.to.row - shownIndices.from.row;
for (let i = 1; i <= shownRowsCount; i += 1) {
const columnIndex = 0;
const rowIndex = i + shownIndices.from.row;
const marginTop =
i === 1 ? sumWidthOrHeight(rowIndex, rowHeight) : undefined;
// @ts-ignore
children.push(
React.createElement(Cell, {
key: `${rowIndex}:${columnIndex}`,
rowIndex,
columnIndex,
style: {
marginTop,
...(rowIndex === data + 1 ? {
width: "max-content"
} : {
rowHeight,
columnWidth,
justifyContent: "center",
maxWidth: columnWidth + "px"
}),
position: "sticky",
left: 0,
zIndex: 99998,
display: "flex"
},
data
})
);
}
return (
//@ts-ignore
<div ref={ref} {...props}>
{children}
<DragIndicator />
<CopyIndicator />
</div>
);
}),
[Cell, columnWidth, rowHeight, data]
);
}
const GridWithStickyCellsForwardRef: ForwardRefRenderFunction<FixedSizeGrid, FixedSizeGridProps> = (props, ref) => (
<Grid
ref={ref}
{...props}
innerElementType={useInnerElementType(
props.children,
props.columnWidth,
props.rowHeight,
props.itemData
)}
/>
);
export const GridWithStickyCells = React.forwardRef(GridWithStickyCellsForwardRef);