autoHeight.tsx 1.91 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
import React from 'react';

export type IReactComponent<P = any> =
  | React.StatelessComponent<P>
  | React.ComponentClass<P>
  | React.ClassicComponentClass<P>;

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;
  }

  const 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<P extends IAutoHeightProps>(
    WrappedComponent: React.ComponentClass<P> | React.SFC<P>,
  ): React.ComponentClass<P> {
    class AutoHeightComponent extends React.Component<P & IAutoHeightProps> {
      state = {
        computedHeight: 0,
      };
      root!: HTMLDivElement;
      componentDidMount() {
        const { height } = this.props;
        if (!height) {
          let h = getAutoHeight(this.root);
          // eslint-disable-next-line
          this.setState({ computedHeight: h });
          if (h < 1) {
            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 (
          <div ref={this.handleRoot}>
            {h > 0 && <WrappedComponent {...this.props} height={h} />}
          </div>
        );
      }
    }
    return AutoHeightComponent;
  };
}
export default autoHeight;