UNPKG

@mui/x-data-grid

Version:

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

151 lines (148 loc) 5.47 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import * as React from 'react'; import clsx from 'clsx'; import useEventCallback from '@mui/utils/useEventCallback'; import composeClasses from '@mui/utils/composeClasses'; import { styled } from '@mui/system'; import { fastMemo } from '@mui/x-internals/fastMemo'; import { useGridRootProps } from "../hooks/utils/useGridRootProps.js"; import { getDataGridUtilityClass, gridClasses } from "../constants/index.js"; import { useGridApiContext } from "../hooks/utils/useGridApiContext.js"; import { useGridEvent } from "../hooks/utils/useGridEvent.js"; import { useGridSelector } from "../hooks/utils/useGridSelector.js"; import { gridDimensionsSelector, gridColumnsTotalWidthSelector } from "../hooks/features/dimensions/gridDimensionsSelectors.js"; import { gridDensityFactorSelector } from "../hooks/features/density/densitySelector.js"; import { useTimeout } from "../hooks/utils/useTimeout.js"; import { getTotalHeaderHeight } from "../hooks/features/columns/gridColumnsUtils.js"; import { createSelector } from "../utils/createSelector.js"; import { jsx as _jsx } from "react/jsx-runtime"; const CLIFF = 1; const SLOP = 1.5; const useUtilityClasses = ownerState => { const { scrollDirection, classes } = ownerState; const slots = { root: ['scrollArea', `scrollArea--${scrollDirection}`] }; return composeClasses(slots, getDataGridUtilityClass, classes); }; const GridScrollAreaRawRoot = styled('div', { name: 'MuiDataGrid', slot: 'ScrollArea', overridesResolver: (props, styles) => [{ [`&.${gridClasses['scrollArea--left']}`]: styles['scrollArea--left'] }, { [`&.${gridClasses['scrollArea--right']}`]: styles['scrollArea--right'] }, styles.scrollArea] })(() => ({ position: 'absolute', top: 0, zIndex: 101, width: 20, bottom: 0, [`&.${gridClasses['scrollArea--left']}`]: { left: 0 }, [`&.${gridClasses['scrollArea--right']}`]: { right: 0 } })); const offsetSelector = createSelector(gridDimensionsSelector, (dimensions, direction) => { if (direction === 'left') { return dimensions.leftPinnedWidth; } if (direction === 'right') { return dimensions.rightPinnedWidth + (dimensions.hasScrollX ? dimensions.scrollbarSize : 0); } return 0; }); function GridScrollAreaWrapper(props) { const apiRef = useGridApiContext(); const [dragging, setDragging] = React.useState(false); useGridEvent(apiRef, 'columnHeaderDragStart', () => setDragging(true)); useGridEvent(apiRef, 'columnHeaderDragEnd', () => setDragging(false)); if (!dragging) { return null; } return /*#__PURE__*/_jsx(GridScrollAreaContent, _extends({}, props)); } function GridScrollAreaContent(props) { const { scrollDirection, scrollPosition } = props; const rootRef = React.useRef(null); const apiRef = useGridApiContext(); const timeout = useTimeout(); const densityFactor = useGridSelector(apiRef, gridDensityFactorSelector); const columnsTotalWidth = useGridSelector(apiRef, gridColumnsTotalWidthSelector); const sideOffset = useGridSelector(apiRef, offsetSelector, scrollDirection); const getCanScrollMore = () => { const dimensions = gridDimensionsSelector(apiRef); if (scrollDirection === 'left') { // Only render if the user has not reached yet the start of the list return scrollPosition.current.left > 0; } if (scrollDirection === 'right') { // Only render if the user has not reached yet the end of the list const maxScrollLeft = columnsTotalWidth - dimensions.viewportInnerSize.width; return scrollPosition.current.left < maxScrollLeft; } return false; }; const [canScrollMore, setCanScrollMore] = React.useState(getCanScrollMore); const rootProps = useGridRootProps(); const ownerState = _extends({}, rootProps, { scrollDirection }); const classes = useUtilityClasses(ownerState); const totalHeaderHeight = getTotalHeaderHeight(apiRef, rootProps); const headerHeight = Math.floor(rootProps.columnHeaderHeight * densityFactor); const style = { height: headerHeight, top: totalHeaderHeight - headerHeight }; if (scrollDirection === 'left') { style.left = sideOffset; } else if (scrollDirection === 'right') { style.right = sideOffset; } const handleScrolling = () => { setCanScrollMore(getCanScrollMore); }; const handleDragOver = useEventCallback(event => { let offset; // Prevents showing the forbidden cursor event.preventDefault(); if (scrollDirection === 'left') { offset = event.clientX - rootRef.current.getBoundingClientRect().right; } else if (scrollDirection === 'right') { offset = Math.max(1, event.clientX - rootRef.current.getBoundingClientRect().left); } else { throw new Error('MUI X: Wrong drag direction'); } offset = (offset - CLIFF) * SLOP + CLIFF; // Avoid freeze and inertia. timeout.start(0, () => { apiRef.current.scroll({ left: scrollPosition.current.left + offset, top: scrollPosition.current.top }); }); }); useGridEvent(apiRef, 'scrollPositionChange', handleScrolling); if (!canScrollMore) { return null; } return /*#__PURE__*/_jsx(GridScrollAreaRawRoot, { ref: rootRef, className: clsx(classes.root), ownerState: ownerState, onDragOver: handleDragOver, style: style }); } export const GridScrollArea = fastMemo(GridScrollAreaWrapper);