wix-style-react
Version:
wix-style-react
142 lines (114 loc) • 5.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var MOUSE_EVENTS_SUPPORTED = ['mouseup', 'touchend'];
var WixComponent = function (_React$PureComponent) {
_inherits(WixComponent, _React$PureComponent);
function WixComponent(params) {
_classCallCheck(this, WixComponent);
var _this = _possibleConstructorReturn(this, (WixComponent.__proto__ || Object.getPrototypeOf(WixComponent)).call(this, params));
_this._addDataHook = _this._addDataHook.bind(_this);
_this._supportOnClickOutside = _this._supportOnClickOutside.bind(_this);
_this._onMouseEventsHandler = _this._onMouseEventsHandler.bind(_this);
return _this;
}
_createClass(WixComponent, [{
key: 'checkIfEventOnElements',
value: function checkIfEventOnElements(e, elem) {
var current = e.target;
while (current.parentNode) {
if (elem.indexOf(current) > -1) {
return true;
}
current = current.parentNode;
}
return current !== document;
}
}, {
key: 'componentElements',
value: function componentElements() {
return [_reactDom2.default.findDOMNode(this)];
}
}, {
key: 'setStyles',
value: function setStyles(styles) {
var typography = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.props.styles) {
this.styles = this.props.styles;
} else {
this.styles = styles;
}
this.typography = this.props.styles || typography;
}
}, {
key: '_onMouseEventsHandler',
value: function _onMouseEventsHandler(e) {
if (!this.checkIfEventOnElements(e, this.componentElements())) {
this.onClickOutside(e);
}
}
}, {
key: '_addDataHook',
value: function _addDataHook(dataHook) {
var domNode = _reactDom2.default.findDOMNode(this);
if (domNode) {
domNode.setAttribute('data-hook', dataHook);
}
}
}, {
key: '_supportOnClickOutside',
value: function _supportOnClickOutside() {
var _this2 = this;
MOUSE_EVENTS_SUPPORTED.forEach(function (eventName) {
document.addEventListener(eventName, _this2._onMouseEventsHandler, true);
});
this._boundEvents = MOUSE_EVENTS_SUPPORTED;
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var dataHook = this.props.dataHook;
if (dataHook) {
this._addDataHook(dataHook);
}
if (typeof this.onClickOutside === 'function') {
this._supportOnClickOutside();
}
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
if (prevProps.dataHook !== this.props.dataHook) {
this._addDataHook(this.props.dataHook);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var _this3 = this;
if (this._boundEvents && typeof document !== 'undefined') {
this._boundEvents.forEach(function (eventName) {
document.removeEventListener(eventName, _this3._onMouseEventsHandler, true);
});
}
}
}]);
return WixComponent;
}(_react2.default.PureComponent);
WixComponent.propTypes = {
dataHook: _propTypes2.default.string,
styles: _propTypes2.default.string
};
exports.default = WixComponent;