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] Include api in gridCellParams interface #14201

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 12 additions & 19 deletions packages/x-data-grid/src/components/cell/GridCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
unstable_ownerDocument as ownerDocument,
unstable_capitalize as capitalize,
} from '@mui/utils';
import type { GridApiCommunity } from '../../internals';
import { fastMemo } from '../../utils/fastMemo';
import { doesSupportPreventScroll } from '../../utils/doesSupportPreventScroll';
import { getDataGridUtilityClass, gridClasses } from '../../constants/gridClasses';
Expand Down Expand Up @@ -75,11 +74,7 @@ export type GridCellProps = {
[x: string]: any; // TODO v7: remove this - it breaks type safety
};

type CellParamsWithAPI = GridCellParams<any, any, any, GridTreeNodeWithRender> & {
api: GridApiCommunity;
};

const EMPTY_CELL_PARAMS: CellParamsWithAPI = {
const EMPTY_CELL_PARAMS: GridCellParams<any, any, any, GridTreeNodeWithRender> = {
id: -1,
field: '__unset__',
row: {},
Expand Down Expand Up @@ -183,19 +178,17 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe

const field = column.field;

const cellParamsWithAPI = useGridSelector(
const cellParams = useGridSelector(
apiRef,
() => {
// This is required because `.getCellParams` tries to get the `state.rows.tree` entry
// associated with `rowId`/`fieldId`, but this selector runs after the state has been
// updated, while `rowId`/`fieldId` reference an entry in the old state.
try {
const cellParams = apiRef.current.getCellParams<any, any, any, GridTreeNodeWithRender>(
const result = apiRef.current.getCellParams<any, any, any, GridTreeNodeWithRender>(
rowId,
field,
);

const result = cellParams as CellParamsWithAPI;
result.api = apiRef.current;
return result;
} catch (e) {
Expand All @@ -215,15 +208,15 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe
}),
);

const { cellMode, hasFocus, isEditable = false, value } = cellParamsWithAPI;
const { cellMode, hasFocus, isEditable = false, value } = cellParams;

const canManageOwnFocus =
column.type === 'actions' &&
(column as GridActionsColDef)
.getActions?.(apiRef.current.getRowParams(rowId))
.some((action) => !action.props.disabled);
const tabIndex =
(cellMode === 'view' || !isEditable) && !canManageOwnFocus ? cellParamsWithAPI.tabIndex : -1;
(cellMode === 'view' || !isEditable) && !canManageOwnFocus ? cellParams.tabIndex : -1;

const { classes: rootClasses, getCellClassName } = rootProps;

Expand All @@ -243,7 +236,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe
if (column.cellClassName) {
classNames.push(
typeof column.cellClassName === 'function'
? column.cellClassName(cellParamsWithAPI)
? column.cellClassName(cellParams)
: column.cellClassName,
);
}
Expand All @@ -253,10 +246,10 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe
}

if (getCellClassName) {
classNames.push(getCellClassName(cellParamsWithAPI));
classNames.push(getCellClassName(cellParams));
}

const valueToRender = cellParamsWithAPI.formattedValue ?? value;
const valueToRender = cellParams.formattedValue ?? value;
const cellRef = React.useRef<HTMLDivElement>(null);
const handleRef = useForkRef(ref, cellRef);
const focusElementRef = React.useRef<FocusElement>(null);
Expand Down Expand Up @@ -373,7 +366,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe
}
}, [hasFocus, cellMode, apiRef]);

if (cellParamsWithAPI === EMPTY_CELL_PARAMS) {
if (cellParams === EMPTY_CELL_PARAMS) {
return null;
}

Expand Down Expand Up @@ -411,7 +404,7 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe
let title: string | undefined;

if (editCellState === null && column.renderCell) {
children = column.renderCell(cellParamsWithAPI);
children = column.renderCell(cellParams);
}

if (editCellState !== null && column.renderEditCell) {
Expand All @@ -422,10 +415,10 @@ const GridCell = React.forwardRef<HTMLDivElement, GridCellProps>(function GridCe

const formattedValue = column.valueFormatter
? column.valueFormatter(editCellState.value as never, updatedRow, column, apiRef)
: cellParamsWithAPI.formattedValue;
: cellParams.formattedValue;

const params: GridRenderEditCellParams = {
...cellParamsWithAPI,
...cellParams,
row: updatedRow,
formattedValue,
...editCellStateRest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function useGridParamsApi(apiRef: React.MutableRefObject<GridPrivateApiCo
value,
formattedValue: value,
isEditable: false,
api: {} as any,
};
if (colDef && colDef.valueFormatter) {
params.formattedValue = colDef.valueFormatter(value as never, row, colDef, apiRef);
Expand Down
4 changes: 4 additions & 0 deletions packages/x-data-grid/src/models/params/gridCellParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export interface GridCellParams<
* the tabIndex value.
*/
tabIndex: 0 | -1;
/**
* GridApi that let you manipulate the grid.
*/
api: GridApiCommunity;
}

export interface FocusElement {
Expand Down
Loading