Skip to content

Commit

Permalink
[DataGridPro] Cleanup pinned rows on removal (#15697)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii authored Dec 2, 2024
1 parent 01fbad7 commit 65ca208
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
GridRowModel,
} from '@mui/x-data-grid';
import { GridPrivateApiPro } from '../../../models/gridApiPro';
import { GridPinnedRowsProp } from './gridRowPinningInterface';
import type { GridPinnedRowsProp, GridRowPinningInternalCache } from './gridRowPinningInterface';
import { insertNodeInTree } from '../../../utils/tree/utils';

type GridPinnedRowPosition = keyof GridPinnedRowsProp;
Expand Down Expand Up @@ -92,9 +92,13 @@ export function addPinnedRow({
export const useGridRowPinningPreProcessors = (
apiRef: React.MutableRefObject<GridPrivateApiPro>,
) => {
const prevPinnedRowsCacheRef = React.useRef<GridRowPinningInternalCache | null>(null);

const addPinnedRows = React.useCallback<GridPipeProcessor<'hydrateRows'>>(
(groupingParams) => {
const pinnedRowsCache = apiRef.current.caches.pinnedRows || {};
const prevPinnedRowsCache = prevPinnedRowsCacheRef.current;
prevPinnedRowsCacheRef.current = pinnedRowsCache;

let newGroupingParams: GridHydrateRowsValue = {
...groupingParams,
Expand All @@ -105,6 +109,22 @@ export const useGridRowPinningPreProcessors = (
},
};

if (prevPinnedRowsCache) {
const pinnedRowCleanup = (rowId: GridRowId) => {
const node = newGroupingParams.tree[rowId];
if (node?.type === 'pinnedRow') {
delete newGroupingParams.tree[rowId];
delete newGroupingParams.dataRowIdToModelLookup[rowId];
delete newGroupingParams.dataRowIdToIdLookup[rowId];
delete apiRef.current.caches.rows.dataRowIdToIdLookup[rowId];
delete apiRef.current.caches.rows.dataRowIdToModelLookup[rowId];
}
};

prevPinnedRowsCache.topIds?.forEach(pinnedRowCleanup);
prevPinnedRowsCache.bottomIds?.forEach(pinnedRowCleanup);
}

pinnedRowsCache.topIds?.forEach((rowId) => {
newGroupingParams = addPinnedRow({
groupingParams: newGroupingParams,
Expand Down

0 comments on commit 65ca208

Please sign in to comment.