@mui/x-data-grid-pro
Version:
The Pro plan edition of the Data Grid components (MUI X).
120 lines (115 loc) • 5.14 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.addPinnedRow = addPinnedRow;
exports.useGridRowPinningPreProcessors = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _internals = require("@mui/x-data-grid/internals");
var _xDataGrid = require("@mui/x-data-grid");
var _utils = require("../../../utils/tree/utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function addPinnedRow({
groupingParams,
rowModel,
rowId,
position,
apiRef,
isAutoGenerated
}) {
const dataRowIdToModelLookup = (0, _extends2.default)({}, groupingParams.dataRowIdToModelLookup);
const dataRowIdToIdLookup = (0, _extends2.default)({}, groupingParams.dataRowIdToIdLookup);
const tree = (0, _extends2.default)({}, groupingParams.tree);
const treeDepths = (0, _extends2.default)({}, groupingParams.treeDepths);
// TODO: warn if id is already present in `props.rows`
const node = {
type: 'pinnedRow',
id: rowId,
depth: 0,
parent: _xDataGrid.GRID_ROOT_GROUP_ID,
isAutoGenerated
};
(0, _utils.insertNodeInTree)(node, tree, treeDepths, null);
if (!isAutoGenerated) {
dataRowIdToModelLookup[rowId] = rowModel;
dataRowIdToIdLookup[rowId] = rowId;
}
// Do not push it to ids list so that pagination is not affected by pinned rows
apiRef.current.caches.rows.dataRowIdToModelLookup[rowId] = (0, _extends2.default)({}, rowModel);
apiRef.current.caches.rows.dataRowIdToIdLookup[rowId] = rowId;
const previousPinnedRows = groupingParams.additionalRowGroups?.pinnedRows || {};
const newPinnedRow = {
id: rowId,
model: rowModel
};
if (groupingParams.additionalRowGroups?.pinnedRows?.[position]?.includes(newPinnedRow)) {
return (0, _extends2.default)({}, groupingParams, {
dataRowIdToModelLookup,
dataRowIdToIdLookup,
tree,
treeDepths
});
}
return (0, _extends2.default)({}, groupingParams, {
dataRowIdToModelLookup,
dataRowIdToIdLookup,
tree,
treeDepths,
additionalRowGroups: (0, _extends2.default)({}, groupingParams.additionalRowGroups, {
pinnedRows: (0, _extends2.default)({}, previousPinnedRows, {
[position]: [...(previousPinnedRows[position] || []), newPinnedRow]
})
})
});
}
const useGridRowPinningPreProcessors = apiRef => {
const addPinnedRows = React.useCallback(groupingParams => {
const pinnedRowsCache = apiRef.current.caches.pinnedRows || {};
let newGroupingParams = (0, _extends2.default)({}, groupingParams, {
additionalRowGroups: (0, _extends2.default)({}, groupingParams.additionalRowGroups, {
// reset pinned rows state
pinnedRows: {}
})
});
pinnedRowsCache.topIds?.forEach(rowId => {
newGroupingParams = addPinnedRow({
groupingParams: newGroupingParams,
rowModel: pinnedRowsCache.idLookup[rowId],
rowId,
position: 'top',
apiRef,
isAutoGenerated: false
});
});
pinnedRowsCache.bottomIds?.forEach(rowId => {
newGroupingParams = addPinnedRow({
groupingParams: newGroupingParams,
rowModel: pinnedRowsCache.idLookup[rowId],
rowId,
position: 'bottom',
apiRef,
isAutoGenerated: false
});
});
// If row with the same `id` is present both in `rows` and `pinnedRows` - remove it from the root group children
if (pinnedRowsCache.bottomIds?.length || pinnedRowsCache.topIds?.length) {
const shouldKeepRow = rowId => {
if (newGroupingParams.tree[rowId] && newGroupingParams.tree[rowId].type === 'pinnedRow') {
return false;
}
return true;
};
const rootGroupNode = newGroupingParams.tree[_xDataGrid.GRID_ROOT_GROUP_ID];
newGroupingParams.tree[_xDataGrid.GRID_ROOT_GROUP_ID] = (0, _extends2.default)({}, rootGroupNode, {
children: rootGroupNode.children.filter(shouldKeepRow)
});
newGroupingParams.dataRowIds = newGroupingParams.dataRowIds.filter(shouldKeepRow);
}
return newGroupingParams;
}, [apiRef]);
(0, _internals.useGridRegisterPipeProcessor)(apiRef, 'hydrateRows', addPinnedRows);
};
exports.useGridRowPinningPreProcessors = useGridRowPinningPreProcessors;
;