@bigfishtv/cockpit
Version:
125 lines (97 loc) • 4.69 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
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;
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactForms = require('@bigfishtv/react-forms');
var _Modal = require('../modal/Modal');
var _Modal2 = _interopRequireDefault(_Modal);
var _ConfirmCancel = require('./ConfirmCancel');
var _ConfirmCancel2 = _interopRequireDefault(_ConfirmCancel);
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; }
// we define this because react-docgen fails when defaultProp directly references an imported component
var DefaultButtons = function DefaultButtons(props) {
return _react2.default.createElement(_ConfirmCancel2.default, 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 _react2.default.createElement(
_Modal2.default,
_extends({}, this.props, {
size: 'xsmall',
title: title,
callback: this.handleCallback,
style: style,
onClose: function onClose() {
return _this2.props.closeModal();
},
ModalActions: Buttons }),
message,
_react2.default.createElement(
'form',
{ onSubmit: this.handleSubmit },
_react2.default.createElement(InputModel, { onChange: this.handleChange, value: this.state.value })
)
);
};
return PromptInputModal;
}(_react.Component), _class.propTypes = {
/** What input component to use, defaults to */
InputModel: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func]),
/** What component to use for tray buttons, default being ConfirmCancel */
Buttons: _propTypes2.default.func,
/** Title string or react node */
title: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]),
/** Message string or react node */
message: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]),
/** Default value for input component -- node */
defaultValue: _propTypes2.default.node,
/** Callback function, will return input value or false if prompt is cancelled */
callback: _propTypes2.default.func
}, _class.defaultProps = {
InputModel: _reactForms.Input,
Buttons: DefaultButtons,
title: 'Input required',
style: 'primary',
message: null,
defaultValue: null,
callback: function callback() {
return console.warn('[PromptInputModal] no callback prop');
}
}, _temp);
exports.default = PromptInputModal;