# All files / utils hoc.js

88% Statements 22/25
76.47% Branches 13/17
100% Functions 9/9
95.24% Lines 20/21

    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

    6x

    11x
    11x

    11x

    4x
    4x

    4x
    4x

    4x

    4x

    3x

    1x

    2x

    1x
    1x

    2x

    12x
    12x

    15x

    12x

     /* eslint-disable  */
    import React, { Component } from 'react';

    const withAutoScroll = ({ direction, threshold, lockable }) => (WrappedComponent) => class extends Component {
      constructor(props) {
        super(props);
        this.state = {
          scrollBottom: 0,
        };
        this.loadRef = this.loadRef.bind(this);
      }

      componentWillReceiveProps() {
        Iif (!this.mlistRef) { return; }
        this.setState({
          scrollBottom: this.getBottom(this.mlistRef),
        });
      }

      componentDidUpdate() {
        const e = this.mlistRef;
        Iif (!e) { return; }

        const bottom = this.getBottom(e);

        if (threshold === '100%' ||
          ((direction === 'bottom' && bottom > threshold) ||
           (direction === 'top' && bottom < threshold)
          )
        ) {
          if (direction === 'bottom') {
            // scroll to bottom
            e.scrollTop = e.scrollHeight;
          } else {
            // scroll to top
            e.scrollTop = 0;
          }
        } else Eif (lockable === true) {
          e.scrollTop = e.scrollHeight - e.offsetHeight - this.state.scrollBottom;
        }
      }

      getBottom(e) {
        return e.scrollHeight - e.scrollTop - e.offsetHeight;
      }

      loadRef(ref) {
        this.mlistRef = ref;
        Iif (this.props.cmpRef) {
          this.props.cmpRef(ref);
        }
      }

      render() {
        return (
          <WrappedComponent
            cmpRef={(el) => this.loadRef(el)}
            {...this.props}
          />
        );
      }
    };

    export {
      withAutoScroll,
    };