UNPKG

@mui/x-data-grid-pro

Version:

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

62 lines (60 loc) 2.53 kB
'use client'; import * as React from 'react'; import composeClasses from '@mui/utils/composeClasses'; import { getDataGridUtilityClass, useGridSelector } from '@mui/x-data-grid'; import { useGridPrivateApiContext } from "../hooks/utils/useGridPrivateApiContext.js"; import { useGridRootProps } from "../hooks/utils/useGridRootProps.js"; import { gridDetailPanelExpandedRowsContentCacheSelector, gridDetailPanelExpandedRowIdsSelector } from "../hooks/features/detailPanel/index.js"; import { GridDetailPanel } from "./GridDetailPanel.js"; import { gridDetailPanelRawHeightCacheSelector } from "../hooks/features/detailPanel/gridDetailPanelSelector.js"; import { jsx as _jsx } from "react/jsx-runtime"; const useUtilityClasses = () => { const slots = { detailPanel: ['detailPanel'] }; return composeClasses(slots, getDataGridUtilityClass, {}); }; export function GridDetailPanels(props) { const rootProps = useGridRootProps(); if (!rootProps.getDetailPanelContent) { return null; } return /*#__PURE__*/React.createElement(GridDetailPanelsImpl, props); } function GridDetailPanelsImpl({ virtualScroller }) { const apiRef = useGridPrivateApiContext(); const classes = useUtilityClasses(); const { setPanels } = virtualScroller; const expandedRowIds = useGridSelector(apiRef, gridDetailPanelExpandedRowIdsSelector); const detailPanelsContent = useGridSelector(apiRef, gridDetailPanelExpandedRowsContentCacheSelector); const detailPanelsHeights = useGridSelector(apiRef, gridDetailPanelRawHeightCacheSelector); const getDetailPanel = React.useCallback(rowId => { const content = detailPanelsContent[rowId]; // Check if the id exists in the current page const rowIndex = apiRef.current.getRowIndexRelativeToVisibleRows(rowId); const exists = rowIndex !== undefined; if (! /*#__PURE__*/React.isValidElement(content) || !exists) { return null; } const heightCache = detailPanelsHeights[rowId]; const height = heightCache.autoHeight ? 'auto' : heightCache.height; return /*#__PURE__*/_jsx(GridDetailPanel, { rowId: rowId, height: height, className: classes.detailPanel, children: content }, `panel-${rowId}`); }, [apiRef, classes.detailPanel, detailPanelsHeights, detailPanelsContent]); React.useEffect(() => { const map = new Map(); for (const rowId of expandedRowIds) { map.set(rowId, getDetailPanel(rowId)); } setPanels(map); }, [expandedRowIds, setPanels, getDetailPanel]); return null; }