Skip to content

Commit

Permalink
Remove dataRowIdToIdLookup selector
Browse files Browse the repository at this point in the history
  • Loading branch information
arminmeh committed Dec 2, 2024
1 parent 270e1ae commit 8383e25
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ Below are described the steps you need to make to migrate from v7 to v8.

- The `apiRef.current.resize()` method was removed.
- The `<GridOverlays />` component is not exported anymore.
- `dataRowIdToIdLookup` selector is removed. Use `dataRowIdToModelLookup` selector in combination with `getRowId()` API method instead.

```tsx
// Before
const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef);
...
const rowId = idToIdLookup[id]

// After
const rowsLookup = gridRowsLookupSelector(apiRef);
...
const rowId = apiRef.current.getRowId(rowsLookup[id])
```

<!-- ### Accessibility
Expand Down
6 changes: 0 additions & 6 deletions docs/pages/x/api/data-grid/selectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,6 @@
"description": "",
"supportsApiRef": true
},
{
"name": "gridRowsDataRowIdToIdLookupSelector",
"returnType": "GridRowIdToIdLookup",
"description": "",
"supportsApiRef": true
},
{
"name": "gridRowsLoadingSelector",
"returnType": "boolean | undefined",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
GRID_CHECKBOX_SELECTION_COL_DEF,
GRID_DETAIL_PANEL_TOGGLE_FIELD,
GridCellCoordinates,
gridRowsDataRowIdToIdLookupSelector,
gridRowsLookupSelector,
GridRowId,
gridClasses,
gridFocusCellSelector,
Expand Down Expand Up @@ -168,7 +168,7 @@ export const useGridCellSelection = (
GridCellSelectionApi['getSelectedCellsAsArray']
>(() => {
const selectionModel = apiRef.current.getCellSelectionModel();
const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef);
const rowsLookup = gridRowsLookupSelector(apiRef);
const currentVisibleRows = getVisibleRows(apiRef, props);
const sortedEntries = currentVisibleRows.rows.reduce(
(result, row) => {
Expand All @@ -186,7 +186,7 @@ export const useGridCellSelection = (
...Object.entries(fields).reduce<{ id: GridRowId; field: string }[]>(
(selectedFields, [field, isSelected]) => {
if (isSelected) {
selectedFields.push({ id: idToIdLookup[id], field });
selectedFields.push({ id: apiRef.current.getRowId(rowsLookup[id]), field });
}
return selectedFields;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function addPinnedRow({
isAutoGenerated: boolean;
}) {
const dataRowIdToModelLookup = { ...groupingParams.dataRowIdToModelLookup };
const dataRowIdToIdLookup = { ...groupingParams.dataRowIdToIdLookup };
const tree = { ...groupingParams.tree };
const treeDepths = { ...groupingParams.treeDepths };

Expand All @@ -52,12 +51,10 @@ export function addPinnedRow({

if (!isAutoGenerated) {
dataRowIdToModelLookup[rowId] = rowModel;
dataRowIdToIdLookup[rowId] = rowId;
}
// Do not push it to ids list so that pagination is not affected by pinned rows

apiRef.current.caches.rows.dataRowIdToModelLookup[rowId] = { ...rowModel };
apiRef.current.caches.rows.dataRowIdToIdLookup[rowId] = rowId;

const previousPinnedRows = groupingParams.additionalRowGroups?.pinnedRows || {};

Expand All @@ -67,7 +64,6 @@ export function addPinnedRow({
return {
...groupingParams,
dataRowIdToModelLookup,
dataRowIdToIdLookup,
tree,
treeDepths,
};
Expand All @@ -76,7 +72,6 @@ export function addPinnedRow({
return {
...groupingParams,
dataRowIdToModelLookup,
dataRowIdToIdLookup,
tree,
treeDepths,
additionalRowGroups: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { gridEditRowsStateSelector } from './gridEditingSelectors';
import { GridRowId } from '../../../models/gridRows';
import { isPrintableKey, isPasteShortcut } from '../../../utils/keyboardUtils';
import { gridRowsDataRowIdToIdLookupSelector } from '../rows/gridRowsSelector';
import { gridRowsLookupSelector } from '../rows/gridRowsSelector';
import { deepClone } from '../../../utils/utils';
import {
GridCellEditStartParams,
Expand Down Expand Up @@ -560,7 +560,7 @@ export const useGridCellEditing = (

// Run this effect synchronously so that the keyboard event can impact the yet-to-be-rendered input.
useEnhancedEffect(() => {
const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef);
const rowsLookup = gridRowsLookupSelector(apiRef);

// Update the ref here because updateStateToStopCellEditMode may change it later
const copyOfPrevCellModes = prevCellModesModel.current;
Expand All @@ -569,7 +569,7 @@ export const useGridCellEditing = (
Object.entries(cellModesModel).forEach(([id, fields]) => {
Object.entries(fields).forEach(([field, params]) => {
const prevMode = copyOfPrevCellModes[id]?.[field]?.mode || GridCellModes.View;
const originalId = idToIdLookup[id] ?? id;
const originalId = apiRef.current.getRowId(rowsLookup[id]) ?? id;
if (params.mode === GridCellModes.Edit && prevMode === GridCellModes.View) {
updateStateToStartCellEditMode({ id: originalId, field, ...params });
} else if (params.mode === GridCellModes.View && prevMode === GridCellModes.Edit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
gridVisibleColumnFieldsSelector,
} from '../columns/gridColumnsSelector';
import { GridCellParams } from '../../../models/params/gridCellParams';
import { gridRowsDataRowIdToIdLookupSelector } from '../rows/gridRowsSelector';
import { gridRowsLookupSelector } from '../rows/gridRowsSelector';
import { deepClone } from '../../../utils/utils';
import {
GridRowEditStopParams,
Expand Down Expand Up @@ -743,7 +743,7 @@ export const useGridRowEditing = (

// Run this effect synchronously so that the keyboard event can impact the yet-to-be-rendered input.
useEnhancedEffect(() => {
const idToIdLookup = gridRowsDataRowIdToIdLookupSelector(apiRef);
const rowsLookup = gridRowsLookupSelector(apiRef);

// Update the ref here because updateStateToStopRowEditMode may change it later
const copyOfPrevRowModesModel = prevRowModesModel.current;
Expand All @@ -753,7 +753,7 @@ export const useGridRowEditing = (
Array.from(ids).forEach((id) => {
const params = rowModesModel[id] ?? { mode: GridRowModes.View };
const prevMode = copyOfPrevRowModesModel[id]?.mode || GridRowModes.View;
const originalId = idToIdLookup[id] ?? id;
const originalId = apiRef.current.getRowId(rowsLookup[id]) ?? id;
if (params.mode === GridRowModes.Edit && prevMode === GridRowModes.View) {
updateStateToStartRowEditMode({ id: originalId, ...params });
} else if (params.mode === GridRowModes.View && prevMode === GridRowModes.Edit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export interface GridRowsInternalCache {
* Lookup containing the latest model at all time (even those not stored in the state yet).
*/
dataRowIdToModelLookup: GridRowIdToModelLookup;
/**
* Lookup containing the latest ids at all time (even those not stored in the state yet).
*/
dataRowIdToIdLookup: GridRowIdToIdLookup;
/**
* List of updates (partial or full) applied since the last time the state was synced with the cache.
* It is used to build the tree.
Expand Down Expand Up @@ -66,7 +62,6 @@ export interface GridRowsState {
*/
totalTopLevelRowCount: number;
dataRowIdToModelLookup: GridRowIdToModelLookup;
dataRowIdToIdLookup: GridRowIdToIdLookup;
additionalRowGroups?: {
pinnedRows?: GridPinnedRowsState;
};
Expand All @@ -82,7 +77,6 @@ export interface GridRowTreeCreationParams {
previousTree: GridRowTreeConfig | null;
previousTreeDepths: GridTreeDepths | null;
updates: GridRowsPartialUpdates | GridRowsFullUpdate;
dataRowIdToIdLookup: GridRowIdToIdLookup;
dataRowIdToModelLookup: GridRowIdToModelLookup;
previousGroupsToFetch?: GridRowId[];
}
Expand All @@ -107,12 +101,7 @@ export type GridRowTreeCreationValue = Pick<

export type GridHydrateRowsValue = Pick<
GridRowsState,
| 'tree'
| 'treeDepths'
| 'dataRowIds'
| 'dataRowIdToIdLookup'
| 'dataRowIdToModelLookup'
| 'additionalRowGroups'
'tree' | 'treeDepths' | 'dataRowIds' | 'dataRowIdToModelLookup' | 'additionalRowGroups'
>;

export type GridRowsPartialUpdateAction = 'insert' | 'modify' | 'remove';
Expand All @@ -122,8 +111,6 @@ export type GridRowIdToModelLookup<R extends GridValidRowModel = GridValidRowMod
R
>;

export type GridRowIdToIdLookup = Record<string, GridRowId>;

export type GridTreeDepths = { [depth: number]: number };

export interface GridRowsFullUpdate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ export const gridRowsLookupSelector = createSelector(
(rows) => rows.dataRowIdToModelLookup,
);

export const gridRowsDataRowIdToIdLookupSelector = createSelector(
gridRowsStateSelector,
(rows) => rows.dataRowIdToIdLookup,
);

export const gridRowTreeSelector = createSelector(gridRowsStateSelector, (rows) => rows.tree);

export const gridRowGroupsToFetchSelector = createSelector(
Expand Down
10 changes: 0 additions & 10 deletions packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
GridRowsState,
GridRowTreeCreationParams,
GridRowIdToModelLookup,
GridRowIdToIdLookup,
GridRowsPartialUpdateAction,
} from './gridRowsInterfaces';
import { gridPinnedRowsSelector } from './gridRowsSelector';
Expand Down Expand Up @@ -90,13 +89,11 @@ export const createRowsInternalCache = ({
};

const dataRowIdToModelLookup: GridRowIdToModelLookup = {};
const dataRowIdToIdLookup: GridRowIdToIdLookup = {};

for (let i = 0; i < rows.length; i += 1) {
const model = rows[i];
const id = getRowIdFromRowModel(model, getRowId);
dataRowIdToModelLookup[id] = model;
dataRowIdToIdLookup[id] = id;
updates.rows.push(id);
}

Expand All @@ -105,7 +102,6 @@ export const createRowsInternalCache = ({
loadingPropBeforePartialUpdates: loading,
rowCountPropBeforePartialUpdates: rowCount,
updates,
dataRowIdToIdLookup,
dataRowIdToModelLookup,
};
};
Expand Down Expand Up @@ -153,7 +149,6 @@ export const getRowsStateFromCache = ({
previousTree,
previousTreeDepths,
updates: cache.updates,
dataRowIdToIdLookup: cache.dataRowIdToIdLookup,
dataRowIdToModelLookup: cache.dataRowIdToModelLookup,
previousGroupsToFetch,
});
Expand All @@ -162,7 +157,6 @@ export const getRowsStateFromCache = ({
const groupingParamsWithHydrateRows = apiRef.current.unstable_applyPipeProcessors('hydrateRows', {
tree: unProcessedTree,
treeDepths: unProcessedTreeDepths,
dataRowIdToIdLookup: cache.dataRowIdToIdLookup,
dataRowIds: unProcessedDataRowIds,
dataRowIdToModelLookup: cache.dataRowIdToModelLookup,
});
Expand Down Expand Up @@ -280,7 +274,6 @@ export const updateCacheWithNewRows = ({
groupKeys,
};
const dataRowIdToModelLookup = { ...previousCache.dataRowIdToModelLookup };
const dataRowIdToIdLookup = { ...previousCache.dataRowIdToIdLookup };

const alreadyAppliedActionsToRemove: Record<
GridRowsPartialUpdateAction,
Expand Down Expand Up @@ -313,7 +306,6 @@ export const updateCacheWithNewRows = ({
// Remove the data row from the lookups and add it to the "delete" update.
partialUpdates.actions.remove.push(id);
delete dataRowIdToModelLookup[id];
delete dataRowIdToIdLookup[id];
return;
}

Expand Down Expand Up @@ -354,7 +346,6 @@ export const updateCacheWithNewRows = ({

// Update the data row lookups.
dataRowIdToModelLookup[id] = partialRow;
dataRowIdToIdLookup[id] = id;
});

const actionTypeWithActionsToRemove = Object.keys(
Expand All @@ -372,7 +363,6 @@ export const updateCacheWithNewRows = ({

return {
dataRowIdToModelLookup,
dataRowIdToIdLookup,
updates: partialUpdates,
rowsBeforePartialUpdates: previousCache.rowsBeforePartialUpdates,
loadingPropBeforePartialUpdates: previousCache.loadingPropBeforePartialUpdates,
Expand Down
1 change: 0 additions & 1 deletion packages/x-data-grid/src/hooks/features/rows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
gridRowsLoadingSelector,
gridTopLevelRowCountSelector,
gridRowsLookupSelector,
gridRowsDataRowIdToIdLookupSelector,
gridRowTreeSelector,
gridRowGroupingNameSelector,
gridRowTreeDepthsSelector,
Expand Down
7 changes: 0 additions & 7 deletions packages/x-data-grid/src/hooks/features/rows/useGridRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
gridRowGroupingNameSelector,
gridRowTreeDepthsSelector,
gridDataRowIdsSelector,
gridRowsDataRowIdToIdLookupSelector,
gridRowMaximumTreeDepthSelector,
gridRowGroupsToFetchSelector,
} from './gridRowsSelector';
Expand Down Expand Up @@ -417,7 +416,6 @@ export const useGridRows = (

const tree = { ...gridRowTreeSelector(apiRef) };
const dataRowIdToModelLookup = { ...gridRowsLookupSelector(apiRef) };
const dataRowIdToIdLookup = { ...gridRowsDataRowIdToIdLookupSelector(apiRef) };
const rootGroup = tree[GRID_ROOT_GROUP_ID] as GridGroupNode;
const rootGroupChildren = [...rootGroup.children];
const seenIds = new Set<GridRowId>();
Expand All @@ -434,7 +432,6 @@ export const useGridRows = (

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

Expand All @@ -446,7 +443,6 @@ export const useGridRows = (
groupingKey: null,
};
dataRowIdToModelLookup[rowId] = rowModel;
dataRowIdToIdLookup[rowId] = rowId;
tree[rowId] = rowTreeNodeConfig;

seenIds.add(rowId);
Expand All @@ -458,14 +454,12 @@ export const useGridRows = (
const dataRowIds = rootGroupChildren.filter((childId) => tree[childId]?.type === 'leaf');

apiRef.current.caches.rows.dataRowIdToModelLookup = dataRowIdToModelLookup;
apiRef.current.caches.rows.dataRowIdToIdLookup = dataRowIdToIdLookup;

apiRef.current.setState((state) => ({
...state,
rows: {
...state.rows,
dataRowIdToModelLookup,
dataRowIdToIdLookup,
dataRowIds,
tree,
},
Expand Down Expand Up @@ -577,7 +571,6 @@ export const useGridRows = (
treeDepths: gridRowTreeDepthsSelector(state, apiRef.current.instanceId),
dataRowIds: gridDataRowIdsSelector(state, apiRef.current.instanceId),
dataRowIdToModelLookup: gridRowsLookupSelector(state, apiRef.current.instanceId),
dataRowIdToIdLookup: gridRowsDataRowIdToIdLookupSelector(state, apiRef.current.instanceId),
});

return {
Expand Down
1 change: 0 additions & 1 deletion scripts/x-data-grid-premium.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,6 @@
{ "name": "GridRowProps", "kind": "Interface" },
{ "name": "GridRowReorderCell", "kind": "Function" },
{ "name": "GridRowScrollEndParams", "kind": "Interface" },
{ "name": "gridRowsDataRowIdToIdLookupSelector", "kind": "Variable" },
{ "name": "GridRowSelectionApi", "kind": "Interface" },
{ "name": "GridRowSelectionCheckboxParams", "kind": "Interface" },
{ "name": "GridRowSelectionModel", "kind": "TypeAlias" },
Expand Down
1 change: 0 additions & 1 deletion scripts/x-data-grid-pro.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@
{ "name": "GridRowProps", "kind": "Interface" },
{ "name": "GridRowReorderCell", "kind": "Function" },
{ "name": "GridRowScrollEndParams", "kind": "Interface" },
{ "name": "gridRowsDataRowIdToIdLookupSelector", "kind": "Variable" },
{ "name": "GridRowSelectionApi", "kind": "Interface" },
{ "name": "GridRowSelectionCheckboxParams", "kind": "Interface" },
{ "name": "GridRowSelectionModel", "kind": "TypeAlias" },
Expand Down
1 change: 0 additions & 1 deletion scripts/x-data-grid.exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@
{ "name": "GridRowProApi", "kind": "Interface" },
{ "name": "GridRowProPrivateApi", "kind": "Interface" },
{ "name": "GridRowProps", "kind": "Interface" },
{ "name": "gridRowsDataRowIdToIdLookupSelector", "kind": "Variable" },
{ "name": "GridRowSelectionApi", "kind": "Interface" },
{ "name": "GridRowSelectionCheckboxParams", "kind": "Interface" },
{ "name": "GridRowSelectionModel", "kind": "TypeAlias" },
Expand Down

0 comments on commit 8383e25

Please sign in to comment.