/
index.js
39 lines (32 loc) · 962 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
32
33
34
35
36
37
38
39
import React from 'react';
export default ({ component: ComponentToMix, props }) => {
return class ClickedOutside extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
document.addEventListener('keydown', this.close);
document.addEventListener('mousedown', this.handleClickOutside);
}
close = (e) => {
if (e.keyCode === 27) {
props.close();
}
};
componentWillUnmount() {
document.removeEventListener('mousedown', this.handleClickOutside);
document.removeEventListener('keydown', this.close);
}
handleClickOutside = (e) => {
if (this.wrapperRef && !this.wrapperRef.contains(e.target)) {
props.close();
}
};
render() {
return (<div className={props.containerClassname || ''} ref={node => this.wrapperRef = node}>
<ComponentToMix {...props} {...this.props}/>
</div>
);
}
}
}