@mui/x-data-grid-pro
Version:
The Pro plan edition of the MUI X Data Grid components.
24 lines • 1.17 kB
JavaScript
import { useGridSelector, useGridEventPriority, gridVisibleColumnDefinitionsSelector, useGridEvent } from '@mui/x-data-grid';
import { useGridVisibleRows, runIf } from '@mui/x-data-grid/internals';
import useEventCallback from '@mui/utils/useEventCallback';
/**
* @requires useGridColumns (state)
* @requires useGridDimensions (method) - can be after
* @requires useGridScroll (method
*/
export const useGridInfiniteLoader = (apiRef, props) => {
const visibleColumns = useGridSelector(apiRef, gridVisibleColumnDefinitionsSelector);
const currentPage = useGridVisibleRows(apiRef, props);
const isEnabled = props.rowsLoadingMode === 'client' && !!props.onRowsScrollEnd;
const handleLoadMoreRows = useEventCallback(() => {
const viewportPageSize = apiRef.current.getViewportPageSize();
const rowScrollEndParams = {
visibleColumns,
viewportPageSize,
visibleRowsCount: currentPage.rows.length
};
apiRef.current.publishEvent('rowsScrollEnd', rowScrollEndParams);
});
useGridEventPriority(apiRef, 'rowsScrollEnd', props.onRowsScrollEnd);
useGridEvent(apiRef, 'rowsScrollEndIntersection', runIf(isEnabled, handleLoadMoreRows));
};