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] Allow to control the grid density #12332

Merged
merged 16 commits into from
Mar 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const densityStateInitializer: GridStateInitializer<

export const useGridDensity = (
apiRef: React.MutableRefObject<GridPrivateApiCommunity>,
props: Pick<DataGridProcessedProps, 'density'>,
props: Pick<DataGridProcessedProps, 'density' | 'onDensityChange'>,
): void => {
const logger = useGridLogger(apiRef, 'useDensity');

Expand All @@ -42,14 +42,18 @@ export const useGridDensity = (
return state;
}

if (props.onDensityChange) {
props.onDensityChange(newDensity);
}

return {
...state,
density: newDensityState,
};
});
apiRef.current.forceUpdate();
},
[logger, apiRef],
[logger, apiRef, props],
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
);

React.useEffect(() => {
Expand Down
6 changes: 6 additions & 0 deletions packages/x-data-grid/src/models/props/DataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,11 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
* @param {GridCallbackDetails} details Additional details for this callback.
*/
onColumnOrderChange?: GridEventListener<'columnOrderChange'>;
/**
* Callback fired when the density changes.
* @param {GridDensity} density New density value.
*/
onDensityChange?: (density: GridDensity) => void;
/**
* Callback fired when a row is clicked.
* Not called if the target clicked is an interactive element added by the built-in columns.
Expand Down Expand Up @@ -660,6 +665,7 @@ export interface DataGridPropsWithoutDefaultValue<R extends GridValidRowModel =
* Callback fired when the selection state of one or multiple rows changes.
* @param {GridRowSelectionModel} rowSelectionModel With all the row ids [[GridSelectionModel]].
* @param {GridCallbackDetails} details Additional details for this callback.
* @param {GridCallbackDetails} details Additional details for this callback.
*/
onRowSelectionModelChange?: (
rowSelectionModel: GridRowSelectionModel,
Expand Down