import React from 'react'; export type IReactComponent
= | React.StatelessComponent
| React.ComponentClass
| React.ClassicComponentClass
; function computeHeight(node: HTMLDivElement) { node.style.height = '100%'; const totalHeight = parseInt(getComputedStyle(node).height + '', 10); const padding = parseInt(getComputedStyle(node).paddingTop + '', 10) + parseInt(getComputedStyle(node).paddingBottom + '', 10); return totalHeight - padding; } function getAutoHeight(n: HTMLDivElement) { if (!n) { return 0; } let node = n; let height = computeHeight(node); const parentNode = node.parentNode as HTMLDivElement; if (parentNode) { height = computeHeight(parentNode); } return height; } interface IAutoHeightProps { height?: number; } function autoHeight() { return function
( WrappedComponent: React.ComponentClass
| React.SFC
): React.ComponentClass
{ class AutoHeightComponent extends React.Component
{ state = { computedHeight: 0, }; root!: HTMLDivElement; componentDidMount() { const { height } = this.props; if (!height) { const h = getAutoHeight(this.root); // eslint-disable-next-line this.setState({ computedHeight: h }); if (h < 1) { const h = getAutoHeight(this.root); this.setState({ computedHeight: h }); } } } handleRoot = (node: HTMLDivElement) => { this.root = node; }; render() { const { height } = this.props; const { computedHeight } = this.state; const h = height || computedHeight; return (