UNPKG

@bigfishtv/cockpit

Version:

121 lines (105 loc) 4.68 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, _temp2; 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 ModelModal from '../modal/ModelModal'; import Cell from '../cell/Cell'; import Icon from '../Icon'; import Button from '../button/Button'; import { modalHandler } from '../modal/ModalHost'; // we define this because react-docgen fails when defaultProp directly references an imported component var DefaultCell = function DefaultCell(props) { return React.createElement(Cell, props); }; /** * Input component for selecting a model entity using ModelModal */ var ModelSelectModal = (_temp2 = _class = function (_Component) { _inherits(ModelSelectModal, _Component); function ModelSelectModal() { var _temp, _this, _ret; _classCallCheck(this, ModelSelectModal); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.handleBrowse = function () { var _this$props = _this.props, queryUrl = _this$props.queryUrl, model = _this$props.model, fields = _this$props.fields, buttonText = _this$props.buttonText, modalProps = _this$props.modalProps; var url = queryUrl ? queryUrl : model ? '/admin/' + model + '.json' : null; modalHandler.add({ Component: ModelModal, props: _extends({}, modalProps, { title: buttonText, fields: fields, url: url, onSave: function onSave(value) { return _this.props.onChange(value); }, onClose: function onClose() {} }) }); }, _temp), _possibleConstructorReturn(_this, _ret); } ModelSelectModal.prototype.render = function render() { var _props = this.props, Cell = _props.Cell, buttonText = _props.buttonText, onChange = _props.onChange, onEdit = _props.onEdit, value = _props.value; return value ? React.createElement(Cell, { Icon: React.createElement(Icon, { name: 'link' }), title: value.title || value.name, onEdit: onEdit, CellControl: function CellControl(props) { return React.createElement( Button, { size: 'icon', onClick: function onClick() { return onChange(null); } }, React.createElement(Icon, { name: 'close', size: '18' }) ); } }) : React.createElement(Button, { text: buttonText, size: 'medium', onClick: this.handleBrowse }); }; return ModelSelectModal; }(Component), _class.propTypes = { /** Cell component to display selection */ Cell: PropTypes.func, /** Model string which will auto construct url i.e. /admin/[model]/index.json */ model: PropTypes.string, /** Explicit query url instead of providing a model */ queryUrl: PropTypes.string, /** Fields to display in table */ fields: PropTypes.arrayOf(PropTypes.object), /** Props to pass on to ModelModal */ modalPropts: PropTypes.object, /** Button text */ buttonText: PropTypes.string, value: PropTypes.node, onChange: PropTypes.func, /** Called by Cell component */ onEdit: PropTypes.func }, _class.defaultProps = { Cell: DefaultCell, model: null, queryUrl: null, fields: [], modalProps: {}, buttonText: 'Select', value: null, onChange: function onChange() { return console.warn('[ModelSelectModal] No onChange prop provided'); }, onEdit: function onEdit() { return console.warn('[ModelSelectModal] No onEdit prop provided'); } }, _temp2); export { ModelSelectModal as default };