@mui/x-data-grid
Version:
The community edition of the data grid component (MUI X).
103 lines (92 loc) • 4.37 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { GridDensityTypes } from '../../../models/gridDensity';
import { useGridLogger } from '../../utils/useGridLogger';
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { gridDensitySelector } from './densitySelector';
import { isDeepEqual } from '../../../utils/utils';
import { useGridSelector } from '../../utils/useGridSelector';
import { gridVisibleColumnDefinitionsSelector } from '../columns';
import { unwrapGroupingColumnModel } from '../columnGrouping/useGridColumnGrouping';
export const COMPACT_DENSITY_FACTOR = 0.7;
export const COMFORTABLE_DENSITY_FACTOR = 1.3; // TODO v6: revise keeping headerHeight and rowHeight in state
const getUpdatedDensityState = (newDensity, newHeaderHeight, newRowHeight, newMaxDepth) => {
switch (newDensity) {
case GridDensityTypes.Compact:
return {
value: newDensity,
headerHeight: Math.floor(newHeaderHeight * COMPACT_DENSITY_FACTOR),
rowHeight: Math.floor(newRowHeight * COMPACT_DENSITY_FACTOR),
headerGroupingMaxDepth: newMaxDepth,
factor: COMPACT_DENSITY_FACTOR
};
case GridDensityTypes.Comfortable:
return {
value: newDensity,
headerHeight: Math.floor(newHeaderHeight * COMFORTABLE_DENSITY_FACTOR),
rowHeight: Math.floor(newRowHeight * COMFORTABLE_DENSITY_FACTOR),
headerGroupingMaxDepth: newMaxDepth,
factor: COMFORTABLE_DENSITY_FACTOR
};
default:
return {
value: newDensity,
headerHeight: newHeaderHeight,
rowHeight: newRowHeight,
headerGroupingMaxDepth: newMaxDepth,
factor: 1
};
}
};
export const densityStateInitializer = (state, props) => {
// TODO: think about improving this initialization. Could it be done in the useColumn initializer?
// TODO: manage to remove ts-ignore
let maxDepth;
if (props.columnGroupingModel == null || Object.keys(props.columnGroupingModel).length === 0) {
maxDepth = 0;
} else {
const unwrappedGroupingColumnModel = unwrapGroupingColumnModel(props.columnGroupingModel);
const columnsState = state.columns;
const visibleColumns = columnsState.all.filter(field => columnsState.columnVisibilityModel[field] !== false);
if (visibleColumns.length === 0) {
maxDepth = 0;
} else {
maxDepth = Math.max(...visibleColumns.map(field => {
var _unwrappedGroupingCol, _unwrappedGroupingCol2;
return (_unwrappedGroupingCol = (_unwrappedGroupingCol2 = unwrappedGroupingColumnModel[field]) == null ? void 0 : _unwrappedGroupingCol2.length) != null ? _unwrappedGroupingCol : 0;
}));
}
}
return _extends({}, state, {
density: getUpdatedDensityState(props.density, props.headerHeight, props.rowHeight, maxDepth)
});
};
export const useGridDensity = (apiRef, props) => {
const visibleColumns = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
const maxDepth = visibleColumns.length > 0 ? Math.max(...visibleColumns.map(column => {
var _column$groupPath$len, _column$groupPath;
return (_column$groupPath$len = (_column$groupPath = column.groupPath) == null ? void 0 : _column$groupPath.length) != null ? _column$groupPath$len : 0;
})) : 0;
const logger = useGridLogger(apiRef, 'useDensity');
const setDensity = React.useCallback((newDensity, newHeaderHeight = props.headerHeight, newRowHeight = props.rowHeight, newMaxDepth = maxDepth) => {
logger.debug(`Set grid density to ${newDensity}`);
apiRef.current.setState(state => {
const currentDensityState = gridDensitySelector(state);
const newDensityState = getUpdatedDensityState(newDensity, newHeaderHeight, newRowHeight, newMaxDepth);
if (isDeepEqual(currentDensityState, newDensityState)) {
return state;
}
return _extends({}, state, {
density: newDensityState
});
});
apiRef.current.forceUpdate();
}, [logger, apiRef, props.headerHeight, props.rowHeight, maxDepth]);
React.useEffect(() => {
apiRef.current.setDensity(props.density, props.headerHeight, props.rowHeight, maxDepth);
}, [apiRef, props.density, props.rowHeight, props.headerHeight, maxDepth]);
const densityApi = {
setDensity
};
useGridApiMethod(apiRef, densityApi, 'GridDensityApi');
};