All files / utils click-outside.js

0% Statements 0/53
0% Branches 0/37
0% Functions 0/14
0% Lines 0/14
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                                                                           
import React, { Component } from 'react'
import PropTypes from 'prop-types'
 
class ClickOutside extends Component {
  componentDidMount () {
    document.addEventListener('click', this.handle, true)
  }
 
  componentWillUnmount () {
    document.removeEventListener('click', this.handle, true)
  }
 
  handle = e => {
    const { onClickOutside, delayClick = 0 } = this.props
 
    if (!this.container.contains(e.target)) {
      setTimeout(onClickOutside, delayClick)
    }
  }
 
  render () {
    const { children } = this.props
 
    return (
      <div ref={node => { this.container = node }}>
        {children}
      </div>
    )
  }
}
 
ClickOutside.propTypes = {
  onClickOutside: PropTypes.func.isRequired,
  delayClick: PropTypes.number
}
 
export { ClickOutside }