Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGridPro] Fix lazy loading error when existing rows are passed to replaceRows #10821

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/x-data-grid-pro/src/tests/lazyLoader.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,26 @@ describe('<DataGridPro /> - Lazy loader', () => {
expect(apiRef.current.getRowNode(4)).to.not.equal(null);
expect(apiRef.current.getRowNode(5)).to.not.equal(null);
});

it('should update rows when `apiRef.current.updateRows` with data reversed', () => {
render(<TestLazyLoader rowCount={5} autoHeight={isJSDOM} />);

const newRows: GridRowModel[] = [
{
id: 3,
first: 'Jim',
},
{
id: 2,
first: 'Jack',
},
{
id: 1,
first: 'Mike',
},
];

act(() => apiRef.current.unstable_replaceRows(0, newRows));
expect(getColumnValues(1)).to.deep.equal(['Jim', 'Jack', 'Mike']);
});
});
13 changes: 9 additions & 4 deletions packages/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export const useGridRows = (
const dataRowIdToIdLookup = { ...gridRowsDataRowIdToIdLookupSelector(apiRef) };
const rootGroup = tree[GRID_ROOT_GROUP_ID] as GridGroupNode;
const rootGroupChildren = [...rootGroup.children];
const seenIds = new Set<GridRowId>();

for (let i = 0; i < newRows.length; i += 1) {
const rowModel = newRows[i];
Expand All @@ -419,11 +420,13 @@ export const useGridRows = (
'A row was provided without id when calling replaceRows().',
);

const [replacedRowId] = rootGroupChildren.splice(firstRowToRender + i, 1, rowId);
const [removedRowId] = rootGroupChildren.splice(firstRowToRender + i, 1, rowId);

delete dataRowIdToModelLookup[replacedRowId];
delete dataRowIdToIdLookup[replacedRowId];
delete tree[replacedRowId];
if (!seenIds.has(removedRowId)) {
delete dataRowIdToModelLookup[removedRowId];
delete dataRowIdToIdLookup[removedRowId];
delete tree[removedRowId];
}

const rowTreeNodeConfig: GridLeafNode = {
id: rowId,
Expand All @@ -435,6 +438,8 @@ export const useGridRows = (
dataRowIdToModelLookup[rowId] = rowModel;
dataRowIdToIdLookup[rowId] = rowId;
tree[rowId] = rowTreeNodeConfig;

seenIds.add(rowId);
}

tree[GRID_ROOT_GROUP_ID] = { ...rootGroup, children: rootGroupChildren };
Expand Down
Loading