Skip to content

Commit

Permalink
[DataGrid] Trigger row spanning computation on rows update (mui#15858)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored and lauri865 committed Dec 19, 2024
1 parent 51baac0 commit a0716b4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/x-data-grid/src/hooks/features/rows/useGridRowSpanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { gridVisibleColumnDefinitionsSelector } from '../columns/gridColumnsSele
import { useGridVisibleRows } from '../../utils/useGridVisibleRows';
import { gridRenderContextSelector } from '../virtualization/gridVirtualizationSelectors';
import { useGridSelector } from '../../utils/useGridSelector';
import { gridRowTreeSelector } from './gridRowsSelector';
import type { GridColDef } from '../../../models/colDef';
import type { GridRowId, GridValidRowModel, GridRowEntry } from '../../../models/gridRows';
import type { DataGridProcessedProps } from '../../../models/props/DataGridProps';
Expand Down Expand Up @@ -71,7 +72,7 @@ const computeRowSpanningState = (

for (
let index = rangeToProcess.firstRowIndex;
index <= rangeToProcess.lastRowIndex;
index < rangeToProcess.lastRowIndex;
index += 1
) {
const row = visibleRows[index];
Expand Down Expand Up @@ -187,7 +188,7 @@ export const rowSpanningStateInitializer: GridStateInitializer = (state, props,
}
const rangeToProcess = {
firstRowIndex: 0,
lastRowIndex: Math.min(DEFAULT_ROWS_TO_PROCESS - 1, Math.max(rowIds.length - 1, 0)),
lastRowIndex: Math.min(DEFAULT_ROWS_TO_PROCESS, Math.max(rowIds.length, 0)),
};
const rows = rowIds.map((id) => ({
id,
Expand Down Expand Up @@ -226,13 +227,14 @@ export const useGridRowSpanning = (
const { range, rows: visibleRows } = useGridVisibleRows(apiRef, props);
const renderContext = useGridSelector(apiRef, gridRenderContextSelector);
const colDefs = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
const tree = useGridSelector(apiRef, gridRowTreeSelector);
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),
DEFAULT_ROWS_TO_PROCESS,
Math.max(apiRef.current.state.rows.dataRowIds.length, 0),
),
}
: EMPTY_RANGE;
Expand Down Expand Up @@ -265,7 +267,7 @@ export const useGridRowSpanning = (
const rangeToProcess = getUnprocessedRange(
{
firstRowIndex: renderContext.firstRowIndex,
lastRowIndex: Math.min(renderContext.lastRowIndex - 1, range.lastRowIndex),
lastRowIndex: Math.min(renderContext.lastRowIndex, range.lastRowIndex + 1),
},
processedRange.current,
);
Expand Down Expand Up @@ -326,11 +328,17 @@ export const useGridRowSpanning = (
const prevRenderContext = React.useRef(renderContext);
const isFirstRender = React.useRef(true);
const shouldResetState = React.useRef(false);
const previousTree = React.useRef(tree);
React.useEffect(() => {
const firstRender = isFirstRender.current;
if (isFirstRender.current) {
isFirstRender.current = false;
}
if (tree !== previousTree.current) {
previousTree.current = tree;
updateRowSpanningState(true);
return;
}
if (range && lastRange.current && isRowRangeUpdated(range, lastRange.current)) {
lastRange.current = range;
shouldResetState.current = true;
Expand All @@ -344,5 +352,5 @@ export const useGridRowSpanning = (
return;
}
updateRowSpanningState();
}, [updateRowSpanningState, renderContext, range, lastRange]);
}, [updateRowSpanningState, renderContext, range, lastRange, tree]);
};
32 changes: 32 additions & 0 deletions packages/x-data-grid/src/tests/rowSpanning.DataGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { createRenderer, fireEvent, act } from '@mui/internal-test-utils';
import { expect } from 'chai';
import { spy } from 'sinon';
import { DataGrid, useGridApiRef, DataGridProps, GridApi } from '@mui/x-data-grid';
import { getCell, getActiveCell } from 'test/utils/helperFn';

Expand Down Expand Up @@ -247,5 +248,36 @@ describe('<DataGrid /> - Row spanning', () => {
});
});

describe('rows update', () => {
it('should update the row spanning state when the rows are updated', () => {
const rowSpanValueGetter = spy();
let rowSpanningStateUpdates = 0;
let spannedCells = {};
render(
<TestDataGrid
columns={[{ field: 'code', rowSpanValueGetter }]}
rows={[{ id: 1, code: 'A101' }]}
onStateChange={(newState) => {
const newSpannedCells = newState.rowSpanning.spannedCells;
if (newSpannedCells !== spannedCells) {
rowSpanningStateUpdates += 1;
spannedCells = newSpannedCells;
}
}}
/>,
);

// First update by initializer
expect(rowSpanningStateUpdates).to.equal(1);

act(() => {
apiRef.current.setRows([{ id: 1, code: 'A101' }]);
});

// Second update on row update
expect(rowSpanningStateUpdates).to.equal(2);
});
});

// TODO: Add tests for row reordering
});

0 comments on commit a0716b4

Please sign in to comment.