UNPKG

@darwino/darwino-react

Version:

A set of Javascript classes and utilities

230 lines (190 loc) 5.79 kB
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } /* * (c) Copyright Darwino Inc. 2014-2017. */ import React, { Component } from "react"; import PropTypes from 'prop-types'; /* * Common dialog component. */ class BaseDialog extends Component { // Context to publish the current parent dialog getChildContext() { return { dialog: this }; } constructor(props, context) { super(props, context); this.actionListeners = []; this.state = { show: false }; } componentDidMount() { BaseDialog.dialogs.push(BaseDialog.dialog); BaseDialog.dialog = this; } componentWillUnmount() { BaseDialog.dialog = BaseDialog.dialogs.pop(); } alert(params) { var { message, title } = params, options = _objectWithoutProperties(params, ["message", "title"]); this.show(_objectSpread({ title: title || "Alert", body: /*#__PURE__*/React.createElement("div", null, message) }, options, { actions: [BaseDialog.OkAction] })); } okCancel(params) { var { message, title } = params, options = _objectWithoutProperties(params, ["message", "title"]); this.show(_objectSpread({ title: title || "Confirmation", body: /*#__PURE__*/React.createElement("div", null, message) }, options, { actions: [BaseDialog.OkAction, BaseDialog.CancelAction] })); } yesNo(params) { var { message, title } = params, options = _objectWithoutProperties(params, ["message", "title"]); this.show(_objectSpread({ title: title || "Confirmation", body: /*#__PURE__*/React.createElement("div", null, message) }, options, { actions: [BaseDialog.YesAction, BaseDialog.NoAction] })); } prompt(params) { var { title, value } = params, options = _objectWithoutProperties(params, ["title", "value"]); this.show(_objectSpread({ title: title || "Enter a Value", value: value !== undefined ? value : "", body: this.promptDialog.bind(this), actions: [BaseDialog.OkAction, BaseDialog.CancelAction] }, options)); } promptDialog(dialog) {// Should be overriden to adapt to the proper style } picker(params) { var { value, multiple, picker, title } = params, options = _objectWithoutProperties(params, ["value", "multiple", "picker", "title"]); var v = multiple ? value || [] : value; var p = typeof picker === "function" ? picker() : picker; this.show(_objectSpread({ title: title || (multiple ? "Select Values" : "Select a Value"), value: v, body: p, multiple, actions: [BaseDialog.OkAction, BaseDialog.CancelAction] }, options)); } form(params) { var { title, form } = params, options = _objectWithoutProperties(params, ["title", "form"]); var f = typeof form === "function" ? form() : form; this.show(_objectSpread({ title: title || "Confirmation", body: f, actions: [BaseDialog.OkAction, BaseDialog.CancelAction] }, options)); } show(options) { this.setState(_objectSpread({ show: true }, options)); } hide(action) { this.setState({ show: false }); if (this.state.onHide) { this.state.onHide(action); } } close(action) { if (action) { var { onAction, onClose, value } = this.state; if (onAction) { onAction(action, value); return; } else if (onClose) { if (onClose(action, value) === false) { return; } } } this.hide(action); } getDomElement() {} } _defineProperty(BaseDialog, "childContextTypes", { dialog: PropTypes.object }); _defineProperty(BaseDialog, "CANCEL", 0); _defineProperty(BaseDialog, "OK", 1); _defineProperty(BaseDialog, "NO", 2); _defineProperty(BaseDialog, "YES", 3); _defineProperty(BaseDialog, "CancelAction", { title: "Cancel", btStyle: "link", action: function action(dialog, e) { dialog.close(BaseDialog.CANCEL); } }); _defineProperty(BaseDialog, "OkAction", { title: "Ok", type: "submit", action: function action(dialog, e) { e.preventDefault(); dialog.close(BaseDialog.OK); } }); _defineProperty(BaseDialog, "NoAction", { title: "No", btStyle: "default", action: function action(dialog, e) { dialog.close(BaseDialog.NO); } }); _defineProperty(BaseDialog, "YesAction", { title: "Yes", action: function action(dialog, e) { dialog.close(BaseDialog.YES); } }); _defineProperty(BaseDialog, "dialog", null); _defineProperty(BaseDialog, "dialogs", []); export default BaseDialog; //# sourceMappingURL=BaseDialog.js.map