UNPKG

@mui/x-data-grid-premium

Version:

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

166 lines (161 loc) 5.78 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapColumnWithAggregationValue = exports.unwrapColumnFromAggregation = void 0; var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _xDataGridPro = require("@mui/x-data-grid-pro"); var _internals = require("@mui/x-data-grid-pro/internals"); var _gridAggregationSelectors = require("./gridAggregationSelectors"); var _GridAggregationHeader = require("../../../components/GridAggregationHeader"); var _gridPivotingSelectors = require("../pivoting/gridPivotingSelectors"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["aggregationWrappedProperties"]; const getAggregationValueWrappedRenderCell = ({ value: renderCell, aggregationRule, getCellAggregationResult, apiRef }) => { const pivotActive = (0, _gridPivotingSelectors.gridPivotActiveSelector)(apiRef); const wrappedRenderCell = params => { const cellAggregationResult = getCellAggregationResult(params.id, params.field); if (cellAggregationResult != null) { if (!renderCell) { if (cellAggregationResult.position === 'footer') { return /*#__PURE__*/(0, _jsxRuntime.jsx)(_internals.GridFooterCell, (0, _extends2.default)({}, params)); } if (pivotActive && cellAggregationResult.value === 0) { return null; } return params.formattedValue; } if (pivotActive && cellAggregationResult.value === 0) { return null; } const aggregationMeta = { hasCellUnit: aggregationRule.aggregationFunction.hasCellUnit ?? true, aggregationFunctionName: aggregationRule.aggregationFunctionName, position: cellAggregationResult.position }; return renderCell((0, _extends2.default)({}, params, { aggregation: aggregationMeta })); } if (!renderCell) { return params.formattedValue; } return renderCell(params); }; return wrappedRenderCell; }; /** * Add the aggregation method around the header name */ const getWrappedRenderHeader = ({ value: renderHeader, aggregationRule }) => { const wrappedRenderHeader = params => { // TODO: investigate why colDef is undefined if (!params.colDef) { return null; } return /*#__PURE__*/(0, _jsxRuntime.jsx)(_GridAggregationHeader.GridAggregationHeader, (0, _extends2.default)({}, params, { aggregation: { aggregationRule }, renderHeader: renderHeader })); }; if (process.env.NODE_ENV !== "production") wrappedRenderHeader.displayName = "wrappedRenderHeader"; return wrappedRenderHeader; }; /** * Add a wrapper around each wrappable property of the column to customize the behavior of the aggregation cells. */ const wrapColumnWithAggregationValue = (column, aggregationRule, apiRef) => { const getCellAggregationResult = (id, field) => { let cellAggregationPosition = null; const rowNode = (0, _xDataGridPro.gridRowNodeSelector)(apiRef, id); if (!rowNode) { return null; } if (rowNode.type === 'group') { cellAggregationPosition = 'inline'; } else if (id.toString().startsWith('auto-generated-group-footer-')) { cellAggregationPosition = 'footer'; } if (cellAggregationPosition == null) { return null; } // TODO: Add custom root id const groupId = cellAggregationPosition === 'inline' ? id : rowNode.parent ?? ''; const aggregationResult = (0, _gridAggregationSelectors.gridAggregationLookupSelector)(apiRef)?.[groupId]?.[field]; if (!aggregationResult || aggregationResult.position !== cellAggregationPosition) { return null; } return aggregationResult; }; let didWrapSomeProperty = false; const wrappedColumn = (0, _extends2.default)({}, column, { aggregationWrappedProperties: [] }); const wrapColumnProperty = (property, wrapper) => { const originalValue = column[property]; const wrappedProperty = wrapper({ apiRef, value: originalValue, colDef: column, aggregationRule, getCellAggregationResult }); if (wrappedProperty !== originalValue) { didWrapSomeProperty = true; wrappedColumn[property] = wrappedProperty; wrappedColumn.aggregationWrappedProperties.push({ name: property, originalValue, wrappedValue: wrappedProperty }); } }; wrapColumnProperty('renderCell', getAggregationValueWrappedRenderCell); wrapColumnProperty('renderHeader', getWrappedRenderHeader); if (!didWrapSomeProperty) { return column; } return wrappedColumn; }; exports.wrapColumnWithAggregationValue = wrapColumnWithAggregationValue; const isColumnWrappedWithAggregation = column => { return typeof column.aggregationWrappedProperties !== 'undefined'; }; /** * Remove the aggregation wrappers around the wrappable properties of the column. */ const unwrapColumnFromAggregation = column => { if (!isColumnWrappedWithAggregation(column)) { return column; } const _ref = column, { aggregationWrappedProperties } = _ref, unwrappedColumn = (0, _objectWithoutPropertiesLoose2.default)(_ref, _excluded); aggregationWrappedProperties.forEach(({ name, originalValue, wrappedValue }) => { // The value changed since we wrapped it if (wrappedValue !== unwrappedColumn[name]) { return; } unwrappedColumn[name] = originalValue; }); return unwrappedColumn; }; exports.unwrapColumnFromAggregation = unwrapColumnFromAggregation;