UNPKG

@mui/x-data-grid-pro

Version:

The Pro plan edition of the MUI X Data Grid components.

81 lines (80 loc) 3.38 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.findSkeletonRowsSection = exports.adjustRowParams = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _xDataGrid = require("@mui/x-data-grid"); /** * Adjusts the row fetch parameters to align with page boundaries. * - Start index is decreased to the start of the page * - End index is increased to the end of the page (capped by rowCount - 1 if defined) */ const adjustRowParams = (params, options) => { if (typeof params.start !== 'number') { return params; } const { pageSize, rowCount } = options; const adjustedStart = params.start - params.start % pageSize; const pageAlignedEnd = params.end + pageSize - params.end % pageSize - 1; // rowCount of -1 means "unknown/infinite", treat same as undefined (no capping). // The first page is never capped: it's always fetched as `{start: 0, end: pageSize - 1}` // (rowCount is typically unknown at that point), so capping it here once rowCount // becomes known would produce a different range and bust the cache for the // post-render revalidation request, causing a spurious second `getRows` call. const shouldCapByRowCount = rowCount !== undefined && rowCount !== -1 && (rowCount === 0 || adjustedStart > 0); const maxEnd = shouldCapByRowCount ? Math.max(0, rowCount - 1) : Infinity; return (0, _extends2.default)({}, params, { start: adjustedStart, end: Math.min(maxEnd, pageAlignedEnd) }); }; exports.adjustRowParams = adjustRowParams; const findSkeletonRowsSection = ({ apiRef, visibleRows, range }) => { let { firstRowIndex, lastRowIndex } = range; const visibleRowsSection = visibleRows.slice(range.firstRowIndex, range.lastRowIndex); if (visibleRowsSection.length === 0) { return undefined; } // The slice may be shorter than `lastRowIndex - firstRowIndex` (e.g., after a // collapse shrinks visible rows below the cached viewport range). Clamp down // so the external indices stay in lockstep with the slice's bounds; otherwise // the returned `lastRowIndex` would point past the slice while `endIndex` only // reaches `visibleRowsSection.length - 1`, drifting the two apart in the loop // below and making the caller fetch a wrong range. lastRowIndex = firstRowIndex + visibleRowsSection.length; let startIndex = 0; let endIndex = visibleRowsSection.length - 1; let isSkeletonSectionFound = false; while (!isSkeletonSectionFound && firstRowIndex < lastRowIndex) { const isStartingWithASkeletonRow = (0, _xDataGrid.gridRowNodeSelector)(apiRef, visibleRowsSection[startIndex].id)?.type === 'skeletonRow'; const isEndingWithASkeletonRow = (0, _xDataGrid.gridRowNodeSelector)(apiRef, visibleRowsSection[endIndex].id)?.type === 'skeletonRow'; if (isStartingWithASkeletonRow && isEndingWithASkeletonRow) { isSkeletonSectionFound = true; } if (!isStartingWithASkeletonRow) { startIndex += 1; firstRowIndex += 1; } if (!isEndingWithASkeletonRow) { endIndex -= 1; lastRowIndex -= 1; } } return isSkeletonSectionFound ? { firstRowIndex, lastRowIndex } : undefined; }; exports.findSkeletonRowsSection = findSkeletonRowsSection;