-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathuseGridRowSpanning.ts
356 lines (326 loc) · 11.6 KB
/
useGridRowSpanning.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import * as React from 'react';
import useLazyRef from '@mui/utils/useLazyRef';
import { GRID_DETAIL_PANEL_TOGGLE_FIELD } from '../../../internals/constants';
import { gridVisibleColumnDefinitionsSelector } from '../columns/gridColumnsSelector';
import { useGridVisibleRows } from '../../utils/useGridVisibleRows';
import { gridRenderContextSelector } from '../virtualization/gridVirtualizationSelectors';
import { useGridSelector } from '../../utils/useGridSelector';
import type { GridColDef } from '../../../models/colDef';
import type { GridRowId, GridValidRowModel, GridRowEntry } from '../../../models/gridRows';
import type { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import type { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import type { GridStateInitializer } from '../../utils/useGridInitializeState';
import {
getUnprocessedRange,
isRowRangeUpdated,
isRowContextInitialized,
getCellValue,
} from './gridRowSpanningUtils';
import { GRID_CHECKBOX_SELECTION_FIELD } from '../../../colDef/gridCheckboxSelectionColDef';
export interface GridRowSpanningState {
spannedCells: Record<GridRowId, Record<GridColDef['field'], number>>;
hiddenCells: Record<GridRowId, Record<GridColDef['field'], boolean>>;
/**
* For each hidden cell, it contains the row index corresponding to the cell that is
* the origin of the hidden cell. i.e. the cell which is spanned.
* Used by the virtualization to properly keep the spanned cells in view.
*/
hiddenCellOriginMap: Record<number, Record<GridColDef['field'], number>>;
}
export type RowRange = { firstRowIndex: number; lastRowIndex: number };
const EMPTY_STATE = { spannedCells: {}, hiddenCells: {}, hiddenCellOriginMap: {} };
const EMPTY_RANGE: RowRange = { firstRowIndex: 0, lastRowIndex: 0 };
const skippedFields = new Set([
GRID_CHECKBOX_SELECTION_FIELD,
'__reorder__',
GRID_DETAIL_PANEL_TOGGLE_FIELD,
]);
/**
* Default number of rows to process during state initialization to avoid flickering.
* Number `20` is arbitrarily chosen to be large enough to cover most of the cases without
* compromising performance.
*/
const DEFAULT_ROWS_TO_PROCESS = 20;
const computeRowSpanningState = (
apiRef: React.MutableRefObject<GridPrivateApiCommunity>,
colDefs: GridColDef[],
visibleRows: GridRowEntry<GridValidRowModel>[],
range: RowRange,
rangeToProcess: RowRange,
resetState: boolean,
processedRange: RowRange,
) => {
const spannedCells = resetState ? {} : { ...apiRef.current.state.rowSpanning.spannedCells };
const hiddenCells = resetState ? {} : { ...apiRef.current.state.rowSpanning.hiddenCells };
const hiddenCellOriginMap = resetState
? {}
: { ...apiRef.current.state.rowSpanning.hiddenCellOriginMap };
if (resetState) {
processedRange = EMPTY_RANGE;
}
colDefs.forEach((colDef) => {
if (skippedFields.has(colDef.field)) {
return;
}
for (
let index = rangeToProcess.firstRowIndex;
index <= rangeToProcess.lastRowIndex;
index += 1
) {
const row = visibleRows[index];
if (hiddenCells[row.id]?.[colDef.field]) {
continue;
}
const cellValue = getCellValue(row.model, colDef, apiRef);
if (cellValue == null) {
continue;
}
let spannedRowId = row.id;
let spannedRowIndex = index;
let rowSpan = 0;
// For first index, also scan in the previous rows to handle the reset state case e.g by sorting
const backwardsHiddenCells: number[] = [];
if (index === rangeToProcess.firstRowIndex) {
let prevIndex = index - 1;
const prevRowEntry = visibleRows[prevIndex];
while (
prevIndex >= range.firstRowIndex &&
getCellValue(prevRowEntry.model, colDef, apiRef) === cellValue
) {
const currentRow = visibleRows[prevIndex + 1];
if (hiddenCells[currentRow.id]) {
hiddenCells[currentRow.id][colDef.field] = true;
} else {
hiddenCells[currentRow.id] = { [colDef.field]: true };
}
backwardsHiddenCells.push(index);
rowSpan += 1;
spannedRowId = prevRowEntry.id;
spannedRowIndex = prevIndex;
prevIndex -= 1;
}
}
backwardsHiddenCells.forEach((hiddenCellIndex) => {
if (hiddenCellOriginMap[hiddenCellIndex]) {
hiddenCellOriginMap[hiddenCellIndex][colDef.field] = spannedRowIndex;
} else {
hiddenCellOriginMap[hiddenCellIndex] = { [colDef.field]: spannedRowIndex };
}
});
// Scan the next rows
let relativeIndex = index + 1;
while (
relativeIndex <= range.lastRowIndex &&
visibleRows[relativeIndex] &&
getCellValue(visibleRows[relativeIndex].model, colDef, apiRef) === cellValue
) {
const currentRow = visibleRows[relativeIndex];
if (hiddenCells[currentRow.id]) {
hiddenCells[currentRow.id][colDef.field] = true;
} else {
hiddenCells[currentRow.id] = { [colDef.field]: true };
}
if (hiddenCellOriginMap[relativeIndex]) {
hiddenCellOriginMap[relativeIndex][colDef.field] = spannedRowIndex;
} else {
hiddenCellOriginMap[relativeIndex] = { [colDef.field]: spannedRowIndex };
}
relativeIndex += 1;
rowSpan += 1;
}
if (rowSpan > 0) {
if (spannedCells[spannedRowId]) {
spannedCells[spannedRowId][colDef.field] = rowSpan + 1;
} else {
spannedCells[spannedRowId] = { [colDef.field]: rowSpan + 1 };
}
}
}
processedRange = {
firstRowIndex: Math.min(processedRange.firstRowIndex, rangeToProcess.firstRowIndex),
lastRowIndex: Math.max(processedRange.lastRowIndex, rangeToProcess.lastRowIndex),
};
});
return { spannedCells, hiddenCells, hiddenCellOriginMap, processedRange };
};
/**
* @requires columnsStateInitializer (method) - should be initialized before
* @requires rowsStateInitializer (method) - should be initialized before
* @requires filterStateInitializer (method) - should be initialized before
*/
export const rowSpanningStateInitializer: GridStateInitializer = (state, props, apiRef) => {
if (props.unstable_rowSpanning) {
const rowIds = state.rows!.dataRowIds || [];
const orderedFields = state.columns!.orderedFields || [];
const dataRowIdToModelLookup = state.rows!.dataRowIdToModelLookup;
const columnsLookup = state.columns!.lookup;
const isFilteringPending =
Boolean(state.filter!.filterModel!.items!.length) ||
Boolean(state.filter!.filterModel!.quickFilterValues?.length);
if (
!rowIds.length ||
!orderedFields.length ||
!dataRowIdToModelLookup ||
!columnsLookup ||
isFilteringPending
) {
return {
...state,
rowSpanning: EMPTY_STATE,
};
}
const rangeToProcess = {
firstRowIndex: 0,
lastRowIndex: Math.min(DEFAULT_ROWS_TO_PROCESS - 1, Math.max(rowIds.length - 1, 0)),
};
const rows = rowIds.map((id) => ({
id,
model: dataRowIdToModelLookup[id!],
})) as GridRowEntry<GridValidRowModel>[];
const colDefs = orderedFields.map((field) => columnsLookup[field!]) as GridColDef[];
const { spannedCells, hiddenCells, hiddenCellOriginMap } = computeRowSpanningState(
apiRef,
colDefs,
rows,
rangeToProcess,
rangeToProcess,
true,
EMPTY_RANGE,
);
return {
...state,
rowSpanning: {
spannedCells,
hiddenCells,
hiddenCellOriginMap,
},
};
}
return {
...state,
rowSpanning: EMPTY_STATE,
};
};
export const useGridRowSpanning = (
apiRef: React.MutableRefObject<GridPrivateApiCommunity>,
props: Pick<DataGridProcessedProps, 'unstable_rowSpanning' | 'pagination' | 'paginationMode'>,
): void => {
const { range, rows: visibleRows } = useGridVisibleRows(apiRef, props);
const renderContext = useGridSelector(apiRef, gridRenderContextSelector);
const colDefs = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
const processedRange = useLazyRef<RowRange, void>(() => {
return Object.keys(apiRef.current.state.rowSpanning.spannedCells).length > 0
? {
firstRowIndex: 0,
lastRowIndex: Math.min(
DEFAULT_ROWS_TO_PROCESS - 1,
Math.max(apiRef.current.state.rows.dataRowIds.length - 1, 0),
),
}
: EMPTY_RANGE;
});
const lastRange = React.useRef<RowRange>(EMPTY_RANGE);
const updateRowSpanningState = React.useCallback(
// A reset needs to occur when:
// - The `unstable_rowSpanning` prop is updated (feature flag)
// - The filtering is applied
// - The sorting is applied
// - The `paginationModel` is updated
// - The rows are updated
(resetState: boolean = true) => {
if (!props.unstable_rowSpanning) {
if (apiRef.current.state.rowSpanning !== EMPTY_STATE) {
apiRef.current.setState((state) => ({ ...state, rowSpanning: EMPTY_STATE }));
}
return;
}
if (range === null || !isRowContextInitialized(renderContext)) {
return;
}
if (resetState) {
processedRange.current = EMPTY_RANGE;
}
const rangeToProcess = getUnprocessedRange(
{
firstRowIndex: renderContext.firstRowIndex,
lastRowIndex: Math.min(renderContext.lastRowIndex - 1, range.lastRowIndex),
},
processedRange.current,
);
if (rangeToProcess === null) {
return;
}
const {
spannedCells,
hiddenCells,
hiddenCellOriginMap,
processedRange: newProcessedRange,
} = computeRowSpanningState(
apiRef,
colDefs,
visibleRows,
range,
rangeToProcess,
resetState,
processedRange.current,
);
processedRange.current = newProcessedRange;
const newSpannedCellsCount = Object.keys(spannedCells).length;
const newHiddenCellsCount = Object.keys(hiddenCells).length;
const currentSpannedCellsCount = Object.keys(
apiRef.current.state.rowSpanning.spannedCells,
).length;
const currentHiddenCellsCount = Object.keys(
apiRef.current.state.rowSpanning.hiddenCells,
).length;
const shouldUpdateState =
resetState ||
newSpannedCellsCount !== currentSpannedCellsCount ||
newHiddenCellsCount !== currentHiddenCellsCount;
if (!shouldUpdateState) {
return;
}
apiRef.current.setState((state) => {
return {
...state,
rowSpanning: {
spannedCells,
hiddenCells,
hiddenCellOriginMap,
},
};
});
},
[
apiRef,
props.unstable_rowSpanning,
range,
renderContext,
visibleRows,
colDefs,
processedRange,
],
);
const prevRenderContext = React.useRef(renderContext);
const isFirstRender = React.useRef(true);
const shouldResetState = React.useRef(false);
React.useEffect(() => {
const firstRender = isFirstRender.current;
if (isFirstRender.current) {
isFirstRender.current = false;
}
if (range && lastRange.current && isRowRangeUpdated(range, lastRange.current)) {
lastRange.current = range;
shouldResetState.current = true;
}
if (!firstRender && prevRenderContext.current !== renderContext) {
if (isRowRangeUpdated(prevRenderContext.current, renderContext)) {
updateRowSpanningState(shouldResetState.current);
shouldResetState.current = false;
}
prevRenderContext.current = renderContext;
return;
}
updateRowSpanningState();
}, [updateRowSpanningState, renderContext, range, lastRange]);
};