All files / src/components/DropdownBox/DropdownBoxContainer index.jsx

0% Statements 0/58
0% Branches 0/37
0% Functions 0/14
0% Lines 0/23
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                                                                                                         
import React, { PureComponent } from 'react';
import classnames from 'classnames';
import is from 'is_js';
import PropTypes from 'prop-types';
 
class DropdownBoxContainer extends PureComponent {
  constructor(props) {
    super(props);
    this._onClickedOutside = this._onClickedOutside.bind(this);
    this._onRef = this._onRef.bind(this);
  }
 
  componentDidMount() {
    if (is.function(this.props.onClickOutside)) {
      document.body.addEventListener('click', this._onClickedOutside);
      document.body.addEventListener('touchstart', this._onClickedOutside);
    }
  }
 
  componentWillUnmount() {
    document.body.removeEventListener('click', this._onClickedOutside);
    document.body.removeEventListener('touchstart', this._onClickedOutside);
  }
 
  _onClickedOutside(evt) {
    if (this._node.contains(evt.target)) {
      return;
    }
 
    this.props.onClickOutside();
  }
 
  _onRef(ref) {
    this._node = ref;
  }
 
  render() {
    const { className, children } = this.props;
    return <div className={classnames(className)}
        ref={this._onRef}>
      {children}
    </div>;
  }
}
 
DropdownBoxContainer.propTypes = {
  children: PropTypes.node,
  className: PropTypes.string,
  onClickOutside: PropTypes.func,
};
 
export default DropdownBoxContainer;