| | |
| | import * as React from "react"; |
| | import PropTypes from "prop-types"; |
| | import ResizeObserver from "resize-observer-polyfill"; |
| | import clsx from "clsx"; |
| | import type { ReactRef } from "../ReactGridLayoutPropTypes"; |
| |
|
| | type WPDefaultProps = {| |
| | measureBeforeMount: boolean |
| | |}; |
| |
|
| | |
| | type WPProps = {| |
| | className?: string, |
| | style?: Object, |
| | ...WPDefaultProps |
| | |}; |
| |
|
| | type WPState = {| |
| | width: number |
| | |}; |
| |
|
| | type ComposedProps<Config> = {| |
| | ...Config, |
| | measureBeforeMount?: boolean, |
| | className?: string, |
| | style?: Object, |
| | width?: number |
| | |}; |
| |
|
| | const layoutClassName = "react-grid-layout"; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | export default function WidthProvideRGL<Config>( |
| | ComposedComponent: React.AbstractComponent<Config> |
| | ): React.AbstractComponent<ComposedProps<Config>> { |
| | return class WidthProvider extends React.Component< |
| | ComposedProps<Config>, |
| | WPState |
| | > { |
| | static defaultProps: WPDefaultProps = { |
| | measureBeforeMount: false |
| | }; |
| |
|
| | static propTypes = { |
| | |
| | |
| | measureBeforeMount: PropTypes.bool |
| | }; |
| |
|
| | state: WPState = { |
| | width: 1280 |
| | }; |
| |
|
| | elementRef: ReactRef<HTMLDivElement> = React.createRef(); |
| | mounted: boolean = false; |
| | resizeObserver: ResizeObserver; |
| |
|
| | componentDidMount() { |
| | this.mounted = true; |
| | this.resizeObserver = new ResizeObserver(entries => { |
| | const node = this.elementRef.current; |
| | if (node instanceof HTMLElement) { |
| | const width = entries[0].contentRect.width; |
| | this.setState({ width }); |
| | } |
| | }); |
| | const node = this.elementRef.current; |
| | if (node instanceof HTMLElement) { |
| | this.resizeObserver.observe(node); |
| | } |
| | } |
| |
|
| | componentWillUnmount() { |
| | this.mounted = false; |
| | const node = this.elementRef.current; |
| | if (node instanceof HTMLElement) { |
| | this.resizeObserver.unobserve(node); |
| | } |
| | this.resizeObserver.disconnect(); |
| | } |
| |
|
| | render() { |
| | const { measureBeforeMount, ...rest } = this.props; |
| | if (measureBeforeMount && !this.mounted) { |
| | return ( |
| | <div |
| | className={clsx(this.props.className, layoutClassName)} |
| | style={this.props.style} |
| | // $FlowIgnore ref types |
| | ref={this.elementRef} |
| | /> |
| | ); |
| | } |
| |
|
| | return ( |
| | <ComposedComponent |
| | innerRef={this.elementRef} |
| | {...rest} |
| | {...this.state} |
| | /> |
| | ); |
| | } |
| | }; |
| | } |
| |
|