@mui/x-data-grid
Version:
The community edition of the data grid component (MUI X).
112 lines (98 loc) • 4.64 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["rowsBeforePartialUpdates"];
import { gridPinnedRowsSelector } from './gridRowsSelector';
/**
* A helper function to check if the id provided is valid.
* @param {GridRowId} id Id as [[GridRowId]].
* @param {GridRowModel | Partial<GridRowModel>} row Row as [[GridRowModel]].
* @param {string} detailErrorMessage A custom error message to display for invalid IDs
*/
export function checkGridRowIdIsValid(id, row) {
var detailErrorMessage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'A row was provided without id in the rows prop:';
if (id == null) {
throw new Error(['MUI: The data grid component requires all rows to have a unique `id` property.', 'Alternatively, you can use the `getRowId` prop to specify a custom id for each row.', detailErrorMessage, JSON.stringify(row)].join('\n'));
}
}
export var getRowIdFromRowModel = function getRowIdFromRowModel(rowModel, getRowId, detailErrorMessage) {
var id = getRowId ? getRowId(rowModel) : rowModel.id;
checkGridRowIdIsValid(id, rowModel, detailErrorMessage);
return id;
};
export var createRowsInternalCache = function createRowsInternalCache(_ref) {
var rows = _ref.rows,
getRowId = _ref.getRowId,
loading = _ref.loading;
var cache = {
rowsBeforePartialUpdates: rows,
loadingPropBeforePartialUpdates: loading,
idRowsLookup: {},
idToIdLookup: {},
ids: []
};
for (var i = 0; i < rows.length; i += 1) {
var row = rows[i];
var id = getRowIdFromRowModel(row, getRowId);
cache.idRowsLookup[id] = row;
cache.idToIdLookup[id] = id;
cache.ids.push(id);
}
return cache;
};
export var getRowsStateFromCache = function getRowsStateFromCache(_ref2) {
var apiRef = _ref2.apiRef,
previousTree = _ref2.previousTree,
rowCountProp = _ref2.rowCountProp,
loadingProp = _ref2.loadingProp;
var _apiRef$current$unsta = apiRef.current.unstable_caches.rows,
rowsBeforePartialUpdates = _apiRef$current$unsta.rowsBeforePartialUpdates,
cacheForGrouping = _objectWithoutProperties(_apiRef$current$unsta, _excluded);
var rowCount = rowCountProp != null ? rowCountProp : 0;
var groupingResponse = apiRef.current.unstable_applyStrategyProcessor('rowTreeCreation', _extends({}, cacheForGrouping, {
previousTree: previousTree
}));
var processedGroupingResponse = apiRef.current.unstable_applyPipeProcessors('hydrateRows', groupingResponse);
var dataTopLevelRowCount = processedGroupingResponse.treeDepth === 1 ? processedGroupingResponse.ids.length : Object.values(processedGroupingResponse.tree).filter(function (node) {
return node.parent == null && !node.isPinned;
}).length;
return _extends({}, processedGroupingResponse, {
groupingResponseBeforeRowHydration: groupingResponse,
loading: loadingProp,
totalRowCount: Math.max(rowCount, processedGroupingResponse.ids.length),
totalTopLevelRowCount: Math.max(rowCount, dataTopLevelRowCount)
});
};
export var getTreeNodeDescendants = function getTreeNodeDescendants(tree, parentId, skipAutoGeneratedRows) {
var _tree$parentId;
var children = (_tree$parentId = tree[parentId]) == null ? void 0 : _tree$parentId.children;
if (children == null) {
return [];
}
var validDescendants = [];
for (var i = 0; i < children.length; i += 1) {
var child = children[i];
var childNode = tree[child];
if (!skipAutoGeneratedRows || !childNode.isAutoGenerated) {
validDescendants.push(child);
}
validDescendants.push.apply(validDescendants, _toConsumableArray(getTreeNodeDescendants(tree, childNode.id, skipAutoGeneratedRows)));
}
return validDescendants;
};
export function calculatePinnedRowsHeight(apiRef) {
var _pinnedRows$top, _pinnedRows$bottom;
var pinnedRows = gridPinnedRowsSelector(apiRef);
var topPinnedRowsHeight = (pinnedRows == null ? void 0 : (_pinnedRows$top = pinnedRows.top) == null ? void 0 : _pinnedRows$top.reduce(function (acc, value) {
acc += apiRef.current.unstable_getRowHeight(value.id);
return acc;
}, 0)) || 0;
var bottomPinnedRowsHeight = (pinnedRows == null ? void 0 : (_pinnedRows$bottom = pinnedRows.bottom) == null ? void 0 : _pinnedRows$bottom.reduce(function (acc, value) {
acc += apiRef.current.unstable_getRowHeight(value.id);
return acc;
}, 0)) || 0;
return {
top: topPinnedRowsHeight,
bottom: bottomPinnedRowsHeight
};
}