UNPKG

@bigfishtv/cockpit

Version:

107 lines (89 loc) 4.11 kB
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; }; var _class, _temp; 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 PropTypes from 'prop-types'; import React, { Component } from 'react'; import { Input } from '@bigfishtv/react-forms'; import Modal from '../modal/Modal'; import ConfirmCancel from './ConfirmCancel'; // we define this because react-docgen fails when defaultProp directly references an imported component var DefaultButtons = function DefaultButtons(props) { return React.createElement(ConfirmCancel, props); }; var PromptInputModal = (_temp = _class = function (_Component) { _inherits(PromptInputModal, _Component); function PromptInputModal(props) { _classCallCheck(this, PromptInputModal); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _this.handleCallback = function (result) { if (result) result = _this.state.value; _this.props.callback(result); _this.props.closeModal(); }; _this.handleChange = function (newValue) { var value = typeof newValue !== 'string' && newValue['target'] ? newValue.target.value : newValue; _this.setState({ value: value }); }; _this.handleSubmit = function (event) { event.preventDefault(); _this.handleCallback(true); return false; }; _this.state = { value: props.defaultValue }; return _this; } PromptInputModal.prototype.render = function render() { var _this2 = this; var _props = this.props, title = _props.title, message = _props.message, style = _props.style, Buttons = _props.Buttons, InputModel = _props.InputModel; return React.createElement( Modal, _extends({}, this.props, { size: 'xsmall', title: title, callback: this.handleCallback, style: style, onClose: function onClose() { return _this2.props.closeModal(); }, ModalActions: Buttons }), message, React.createElement( 'form', { onSubmit: this.handleSubmit }, React.createElement(InputModel, { onChange: this.handleChange, value: this.state.value }) ) ); }; return PromptInputModal; }(Component), _class.propTypes = { /** What input component to use, defaults to */ InputModel: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), /** What component to use for tray buttons, default being ConfirmCancel */ Buttons: PropTypes.func, /** Title string or react node */ title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** Message string or react node */ message: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), /** Default value for input component -- node */ defaultValue: PropTypes.node, /** Callback function, will return input value or false if prompt is cancelled */ callback: PropTypes.func }, _class.defaultProps = { InputModel: Input, Buttons: DefaultButtons, title: 'Input required', style: 'primary', message: null, defaultValue: null, callback: function callback() { return console.warn('[PromptInputModal] no callback prop'); } }, _temp); export { PromptInputModal as default };