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

[DataGrid] Fix an error when deleting pinned row using actions column button #7767

Merged
merged 1 commit into from
Feb 1, 2023
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 @@ -6,7 +6,7 @@ import {
} from '@mui/x-data-grid/internals';
import { GridRowEntry, GridRowId, GridRowModel } from '@mui/x-data-grid';
import { GridApiPro } from '../../../models/gridApiPro';
import { GridPinnedRowsProp } from './gridRowPinningInterface';
import type { GridPinnedRowsProp, GridRowPinningInternalCache } from './gridRowPinningInterface';

type GridPinnedRowPosition = keyof GridPinnedRowsProp;

Expand Down Expand Up @@ -63,11 +63,35 @@ export function addPinnedRow({
};
}

export function removePinnedRow({
groupingParams,
rowId,
apiRef,
}: {
groupingParams: GridHydrateRowsValue;
rowId: GridRowId;
apiRef: React.MutableRefObject<GridApiPro>;
}) {
const idRowsLookup = { ...groupingParams.idRowsLookup };
const tree = { ...groupingParams.tree };

delete idRowsLookup[rowId];
delete tree[rowId];

delete apiRef.current.unstable_caches.rows.idRowsLookup[rowId];
delete apiRef.current.unstable_caches.rows.idToIdLookup[rowId];
}

export const useGridRowPinningPreProcessors = (apiRef: React.MutableRefObject<GridApiPro>) => {
const previousPinnedRowsCacheRef = React.useRef<GridRowPinningInternalCache | null>(null);

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

const previousPinnedRowsCache = previousPinnedRowsCacheRef.current;
previousPinnedRowsCacheRef.current = pinnedRowsCache;

let newGroupingParams = {
...groupingParams,
additionalRowGroups: {
Expand All @@ -77,6 +101,24 @@ export const useGridRowPinningPreProcessors = (apiRef: React.MutableRefObject<Gr
},
};

if (previousPinnedRowsCache) {
previousPinnedRowsCache.topIds?.forEach((rowId) => {
removePinnedRow({
groupingParams: newGroupingParams,
rowId,
apiRef,
});
});

previousPinnedRowsCache.bottomIds?.forEach((rowId) => {
removePinnedRow({
groupingParams: newGroupingParams,
rowId,
apiRef,
});
});
}

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