# All files / elements/utils Hideable.js

100% Statements 13/13
100% Branches 2/2
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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

    6x
    277x
    277x
    153x

    124x

    6x

    6x

    6x

    3x
    3x

    1x

    1x

    1x

     import * as React from 'react';
    import PropTypes from 'prop-types';

    const Hideable = props => {
      const { hide, children } = props;
      if (hide) {
        return null;
      }
      return (
        <>
          {children}
        </>
      );
    };

    Hideable.propTypes = {
      children: PropTypes.any.isRequired,
      hide: PropTypes.bool,
    };

    Hideable.defaultProps = {
      hide: false,
    };

    /**
     * Decorator to compose a Child (target) component within Hideable.
     * @param {object} Child The target or child component to be composed within Hideable. The hide attribute is used to hide the Child.
     * @return {object} Returns the decorated object; The Child component is now composed within a Hideable.
     */

    const withHideable = Child => {
      function HideableComponent(props) {
        const { hide, ...otherProps } = props;
        return (<Hideable hide={hide}><Child {...otherProps} /></Hideable>);
      }
      HideableComponent.propTypes = {
        hide: PropTypes.bool,
      };
      HideableComponent.defaultProps = {
        hide: false,
      };
      return HideableComponent;
    };

    export { Hideable, withHideable };