@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
70 lines • 2.83 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2007-2018 GoodData Corporation
var React = require("react");
var OutsideClickHandler = /** @class */ (function (_super) {
__extends(OutsideClickHandler, _super);
function OutsideClickHandler() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.wrapperEl = null;
_this.setWrapperEl = function (el) {
_this.wrapperEl = el;
};
_this.handleClick = function (e) {
if (!_this.wrapperEl) {
// In IE11 the wrapperEl is not initialized for some reason.
return;
}
if (_this.wrapperEl.contains(e.target)) {
return;
}
if (_this.props.onOutsideClick) {
_this.props.onOutsideClick(e);
}
};
_this.addListeners = function () {
document.addEventListener("click", _this.handleClick, _this.props.useCapture);
};
_this.removeListeners = function () {
document.removeEventListener("click", _this.handleClick, _this.props.useCapture);
};
return _this;
}
OutsideClickHandler.prototype.componentDidUpdate = function (prevProps) {
if (prevProps.onOutsideClick !== this.props.onOutsideClick ||
prevProps.useCapture !== this.props.useCapture) {
this.removeListeners();
this.addListeners();
}
};
OutsideClickHandler.prototype.componentDidMount = function () {
this.addListeners();
};
OutsideClickHandler.prototype.componentWillUnmount = function () {
this.removeListeners();
};
OutsideClickHandler.prototype.render = function () {
return React.createElement("div", { ref: this.setWrapperEl }, this.props.children);
};
OutsideClickHandler.defaultProps = {
// Set to true by default so that a `stopPropagation` in the
// children will not prevent all outside click handlers from firing
useCapture: true,
};
return OutsideClickHandler;
}(React.Component));
exports.default = OutsideClickHandler;
//# sourceMappingURL=OutsideClickHandler.js.map