@bigfishtv/cockpit
Version:
96 lines (74 loc) • 3.42 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }
import React, { Component } from 'react';
import classnames from 'classnames';
import newId from '../../utils/newId';
export var modalHandler = {};
var ModalHost = function (_Component) {
_inherits(ModalHost, _Component);
function ModalHost(props) {
_classCallCheck(this, ModalHost);
var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_this.handleKeyDown = function (event) {
if (event.key === 'Escape') {
_this.removeTopModal();
}
};
_this.addModal = function (modal) {
modal.id = newId();
if (modal.closable === undefined) {
modal.closable = true;
}
_this.setState({
modals: _this.state.modals.concat([modal])
});
return modal;
};
_this.removeModal = function (modal) {
_this.setState({
modals: _this.state.modals.filter(function (m) {
return m !== modal;
})
});
};
_this.removeTopModal = function () {
if (_this.state.modals.length) {
var modal = _this.state.modals[_this.state.modals.length - 1];
if (modal.closable) {
_this.removeModal(_this.state.modals[_this.state.modals.length - 1]);
}
}
};
_this.state = { modals: [] };
modalHandler.add = _this.addModal;
modalHandler.remove = _this.removeModal;
modalHandler.removeTop = _this.removeTopModal;
return _this;
}
ModalHost.prototype.componentDidMount = function componentDidMount() {
document.addEventListener('keydown', this.handleKeyDown);
};
ModalHost.prototype.componentWillUnmount = function componentWillUnmount() {
document.removeEventListener('keydown', this.handleKeyDown);
};
ModalHost.prototype.render = function render() {
var _this2 = this;
var modals = this.state.modals;
var active = modals.length > 0;
return React.createElement(
'div',
{ className: classnames('modal', { hide: !active }) },
React.createElement('div', { className: 'modal-background', onClick: this.removeTopModal }),
modals.map(function (modal) {
var Component = modal.Component,
props = modal.props,
id = modal.id;
return React.createElement(Component, _extends({ key: id }, props, { closeModal: _this2.removeModal.bind(_this2, modal) }));
})
);
};
return ModalHost;
}(Component);
export { ModalHost as default };