-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathuseCalculatedColumns.ts
289 lines (249 loc) · 9.25 KB
/
useCalculatedColumns.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { useMemo } from 'react';
import { max, min } from '../utils';
import type { CalculatedColumn, CalculatedColumnParent, ColumnOrColumnGroup, Omit } from '../types';
import { renderValue } from '../cellRenderers';
import { SELECT_COLUMN_KEY } from '../Columns';
import type { DataGridProps } from '../DataGrid';
import defaultRenderHeaderCell from '../renderHeaderCell';
type Mutable<T> = {
-readonly [P in keyof T]: T[P] extends ReadonlyArray<infer V> ? Mutable<V>[] : T[P];
};
interface WithParent<R, SR> {
readonly parent: MutableCalculatedColumnParent<R, SR> | undefined;
}
type MutableCalculatedColumnParent<R, SR> = Omit<Mutable<CalculatedColumnParent<R, SR>>, 'parent'> &
WithParent<R, SR>;
type MutableCalculatedColumn<R, SR> = Omit<Mutable<CalculatedColumn<R, SR>>, 'parent'> &
WithParent<R, SR>;
interface ColumnMetric {
width: number;
left: number;
}
const DEFAULT_COLUMN_WIDTH = 'auto';
const DEFAULT_COLUMN_MIN_WIDTH = 50;
interface CalculatedColumnsArgs<R, SR> {
rawColumns: readonly ColumnOrColumnGroup<R, SR>[];
defaultColumnOptions: DataGridProps<R, SR>['defaultColumnOptions'];
viewportWidth: number;
scrollLeft: number;
getColumnWidth: (column: CalculatedColumn<R, SR>) => string | number;
enableVirtualization: boolean;
}
export function useCalculatedColumns<R, SR>({
rawColumns,
defaultColumnOptions,
getColumnWidth,
viewportWidth,
scrollLeft,
enableVirtualization
}: CalculatedColumnsArgs<R, SR>) {
const defaultWidth = defaultColumnOptions?.width ?? DEFAULT_COLUMN_WIDTH;
const defaultMinWidth = defaultColumnOptions?.minWidth ?? DEFAULT_COLUMN_MIN_WIDTH;
const defaultMaxWidth = defaultColumnOptions?.maxWidth ?? undefined;
const defaultCellRenderer = defaultColumnOptions?.renderCell ?? renderValue;
const defaultHeaderCellRenderer =
defaultColumnOptions?.renderHeaderCell ?? defaultRenderHeaderCell;
const defaultSortable = defaultColumnOptions?.sortable ?? false;
const defaultResizable = defaultColumnOptions?.resizable ?? false;
const defaultDraggable = defaultColumnOptions?.draggable ?? false;
const { columns, colSpanColumns, lastFrozenColumnIndex, headerRowsCount } = useMemo((): {
readonly columns: readonly CalculatedColumn<R, SR>[];
readonly colSpanColumns: readonly CalculatedColumn<R, SR>[];
readonly lastFrozenColumnIndex: number;
readonly headerRowsCount: number;
} => {
let lastFrozenColumnIndex = -1;
let headerRowsCount = 1;
const columns: MutableCalculatedColumn<R, SR>[] = [];
collectColumns(rawColumns, 1);
function collectColumns(
rawColumns: readonly ColumnOrColumnGroup<R, SR>[],
level: number,
parent?: MutableCalculatedColumnParent<R, SR>
) {
for (const rawColumn of rawColumns) {
if ('children' in rawColumn) {
const calculatedColumnParent: MutableCalculatedColumnParent<R, SR> = {
name: rawColumn.name,
parent,
idx: -1,
colSpan: 0,
level: 0,
headerCellClass: rawColumn.headerCellClass
};
collectColumns(rawColumn.children, level + 1, calculatedColumnParent);
continue;
}
const frozen = rawColumn.frozen ?? false;
const column: MutableCalculatedColumn<R, SR> = {
...rawColumn,
parent,
idx: 0,
level: 0,
frozen,
width: rawColumn.width ?? defaultWidth,
minWidth: rawColumn.minWidth ?? defaultMinWidth,
maxWidth: rawColumn.maxWidth ?? defaultMaxWidth,
sortable: rawColumn.sortable ?? defaultSortable,
resizable: rawColumn.resizable ?? defaultResizable,
draggable: rawColumn.draggable ?? defaultDraggable,
renderCell: rawColumn.renderCell ?? defaultCellRenderer,
renderHeaderCell: rawColumn.renderHeaderCell ?? defaultHeaderCellRenderer
};
columns.push(column);
if (frozen) {
lastFrozenColumnIndex++;
}
if (level > headerRowsCount) {
headerRowsCount = level;
}
}
}
columns.sort(({ key: aKey, frozen: frozenA }, { key: bKey, frozen: frozenB }) => {
// Sort select column first:
if (aKey === SELECT_COLUMN_KEY) return -1;
if (bKey === SELECT_COLUMN_KEY) return 1;
// Sort frozen columns second:
if (frozenA) {
if (frozenB) return 0;
return -1;
}
if (frozenB) return 1;
// TODO: sort columns to keep them grouped if they have a parent
// Sort other columns last:
return 0;
});
const colSpanColumns: CalculatedColumn<R, SR>[] = [];
columns.forEach((column, idx) => {
column.idx = idx;
updateColumnParent(column, idx, 0);
if (column.colSpan != null) {
colSpanColumns.push(column);
}
});
return {
columns,
colSpanColumns,
lastFrozenColumnIndex,
headerRowsCount
};
}, [
rawColumns,
defaultWidth,
defaultMinWidth,
defaultMaxWidth,
defaultCellRenderer,
defaultHeaderCellRenderer,
defaultResizable,
defaultSortable,
defaultDraggable
]);
const { templateColumns, layoutCssVars, totalFrozenColumnWidth, columnMetrics } = useMemo((): {
templateColumns: readonly string[];
layoutCssVars: Readonly<Record<string, string>>;
totalFrozenColumnWidth: number;
columnMetrics: ReadonlyMap<CalculatedColumn<R, SR>, ColumnMetric>;
} => {
const columnMetrics = new Map<CalculatedColumn<R, SR>, ColumnMetric>();
let left = 0;
let totalFrozenColumnWidth = 0;
const templateColumns: string[] = [];
for (const column of columns) {
let width = getColumnWidth(column);
if (typeof width === 'string') {
// This is a placeholder width so we can continue to use virtualization.
// The actual value is set after the column is rendered
width = column.minWidth;
}
templateColumns.push(`${width}px`);
columnMetrics.set(column, { width, left });
left += width;
}
if (lastFrozenColumnIndex !== -1) {
const columnMetric = columnMetrics.get(columns[lastFrozenColumnIndex])!;
totalFrozenColumnWidth = columnMetric.left + columnMetric.width;
}
const layoutCssVars: Record<string, string> = {};
for (let i = 0; i <= lastFrozenColumnIndex; i++) {
const column = columns[i];
layoutCssVars[`--rdg-frozen-left-${column.idx}`] = `${columnMetrics.get(column)!.left}px`;
}
return { templateColumns, layoutCssVars, totalFrozenColumnWidth, columnMetrics };
}, [getColumnWidth, columns, lastFrozenColumnIndex]);
const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => {
if (!enableVirtualization) {
return [0, columns.length - 1];
}
// get the viewport's left side and right side positions for non-frozen columns
const viewportLeft = scrollLeft + totalFrozenColumnWidth;
const viewportRight = scrollLeft + viewportWidth;
// get first and last non-frozen column indexes
const lastColIdx = columns.length - 1;
const firstUnfrozenColumnIdx = min(lastFrozenColumnIndex + 1, lastColIdx);
// skip rendering non-frozen columns if the frozen columns cover the entire viewport
if (viewportLeft >= viewportRight) {
return [firstUnfrozenColumnIdx, firstUnfrozenColumnIdx];
}
// get the first visible non-frozen column index
let colVisibleStartIdx = firstUnfrozenColumnIdx;
while (colVisibleStartIdx < lastColIdx) {
const { left, width } = columnMetrics.get(columns[colVisibleStartIdx])!;
// if the right side of the columnn is beyond the left side of the available viewport,
// then it is the first column that's at least partially visible
if (left + width > viewportLeft) {
break;
}
colVisibleStartIdx++;
}
// get the last visible non-frozen column index
let colVisibleEndIdx = colVisibleStartIdx;
while (colVisibleEndIdx < lastColIdx) {
const { left, width } = columnMetrics.get(columns[colVisibleEndIdx])!;
// if the right side of the column is beyond or equal to the right side of the available viewport,
// then it the last column that's at least partially visible, as the previous column's right side is not beyond the viewport.
if (left + width >= viewportRight) {
break;
}
colVisibleEndIdx++;
}
const colOverscanStartIdx = max(firstUnfrozenColumnIdx, colVisibleStartIdx - 1);
const colOverscanEndIdx = min(lastColIdx, colVisibleEndIdx + 1);
return [colOverscanStartIdx, colOverscanEndIdx];
}, [
columnMetrics,
columns,
lastFrozenColumnIndex,
scrollLeft,
totalFrozenColumnWidth,
viewportWidth,
enableVirtualization
]);
return {
columns,
colSpanColumns,
colOverscanStartIdx,
colOverscanEndIdx,
templateColumns,
layoutCssVars,
headerRowsCount,
lastFrozenColumnIndex,
totalFrozenColumnWidth
};
}
function updateColumnParent<R, SR>(
column: MutableCalculatedColumn<R, SR> | MutableCalculatedColumnParent<R, SR>,
index: number,
level: number
) {
if (level < column.level) {
column.level = level;
}
if (column.parent !== undefined) {
const { parent } = column;
if (parent.idx === -1) {
parent.idx = index;
}
parent.colSpan += 1;
updateColumnParent(parent, index, level - 1);
}
}