MCPcopy Create free account
hub / github.com/Lobos/react-ui /

Class

src/mixins/ClickAway.js:7–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import { isDescendant } from '../utils/dom';
6
7module.exports = (Component) => class extends Component {
8 constructor (props) {
9 super(props);
10 }
11
12 componentWillUnmount () {
13 this.unbindClickAway();
14 }
15
16 bindClickAway () {
17 const fn = this.getClickAwayEvent();
18 Events.on(document, 'click', fn);
19 Events.on(document, 'touchstart', fn);
20 }
21
22 unbindClickAway () {
23 const fn = this.getClickAwayEvent();
24 Events.off(document, 'click', fn);
25 Events.off(document, 'touchstart', fn);
26 }
27
28 registerClickAway (onClickAway, target) {
29 this.clickAwayTarget = target;
30 this.onClickAway = onClickAway;
31 }
32
33 getClickAwayEvent () {
34 let fn = this._clickAwayEvent;
35 if (!fn) {
36 fn = (event) => {
37 let el = this.clickAwayTarget || ReactDOM.findDOMNode(this);
38
39 // Check if the target is inside the current component
40 if (event.target !== el && !isDescendant(el, event.target)) {
41 if (this.onClickAway) {
42 this.onClickAway();
43 }
44 }
45 }
46 this._clickAwayEvent = fn;
47 }
48 return fn;
49 }
50};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected