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