UNPKG

doj-react-adminlte

Version:

Simple and easy-to-use AdminLTE components for React

325 lines (255 loc) 11.8 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _reactTransitionGroup = require("react-transition-group"); var _ModalHeader = _interopRequireDefault(require("./ModalHeader")); var _ModalFooter = _interopRequireDefault(require("./ModalFooter")); 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 _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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var ModalBody = function ModalBody(_ref) { var children = _ref.children; return children || null; }; var Modal = /*#__PURE__*/function (_React$Component) { _inherits(Modal, _React$Component); var _super = _createSuper(Modal); function Modal(props) { var _this; _classCallCheck(this, Modal); _this = _super.call(this, props); _this.updateDimensions = function () { if (_this.props.fixedScroll) { var maxWidth = 600; if (_this.props.size === 'large') { maxWidth = 1100; } else if (_this.props.size === 'small') { maxWidth = 300; } var isSmall = window.innerWidth < 768; _this.setState({ fixedBodyHeight: window.innerHeight - (isSmall ? 141 : 182), fixedWidth: window.innerWidth - 21, fixedMaxWidth: maxWidth }); } if (_this.bodyRef) { _this.setState({ bodyHeight: _this.bodyRef.clientHeight }); } }; _this.handleBackdropClick = function (event) { var _this$props = _this.props, closeOnBackdropClick = _this$props.closeOnBackdropClick, onCloseClick = _this$props.onCloseClick; if (closeOnBackdropClick && event.target === _this.backdropRef) { onCloseClick(); } }; _this.handleEscapeKeypress = function (event) { event = event || window.event; var isEscape = false; if ("key" in event) { isEscape = event.key === "Escape" || event.key === "Esc"; } else { isEscape = event.keyCode === 27; } if (isEscape) { _this.props.onCloseClick(); } }; _this.handleEnter = function () { var scrollbarWidth = Modal.getScrollbarWidth(); document.body.classList.add('modal-open'); if (scrollbarWidth) { document.body.style.paddingRight = scrollbarWidth + 'px'; } _this.setState({ exited: false }); // Add escape key event listener var closeOnEscapeKey = _this.props.closeOnEscapeKey; if (closeOnEscapeKey) { window.addEventListener('keyup', _this.handleEscapeKeypress); } _this.props.onEnter(); }; _this.handleExited = function () { _this.setState({ exited: true }); _this.cleanupOnExit(); _this.props.onExit(); }; _this.cleanupOnExit = function () { document.body.classList.remove('modal-open'); document.body.style.paddingRight = null; // Remove escape key event listener window.removeEventListener('keyup', _this.handleEscapeKeypress); }; _this.state = { mounted: false, exited: true, fixedWidth: 0, fixedHeight: 0, fixedMaxWidth: 0, bodyHeight: 0 }; _this.bodyRef = null; _this.backdropRef = null; _this.setBodyRef = function (element) { _this.bodyRef = element; }; _this.setBackdropRef = function (element) { _this.backdropRef = element; }; return _this; } _createClass(Modal, [{ key: "componentDidMount", value: function componentDidMount() { this.setState({ mounted: true }); window.addEventListener('resize', this.updateDimensions); this.updateDimensions(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('resize', this.updateDimensions); this.cleanupOnExit(); } }, { key: "render", value: function render() { var _this2 = this; var children = _react.default.Children.toArray(this.props.children); var header = children.find(function (child) { return child.type === _ModalHeader.default; }); if (header) { header = /*#__PURE__*/_react.default.cloneElement(header, { onCloseClick: this.props.onCloseClick }); } var body = children.find(function (child) { return child.type === ModalBody; }); var footer = children.find(function (child) { return child.type === _ModalFooter.default; }); var dialogClass = "default", dialogStyle = null, bodyStyle = null; if (this.props.size === 'large') { dialogClass = 'modal-lg'; } else if (this.props.size === 'small') { dialogClass = 'modal-sm'; } if (this.props.fixedScroll) { dialogStyle = { width: this.state.fixedWidth, maxWidth: this.state.fixedMaxWidth }; bodyStyle = { width: '100%', maxHeight: this.state.fixedBodyHeight, overflow: 'auto' }; } var isLoading = this.props.isLoading; return /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.CSSTransition, { in: this.state.mounted && this.props.show, timeout: 150, classNames: "modal", onEnter: this.handleEnter, onEntering: this.updateDimensions, onExited: this.handleExited, unmountOnExit: true }, function (state) { return /*#__PURE__*/_react.default.createElement("div", { className: "modal " + (_this2.props.className || ''), ref: _this2.setBackdropRef, onClick: _this2.handleBackdropClick }, /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.CSSTransition, { in: state === 'entering' || state === 'entered', timeout: 300, classNames: 'modal-dialog', unmountOnExit: true }, /*#__PURE__*/_react.default.createElement("div", { className: "modal-dialog ".concat(dialogClass, " ") + (_this2.props.dialogClassName || ''), style: dialogStyle }, /*#__PURE__*/_react.default.createElement("div", { className: "modal-content" }, header, body && /*#__PURE__*/_react.default.createElement("div", { className: "modal-body", style: bodyStyle, ref: _this2.setBodyRef }, isLoading && /*#__PURE__*/_react.default.createElement("div", { className: "modal-overlay" }, /*#__PURE__*/_react.default.createElement("i", { className: "fa fa-spinner fa-spin" })), body), footer)))); }); } }], [{ key: "getScrollbarWidth", value: function getScrollbarWidth() { return window.innerWidth - document.documentElement.clientWidth; } }]); return Modal; }(_react.default.Component); Modal.defaultProps = { closeOnBackdropClick: true, closeOnEscapeKey: true, fixedScroll: false, isLoading: false, onCloseClick: function onCloseClick() {}, onEnter: function onEnter() {}, onExit: function onExit() {}, show: false, size: 'default' }; Modal.propTypes = process.env.NODE_ENV !== "production" ? { /** className gets and sets the value of the class attribute of the specified element. You can also add a CSS class in this prop to style a particular element.*/ className: _propTypes.default.string, /** Set to true to close the modal by just pressing the backdrop.*/ closeOnBackdropClick: _propTypes.default.bool, /** Set to true to close the modal by pressing Escape key.*/ closeOnEscapeKey: _propTypes.default.bool, /** dialogClassName is a css class to apply to the Modal Dialog.*/ dialogClassName: _propTypes.default.string, /** Set to true to show a scrollbar.*/ fixedScroll: _propTypes.default.bool, /** Returns a boolean value for a postback or loading a content.*/ isLoading: _propTypes.default.bool, /** The onCloseClick prop allows passing a function that will be invoked when the close button is clicked.*/ onCloseClick: _propTypes.default.func, /** The onEnter prop allows binding a function that will be invoked when the Enter button is clicked.*/ onEnter: _propTypes.default.func, /** The function that is binded in this event will be invoked when the user exits the Modal.*/ onExit: _propTypes.default.func, /** Set to true to automatically show the modal.*/ show: _propTypes.default.bool, /** You can set the size of the Modal in this prop.*/ size: _propTypes.default.oneOf(['default', 'large', 'small']) } : {}; Modal.Header = _ModalHeader.default; Modal.Body = ModalBody; Modal.Footer = _ModalFooter.default; var _default = Modal; exports.default = _default;