forked from kentor/react-click-outside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (25 loc) · 967 Bytes
/
index.js
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
const React = require('react');
const ReactDOM = require('react-dom');
module.exports = function enhanceWithClickOutside(WrappedComponent) {
const componentName = WrappedComponent.displayName || WrappedComponent.name;
return React.createClass({
displayName: `Wrapped${componentName}`,
componentDidMount() {
this.__wrappedComponent = this.refs.wrappedComponent;
document.addEventListener('click', this.handleClickOutside, true);
},
componentWillUnmount() {
document.removeEventListener('click', this.handleClickOutside, true);
},
handleClickOutside(e) {
const domNode = ReactDOM.findDOMNode(this);
if ((!domNode || !domNode.contains(e.target)) &&
typeof this.refs.wrappedComponent.handleClickOutside === 'function') {
this.refs.wrappedComponent.handleClickOutside(e);
}
},
render() {
return <WrappedComponent {...this.props} ref="wrappedComponent" />;
},
});
};