UNPKG

@mui/x-data-grid-pro

Version:

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

63 lines (62 loc) 2.35 kB
"use strict"; 'use client'; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGridRowPinning = exports.rowPinningStateInitializer = 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"); function createPinnedRowsInternalCache(pinnedRows, getRowId) { const cache = { topIds: [], bottomIds: [], idLookup: {} }; pinnedRows?.top?.forEach(rowModel => { const id = (0, _internals.getRowIdFromRowModel)(rowModel, getRowId); cache.topIds.push(id); cache.idLookup[id] = rowModel; }); pinnedRows?.bottom?.forEach(rowModel => { const id = (0, _internals.getRowIdFromRowModel)(rowModel, getRowId); cache.bottomIds.push(id); cache.idLookup[id] = rowModel; }); return cache; } const rowPinningStateInitializer = (state, props, apiRef) => { apiRef.current.caches.pinnedRows = createPinnedRowsInternalCache(props.pinnedRows, props.getRowId); return (0, _extends2.default)({}, state, { rows: (0, _extends2.default)({}, state.rows, { additionalRowGroups: (0, _extends2.default)({}, state.rows?.additionalRowGroups, { pinnedRows: { top: [], bottom: [] } }) }) }); }; exports.rowPinningStateInitializer = rowPinningStateInitializer; const useGridRowPinning = (apiRef, props) => { const setPinnedRows = React.useCallback(newPinnedRows => { apiRef.current.caches.pinnedRows = createPinnedRowsInternalCache(newPinnedRows, props.getRowId); apiRef.current.requestPipeProcessorsApplication('hydrateRows'); }, [apiRef, props.getRowId]); (0, _xDataGrid.useGridApiMethod)(apiRef, { unstable_setPinnedRows: setPinnedRows }, 'public'); const isFirstRender = React.useRef(true); React.useEffect(() => { if (isFirstRender.current) { isFirstRender.current = false; return; } apiRef.current.unstable_setPinnedRows(props.pinnedRows); }, [apiRef, props.pinnedRows]); }; exports.useGridRowPinning = useGridRowPinning;