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] Cleanup pinned rows on removal (@cherniavskii) #15702

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
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