import React, { useRef, useState, useEffect } from 'react'; import RGL, { WidthProvider } from 'react-grid-layout'; import { getPrefixedClassName } from '../utils'; import { LayoutProps } from './types'; const ReactGridLayout = WidthProvider(RGL); const Layout: React.FC = (props) => { const { layouts, resizable, draggable, margin = [0, 0], children, onLayoutChange } = props; const containerRef = useRef(); const [rowHeight, setRowHeight] = useState(0); const changRowHeight = () => { /** * Split height to 12 grids * If there is the y margin prop, it will have 11 actual y margins, which need to be subtracted from the container height. */ setRowHeight((containerRef.current.clientHeight - margin[1] * 11) / 12); }; useEffect(() => { // Monitor window resizing triggering canvas container change window.addEventListener('resize', changRowHeight); return () => { window.removeEventListener('resize', changRowHeight); }; }, []); useEffect(() => { if (containerRef.current) { changRowHeight(); } }, [containerRef]); return (
{!!rowHeight && ( {children} )}
); }; export default Layout;