UNPKG

@mui/x-data-grid

Version:

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

126 lines (121 loc) 4.96 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import useLazyRef from '@mui/utils/useLazyRef'; import { useStoreEffect } from '@mui/x-internals/store'; import { gridFilteredTopLevelRowCountSelector } from "../filter/index.js"; import { useGridLogger, useGridApiMethod, useGridEvent } from "../../utils/index.js"; import { useGridRegisterPipeProcessor } from "../../core/pipeProcessing/index.js"; import { gridPaginationRowCountSelector, gridPaginationMetaSelector, gridPaginationModelSelector } from "./gridPaginationSelector.js"; export const useGridRowCount = (apiRef, props) => { const logger = useGridLogger(apiRef, 'useGridRowCount'); const previousPageSize = useLazyRef(() => gridPaginationModelSelector(apiRef).pageSize); apiRef.current.registerControlState({ stateId: 'paginationRowCount', propModel: props.rowCount, propOnChange: props.onRowCountChange, stateSelector: gridPaginationRowCountSelector, changeEvent: 'rowCountChange' }); /** * API METHODS */ const setRowCount = React.useCallback(newRowCount => { const rowCountState = gridPaginationRowCountSelector(apiRef); if (rowCountState === newRowCount) { return; } logger.debug("Setting 'rowCount' to", newRowCount); apiRef.current.setState(state => _extends({}, state, { pagination: _extends({}, state.pagination, { rowCount: newRowCount }) })); }, [apiRef, logger]); const paginationRowCountApi = { setRowCount }; useGridApiMethod(apiRef, paginationRowCountApi, 'public'); /** * PRE-PROCESSING */ const stateExportPreProcessing = React.useCallback((prevState, context) => { const exportedRowCount = gridPaginationRowCountSelector(apiRef); const shouldExportRowCount = // Always export if the `exportOnlyDirtyModels` property is not activated !context.exportOnlyDirtyModels || // Always export if the `rowCount` is controlled props.rowCount != null || // Always export if the `rowCount` has been initialized props.initialState?.pagination?.rowCount != null; if (!shouldExportRowCount) { return prevState; } return _extends({}, prevState, { pagination: _extends({}, prevState.pagination, { rowCount: exportedRowCount }) }); }, [apiRef, props.rowCount, props.initialState?.pagination?.rowCount]); const stateRestorePreProcessing = React.useCallback((params, context) => { const restoredRowCount = context.stateToRestore.pagination?.rowCount ? context.stateToRestore.pagination.rowCount : gridPaginationRowCountSelector(apiRef); apiRef.current.setState(state => _extends({}, state, { pagination: _extends({}, state.pagination, { rowCount: restoredRowCount }) })); return params; }, [apiRef]); useGridRegisterPipeProcessor(apiRef, 'exportState', stateExportPreProcessing); useGridRegisterPipeProcessor(apiRef, 'restoreState', stateRestorePreProcessing); /** * EVENTS */ const handlePaginationModelChange = React.useCallback(model => { if (props.paginationMode === 'client' || !previousPageSize.current) { return; } if (model.pageSize !== previousPageSize.current) { previousPageSize.current = model.pageSize; const rowCountState = gridPaginationRowCountSelector(apiRef); if (rowCountState === -1) { // Row count unknown and page size changed, reset the page apiRef.current.setPage(0); } } }, [props.paginationMode, previousPageSize, apiRef]); useGridEvent(apiRef, 'paginationModelChange', handlePaginationModelChange); /** * EFFECTS */ React.useEffect(() => { if (props.paginationMode === 'server' && props.rowCount != null) { apiRef.current.setRowCount(props.rowCount); } }, [apiRef, props.paginationMode, props.rowCount]); useStoreEffect( // typings not supported currently, but methods work apiRef.current.store, () => { const isLastPage = gridPaginationMetaSelector(apiRef).hasNextPage === false; if (isLastPage) { return true; } if (props.paginationMode === 'client') { return gridFilteredTopLevelRowCountSelector(apiRef); } return undefined; }, (_, isLastPageOrRowCount) => { if (isLastPageOrRowCount === true && gridPaginationRowCountSelector(apiRef) !== -1) { const visibleTopLevelRowCount = gridFilteredTopLevelRowCountSelector(apiRef); const paginationModel = gridPaginationModelSelector(apiRef); apiRef.current.setRowCount(paginationModel.pageSize * paginationModel.page + visibleTopLevelRowCount); } else if (typeof isLastPageOrRowCount === 'number') { apiRef.current.setRowCount(isLastPageOrRowCount); } }); React.useEffect(() => { if (props.paginationMode === 'client') { apiRef.current.setRowCount(gridFilteredTopLevelRowCountSelector(apiRef)); } }, [apiRef, props.paginationMode]); };