import * as React from 'react' import styled from 'styled-components' import { spacing, spacingPx, useResizeObserver } from '@planview/pv-utilities' type LayoutProps = { children: React.ReactNode /** * Requested number of columns. * Will collapse if there is not enough space to fit element with at least min-width (140px) + gutter (spacing.medium) */ columns?: number } export const COLUMN_MIN_WIDTH = 140 export const getColumnCount = ( c: number, availableWidth: number, colMinWidth = COLUMN_MIN_WIDTH ): number => { let ret: number = c while (ret * (colMinWidth + spacing.medium) > availableWidth && ret !== 1) { ret-- } return ret } const Wrap = styled.div<{ $columns: number }>` min-width: ${COLUMN_MIN_WIDTH}px; display: grid; grid-auto-flow: row; align-content: start; grid-template-columns: repeat(${(props) => props.$columns}, 1fr); grid-column-gap: ${spacingPx.medium}; ` /** * Implementation of the [Form Layout specification](https://design.planview.com/components/forms/form-layout) * * `import { Layout } from '@planview/pv-form'` * * Form layouts are used to arrange fields within a form using standard spacing. * By default, fields are stacked vertically in one column, * but multiple column layouts are also supported. */ const Layout: React.FC = ({ children, columns = 1 }) => { const ref = React.useRef(null) const { width: resizeWidth } = useResizeObserver({ ref, }) const [initialWidth, setInitialWidth] = React.useState(0) const width = resizeWidth || initialWidth React.useLayoutEffect(() => { if (ref.current) { setInitialWidth(ref.current.getBoundingClientRect().width) } }, []) return ( {children} ) } export { Layout } export default Layout