UNPKG

@mui/x-data-grid

Version:

The community edition of the data grid component (MUI X).

109 lines (98 loc) 4.87 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; 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 var COMPACT_DENSITY_FACTOR = 0.7; export var COMFORTABLE_DENSITY_FACTOR = 1.3; // TODO v6: revise keeping headerHeight and rowHeight in state var getUpdatedDensityState = function 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 var densityStateInitializer = function densityStateInitializer(state, props) { // TODO: think about improving this initialization. Could it be done in the useColumn initializer? // TODO: manage to remove ts-ignore var maxDepth; if (props.columnGroupingModel == null || Object.keys(props.columnGroupingModel).length === 0) { maxDepth = 0; } else { var unwrappedGroupingColumnModel = unwrapGroupingColumnModel(props.columnGroupingModel); var columnsState = state.columns; var visibleColumns = columnsState.all.filter(function (field) { return columnsState.columnVisibilityModel[field] !== false; }); if (visibleColumns.length === 0) { maxDepth = 0; } else { maxDepth = Math.max.apply(Math, _toConsumableArray(visibleColumns.map(function (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 var useGridDensity = function useGridDensity(apiRef, props) { var visibleColumns = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector); var maxDepth = visibleColumns.length > 0 ? Math.max.apply(Math, _toConsumableArray(visibleColumns.map(function (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; var logger = useGridLogger(apiRef, 'useDensity'); var setDensity = React.useCallback(function (newDensity) { var newHeaderHeight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : props.headerHeight; var newRowHeight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : props.rowHeight; var newMaxDepth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : maxDepth; logger.debug("Set grid density to ".concat(newDensity)); apiRef.current.setState(function (state) { var currentDensityState = gridDensitySelector(state); var 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(function () { apiRef.current.setDensity(props.density, props.headerHeight, props.rowHeight, maxDepth); }, [apiRef, props.density, props.rowHeight, props.headerHeight, maxDepth]); var densityApi = { setDensity: setDensity }; useGridApiMethod(apiRef, densityApi, 'GridDensityApi'); };