|
| 1 | +var React = require('react'); |
| 2 | +var objectAssign = require('react/lib/Object.assign'); |
| 3 | +var OnClickOutsideMixin = require('react-onclickoutside'); |
| 4 | + |
| 5 | + |
| 6 | +function addClickOutsideListener(Component) { |
| 7 | + |
| 8 | + return React.createClass({ |
| 9 | + |
| 10 | + displayName: (Component.displayName || Component.name) + 'ClickOutside', |
| 11 | + |
| 12 | + mixins: [OnClickOutsideMixin], |
| 13 | + |
| 14 | + handleClickOutside: function(event) { |
| 15 | + if (this.refs.inner.handleClickOutside) { |
| 16 | + this.refs.inner.handleClickOutside(event); |
| 17 | + } |
| 18 | + else if (this.props.onClickOutside) { |
| 19 | + this.props.onClickOutside(event); |
| 20 | + } |
| 21 | + }, |
| 22 | + |
| 23 | + render: function render() { |
| 24 | + return React.createElement(Component, objectAssign({ |
| 25 | + enableOnClickOutside: this.enableOnClickOutside, |
| 26 | + disableOnClickOutside: this.disableOnClickOutside, |
| 27 | + ref: 'inner' |
| 28 | + }, this.props)); |
| 29 | + } |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * @function listensToClickOutside |
| 36 | + * |
| 37 | + * A higher-order component for ES6 React classes to use the `handleClickOutside` event handler: |
| 38 | + * |
| 39 | + * import listensToClickOutside from 'react-onclickoutside/decorator'; |
| 40 | + * |
| 41 | + * class Es6Component extends React.Component { |
| 42 | + * handleClickOutside = (event) => { |
| 43 | + * // ...handling code goes here... |
| 44 | + * } |
| 45 | + * } |
| 46 | + * |
| 47 | + * export default listensToClickOutside(Es6Component); |
| 48 | + * |
| 49 | + * Alternatively you can pass the handler down from the parent on an `onClickOutside` prop: |
| 50 | + * |
| 51 | + * class Child extends React.Component { |
| 52 | + * // No event handler here, if provided this handler takes precedence and the one passed down |
| 53 | + * // is not called automatically. If it should be, call it on the props from the child handler. |
| 54 | + * } |
| 55 | + * |
| 56 | + * Child = listenToClickOutside(Child); |
| 57 | + * |
| 58 | + * |
| 59 | + * class Parent extends React.Component { |
| 60 | + * handleClickOutside = (event) => { |
| 61 | + * // ...handling code goes here... |
| 62 | + * } |
| 63 | + * |
| 64 | + * render() { |
| 65 | + * return ( |
| 66 | + * <Child onClickOutside={this.handleClickOutside}/> |
| 67 | + * ); |
| 68 | + * } |
| 69 | + * } |
| 70 | + * |
| 71 | + * The [ES7 Decorator Pattern](https://github.com/wycats/javascript-decorators) is also supported |
| 72 | + * using the same import: |
| 73 | + * |
| 74 | + * import listensToClickOutside from 'react-onclickoutside/decorator'; |
| 75 | + * |
| 76 | + * @listensToClickOutside() |
| 77 | + * class Es6Component extends React.Component { |
| 78 | + * handleClickOutside = (event) => { |
| 79 | + * // ...handling code goes here... |
| 80 | + * } |
| 81 | + * } |
| 82 | + * |
| 83 | + * @param {React.Component} [Component] The component outside of which to listen to clicks. |
| 84 | + * @returns {React.Component} or {Function} if using the decorator pattern. |
| 85 | + */ |
| 86 | +function listensToClickOutside(Component) { |
| 87 | + // support decorator pattern |
| 88 | + if (!Component) { |
| 89 | + return function listensToClickOutsideDecorator(ComponentToDecorate) { |
| 90 | + return addClickOutsideListener(ComponentToDecorate); |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + return addClickOutsideListener(Component); |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = listensToClickOutside; |
0 commit comments