@mui/x-data-grid-premium
Version:
The Premium plan edition of the MUI X Data Grid Components.
137 lines (136 loc) • 5.83 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GridDataSourceGroupingCriteriaCell = GridDataSourceGroupingCriteriaCell;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _composeClasses = _interopRequireDefault(require("@mui/utils/composeClasses"));
var _internals = require("@mui/x-data-grid-pro/internals");
var _xDataGridPro = require("@mui/x-data-grid-pro");
var _useGridApiContext = require("../hooks/utils/useGridApiContext");
var _useGridRootProps = require("../hooks/utils/useGridRootProps");
var _gridRowGroupingSelector = require("../hooks/features/rowGrouping/gridRowGroupingSelector");
var _jsxRuntime = require("react/jsx-runtime");
const useUtilityClasses = ownerState => {
const {
classes
} = ownerState;
const slots = {
root: ['groupingCriteriaCell'],
toggle: ['groupingCriteriaCellToggle'],
loadingContainer: ['groupingCriteriaCellLoadingContainer']
};
return (0, _composeClasses.default)(slots, _xDataGridPro.getDataGridUtilityClass, classes);
};
function GridGroupingCriteriaCellIcon(props) {
const apiRef = (0, _internals.useGridPrivateApiContext)();
const rootProps = (0, _useGridRootProps.useGridRootProps)();
const classes = useUtilityClasses(rootProps);
const {
rowNode,
id,
field,
descendantCount
} = props;
const isDataLoading = (0, _xDataGridPro.useGridSelector)(apiRef, _internals.gridDataSourceLoadingIdSelector, id);
const error = (0, _xDataGridPro.useGridSelector)(apiRef, _internals.gridDataSourceErrorSelector, id);
const handleClick = event => {
if (!rowNode.childrenExpanded) {
// always fetch/get from cache the children when the node is expanded
apiRef.current.dataSource.fetchRows(id);
} else {
// Collapse the node and remove child rows from the grid
apiRef.current.setRowChildrenExpansion(id, false);
apiRef.current.removeChildrenRows(id);
}
apiRef.current.setCellFocus(id, field);
event.stopPropagation();
};
const Icon = rowNode.childrenExpanded ? rootProps.slots.groupingCriteriaCollapseIcon : rootProps.slots.groupingCriteriaExpandIcon;
if (isDataLoading) {
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: classes.loadingContainer,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.baseCircularProgress, {
size: "1rem",
color: "inherit"
})
});
}
return descendantCount === -1 || descendantCount > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.baseIconButton, (0, _extends2.default)({
size: "small",
onClick: handleClick,
tabIndex: -1,
"aria-label": rowNode.childrenExpanded ? apiRef.current.getLocaleText('treeDataCollapse') : apiRef.current.getLocaleText('treeDataExpand')
}, rootProps?.slotProps?.baseIconButton, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.baseTooltip, {
title: error?.message ?? null,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(rootProps.slots.baseBadge, {
variant: "dot",
color: "error",
invisible: !error,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Icon, {
fontSize: "inherit"
})
})
})
})) : null;
}
function GridDataSourceGroupingCriteriaCell(props) {
const {
id,
field,
rowNode,
hideDescendantCount,
formattedValue
} = props;
const rootProps = (0, _useGridRootProps.useGridRootProps)();
const apiRef = (0, _useGridApiContext.useGridApiContext)();
const row = (0, _xDataGridPro.useGridSelector)(apiRef, _internals.gridRowSelector, id);
const pivotActive = (0, _xDataGridPro.useGridSelector)(apiRef, _internals.gridPivotActiveSelector);
const rowGroupingModelLength = (0, _xDataGridPro.useGridSelector)(apiRef, _gridRowGroupingSelector.gridRowGroupingModelSelector).length;
const classes = useUtilityClasses(rootProps);
const shouldShowToggleContainer = !pivotActive || rowGroupingModelLength > 1;
// Do not allow expand/collapse the last grouping criteria cell when in pivot mode
const shouldShowToggleButton = !pivotActive || rowNode.depth < rowGroupingModelLength - 1;
let descendantCount = 0;
if (row) {
descendantCount = rootProps.dataSource?.getChildrenCount?.(row) ?? 0;
}
let cellContent;
const colDef = apiRef.current.getColumn(rowNode.groupingField);
if (typeof colDef?.renderCell === 'function') {
cellContent = colDef.renderCell(props);
} else if (typeof formattedValue !== 'undefined') {
cellContent = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: formattedValue
});
} else {
cellContent = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: rowNode.groupingKey
});
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: classes.root,
style: {
marginLeft: rootProps.rowGroupingColumnMode === 'multiple' ? 0 : `calc(var(--DataGrid-cellOffsetMultiplier) * ${_internals.vars.spacing(rowNode.depth)})`
},
children: [shouldShowToggleContainer && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: classes.toggle,
children: shouldShowToggleButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(GridGroupingCriteriaCellIcon, {
id: id,
field: field,
rowNode: rowNode,
row: row,
descendantCount: descendantCount
})
}), cellContent, !hideDescendantCount && descendantCount > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
style: {
whiteSpace: 'pre'
},
children: [" (", descendantCount, ")"]
}) : null]
});
}