@mui/x-data-grid-pro
Version:
The Pro plan edition of the MUI X Data Grid components.
50 lines (49 loc) • 3.42 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.useGridDetailPanelPreProcessors = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _xDataGrid = require("@mui/x-data-grid");
var _internals = require("@mui/x-data-grid/internals");
var _gridDetailPanelToggleColDef = require("./gridDetailPanelToggleColDef");
var _gridDetailPanelSelector = require("./gridDetailPanelSelector");
const useGridDetailPanelPreProcessors = (privateApiRef, props) => {
const addToggleColumn = React.useCallback(columnsState => {
const detailPanelToggleColumn = (0, _extends2.default)({}, _gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_COL_DEF, {
headerName: privateApiRef.current.getLocaleText('detailPanelToggle')
});
const shouldHaveToggleColumn = !!props.getDetailPanelContent;
const hasToggleColumn = columnsState.lookup[_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD] != null;
if (shouldHaveToggleColumn && !hasToggleColumn) {
columnsState.lookup[_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD] = detailPanelToggleColumn;
columnsState.orderedFields = [_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD, ...columnsState.orderedFields];
} else if (!shouldHaveToggleColumn && hasToggleColumn) {
delete columnsState.lookup[_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD];
columnsState.orderedFields = columnsState.orderedFields.filter(field => field !== _gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD);
} else if (shouldHaveToggleColumn && hasToggleColumn) {
columnsState.lookup[_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD] = (0, _extends2.default)({}, detailPanelToggleColumn, columnsState.lookup[_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD]);
// If the column is not in the columns array (not a custom detail panel toggle column), move it to the beginning of the column order
if (!props.columns.some(col => col.field === _gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD)) {
columnsState.orderedFields = [_gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD, ...columnsState.orderedFields.filter(field => field !== _gridDetailPanelToggleColDef.GRID_DETAIL_PANEL_TOGGLE_FIELD)];
}
}
return columnsState;
}, [privateApiRef, props.columns, props.getDetailPanelContent]);
const addExpandedClassToRow = React.useCallback((classes, id) => {
if (props.getDetailPanelContent == null) {
return classes;
}
const expandedRowIds = (0, _gridDetailPanelSelector.gridDetailPanelExpandedRowIdsSelector)(privateApiRef);
if (!expandedRowIds.has(id)) {
return classes;
}
return [...classes, _xDataGrid.gridClasses['row--detailPanelExpanded']];
}, [privateApiRef, props.getDetailPanelContent]);
(0, _internals.useGridRegisterPipeProcessor)(privateApiRef, 'hydrateColumns', addToggleColumn);
(0, _internals.useGridRegisterPipeProcessor)(privateApiRef, 'rowClassName', addExpandedClassToRow);
};
exports.useGridDetailPanelPreProcessors = useGridDetailPanelPreProcessors;
;