UNPKG

@mui/x-data-grid-premium

Version:

The Premium plan edition of the MUI X Data Grid Components.

99 lines (94 loc) 4.02 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import { gridColumnLookupSelector, useGridEvent, useGridApiMethod } from '@mui/x-data-grid-pro'; import { useGridRegisterPipeProcessor } from '@mui/x-data-grid-pro/internals'; import { gridAggregationModelSelector } from "./gridAggregationSelectors.js"; import { getAggregationRules, mergeStateWithAggregationModel, areAggregationRulesEqual } from "./gridAggregationUtils.js"; import { createAggregationLookup } from "./createAggregationLookup.js"; export const aggregationStateInitializer = (state, props, apiRef) => { apiRef.current.caches.aggregation = { rulesOnLastColumnHydration: {}, rulesOnLastRowHydration: {} }; return _extends({}, state, { aggregation: { model: props.aggregationModel ?? props.initialState?.aggregation?.model ?? {} } }); }; export const useGridAggregation = (apiRef, props) => { apiRef.current.registerControlState({ stateId: 'aggregation', propModel: props.aggregationModel, propOnChange: props.onAggregationModelChange, stateSelector: gridAggregationModelSelector, changeEvent: 'aggregationModelChange' }); /** * API METHODS */ const setAggregationModel = React.useCallback(model => { const currentModel = gridAggregationModelSelector(apiRef); if (currentModel !== model) { apiRef.current.setState(mergeStateWithAggregationModel(model)); } }, [apiRef]); const applyAggregation = React.useCallback(() => { const aggregationLookup = createAggregationLookup({ apiRef, getAggregationPosition: props.getAggregationPosition, aggregationFunctions: props.aggregationFunctions, aggregationRowsScope: props.aggregationRowsScope, isDataSource: !!props.dataSource }); apiRef.current.setState(state => _extends({}, state, { aggregation: _extends({}, state.aggregation, { lookup: aggregationLookup }) })); }, [apiRef, props.getAggregationPosition, props.aggregationFunctions, props.aggregationRowsScope, props.dataSource]); const aggregationApi = { setAggregationModel }; const aggregationPrivateApi = { applyAggregation }; useGridApiMethod(apiRef, aggregationApi, 'public'); useGridApiMethod(apiRef, aggregationPrivateApi, 'private'); const addGetRowsParams = React.useCallback(params => { return _extends({}, params, { aggregationModel: gridAggregationModelSelector(apiRef) }); }, [apiRef]); useGridRegisterPipeProcessor(apiRef, 'getRowsParams', addGetRowsParams); /** * EVENTS */ const checkAggregationRulesDiff = React.useCallback(() => { const { rulesOnLastRowHydration, rulesOnLastColumnHydration } = apiRef.current.caches.aggregation; const aggregationRules = props.disableAggregation ? {} : getAggregationRules(gridColumnLookupSelector(apiRef), gridAggregationModelSelector(apiRef), props.aggregationFunctions, !!props.dataSource); // Re-apply the row hydration to add / remove the aggregation footers if (!props.dataSource && !areAggregationRulesEqual(rulesOnLastRowHydration, aggregationRules)) { apiRef.current.requestPipeProcessorsApplication('hydrateRows'); applyAggregation(); } // Re-apply the column hydration to wrap / unwrap the aggregated columns if (!areAggregationRulesEqual(rulesOnLastColumnHydration, aggregationRules)) { apiRef.current.requestPipeProcessorsApplication('hydrateColumns'); } }, [apiRef, applyAggregation, props.aggregationFunctions, props.disableAggregation, props.dataSource]); useGridEvent(apiRef, 'aggregationModelChange', checkAggregationRulesDiff); useGridEvent(apiRef, 'columnsChange', checkAggregationRulesDiff); useGridEvent(apiRef, 'filteredRowsSet', applyAggregation); /** * EFFECTS */ React.useEffect(() => { if (props.aggregationModel !== undefined) { apiRef.current.setAggregationModel(props.aggregationModel); } }, [apiRef, props.aggregationModel]); };