All files / nexshop-web-contents/src/enhancers popup-enhancer.js

100% Statements 14/14
100% Branches 4/4
100% Functions 6/6
100% Lines 13/13
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 421x 1x     1x       7x   7x       3x   3x 2x         1x       1x       7x           1x       1x  
import React, {Component} from 'react';
import PropTypes from 'prop-types';
 
export default (ChildComponent, classNameIgnoreClick) => {
    class PopupEnhancer extends Component {
        static WrappedPopupComponent = ChildComponent;
 
        constructor(props) {
            super(props);
 
            this.handleClick = this.handleClick.bind(this);
        }
 
        handleClick (e) {
            e.stopPropagation();
 
            if (!classNameIgnoreClick || !e.path.find((node) => node.className === classNameIgnoreClick)) {
                this.props.close();
            }
        }
 
        componentDidMount () {
            window.addEventListener('click', this.handleClick);
        }
 
        componentWillUnmount () {
            window.removeEventListener('click', this.handleClick);
        }
 
        render () {
            return (
                <ChildComponent {...this.props}/>
            )
        }
    }
 
    PopupEnhancer.propTypes = {
        close: PropTypes.func.isRequired
    };
 
    return PopupEnhancer;
}