Skip to content

Commit

Permalink
Moved uid generation from componentDidMount to the constructor, other…
Browse files Browse the repository at this point in the history
…wise if enableOnClickOutside got called before componentDidMount (i.e. in WrappedComponent's componentDidMount) the handler got registered with under undefined uid
  • Loading branch information
Andarist committed Oct 19, 2017
1 parent 271ad5b commit 0db5dff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
36 changes: 21 additions & 15 deletions dist/react-onclickoutside.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,17 @@ var IGNORE_CLASS_NAME = 'ignore-react-onclickoutside';
* onClickOutsideHOC function defined inside setupHOC().
*/
function onClickOutsideHOC(WrappedComponent, config) {
var _class, _temp2;
var _class, _temp;

return _temp2 = _class = function (_Component) {
return _temp = _class = function (_Component) {
inherits(onClickOutside, _Component);

function onClickOutside() {
var _temp, _this, _ret;

function onClickOutside(props) {
classCallCheck(this, onClickOutside);

for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _this = possibleConstructorReturn(this, _Component.call(this, props));

return _ret = (_temp = (_this = possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.__outsideClickHandler = function (event) {
_this.__outsideClickHandler = function (event) {
if (typeof _this.__clickOutsideHandlerProp === 'function') {
_this.__clickOutsideHandlerProp(event);
return;
Expand All @@ -160,7 +156,9 @@ function onClickOutsideHOC(WrappedComponent, config) {
}

throw new Error('WrappedComponent lacks a handleClickOutside(event) function for processing outside click events.');
}, _this.enableOnClickOutside = function () {
};

_this.enableOnClickOutside = function () {
if (typeof document === 'undefined' || enabledInstances[_this._uid]) return;
enabledInstances[_this._uid] = true;

Expand Down Expand Up @@ -202,7 +200,9 @@ function onClickOutsideHOC(WrappedComponent, config) {

document.addEventListener(eventName, handlersMap[_this._uid], handlerOptions);
});
}, _this.disableOnClickOutside = function () {
};

_this.disableOnClickOutside = function () {
delete enabledInstances[_this._uid];
var fn = handlersMap[_this._uid];

Expand All @@ -216,14 +216,21 @@ function onClickOutsideHOC(WrappedComponent, config) {
});
delete handlersMap[_this._uid];
}
}, _this.getRef = function (ref) {
};

_this.getRef = function (ref) {
return _this.instanceRef = ref;
}, _temp), possibleConstructorReturn(_this, _ret);
};

_this._uid = uid();
return _this;
}

/**
* Access the WrappedComponent's instance.
*/


onClickOutside.prototype.getInstance = function getInstance() {
if (!WrappedComponent.prototype.isReactComponent) {
return this;
Expand All @@ -237,7 +244,6 @@ function onClickOutsideHOC(WrappedComponent, config) {
* linked to this component's state.
*/
onClickOutside.prototype.componentDidMount = function componentDidMount() {
this._uid = uid();
// If we are in an environment without a DOM such
// as shallow rendering or snapshots then we exit
// early to prevent any unhandled errors being thrown.
Expand Down Expand Up @@ -317,7 +323,7 @@ function onClickOutsideHOC(WrappedComponent, config) {
stopPropagation: false
}, _class.getClass = function () {
return WrappedComponent.getClass ? WrappedComponent.getClass() : WrappedComponent;
}, _temp2;
}, _temp;
}

exports.IGNORE_CLASS_NAME = IGNORE_CLASS_NAME;
Expand Down
2 changes: 1 addition & 1 deletion dist/react-onclickoutside.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export default function onClickOutsideHOC(WrappedComponent, config) {

static getClass = () => (WrappedComponent.getClass ? WrappedComponent.getClass() : WrappedComponent);

constructor(props) {
super(props);
this._uid = uid();
}

/**
* Access the WrappedComponent's instance.
*/
Expand Down Expand Up @@ -69,7 +74,6 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
* linked to this component's state.
*/
componentDidMount() {
this._uid = uid();
// If we are in an environment without a DOM such
// as shallow rendering or snapshots then we exit
// early to prevent any unhandled errors being thrown.
Expand Down

0 comments on commit 0db5dff

Please sign in to comment.