Skip to content

Commit

Permalink
[DataGridPro] Fix useGridRows not giving error on reversed data
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn-basesoft committed Oct 27, 2023
1 parent 2cbc8df commit b536b83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,28 @@ 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']);
});

});
11 changes: 8 additions & 3 deletions packages/grid/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: GridRowId[] = [];

for (let i = 0; i < newRows.length; i += 1) {
const rowModel = newRows[i];
Expand All @@ -421,9 +422,11 @@ export const useGridRows = (

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

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

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

seenIds.push(rowId);
}

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

0 comments on commit b536b83

Please sign in to comment.