UNPKG

hbp-quickfire

Version:

A library of useful user-interface components built with React on top of React Bootstrap and MobX

156 lines (104 loc) 18.4 kB
var _createClass = 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);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();var _dec, _dec2, _class;function _asyncToGenerator(fn) {return function () {var gen = fn.apply(this, arguments);return new Promise(function (resolve, reject) {function step(key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {return Promise.resolve(value).then(function (value) {step("next", value);}, function (err) {step("throw", err);});}}return step("next");});};}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;} /* * Copyright (c) Human Brain Project * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import React from "react"; import { inject, observer } from "mobx-react"; import injectStyles from "react-jss";import FormGroup from "react-bootstrap/lib/FormGroup";import FormControl from "react-bootstrap/lib/FormControl";import Alert from "react-bootstrap/lib/Alert";import isString from "lodash/isString";import isNumber from "lodash/isNumber";import isFunction from "lodash/isFunction"; import FieldLabel from "./FieldLabel"; var style = { readMode: { "& .quickfire-label:after": { content: "':\\00a0'" } } }; /** * A simple select input field * @class SelectField * @memberof FormFields * @namespace SelectField */var SelectField = (_dec = injectStyles(style), _dec2 = inject("formStore"), _dec(_class = _dec2(_class = observer(_class = function (_React$Component) {_inherits(SelectField, _React$Component); function SelectField(props) {_classCallCheck(this, SelectField);var _this = _possibleConstructorReturn(this, (SelectField.__proto__ || Object.getPrototypeOf(SelectField)).call(this, props));_this. triggerOnLoad = function () { if (_this.inputRef && _this.inputRef.parentNode) { var event = new Event("load", { bubbles: true }); _this.inputRef.dispatchEvent(event); } };_this. handleChange = function (e) { var field = _this.props.field; if (!field.disabled && !field.readOnly) { if (!e.target.value) { _this.beforeSetValue(null); } else { _this.beforeSetValue(field.optionsMap.get(e.target.value)); } } };_this.initField();return _this;}_createClass(SelectField, [{ key: "initField", value: function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {var _props, field, formStore, optionsUrl, cacheOptionsUrl;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:_props = this.props, field = _props.field, formStore = _props.formStore;optionsUrl = field.optionsUrl, cacheOptionsUrl = field.cacheOptionsUrl;if (!optionsUrl) {_context.next = 9;break;}_context.t0 = field;_context.next = 6;return formStore.resolveURL(optionsUrl, cacheOptionsUrl);case 6:_context.t1 = _context.sent;_context.t0.updateOptions.call(_context.t0, _context.t1);this.triggerOnLoad();case 9:case "end":return _context.stop();}}}, _callee, this);}));function initField() {return _ref.apply(this, arguments);}return initField;}() }, { key: "beforeSetValue", value: function beforeSetValue( value) {var _this2 = this; if (isFunction(this.props.onBeforeSetValue)) { this.props.onBeforeSetValue(function () {_this2.props.field.setValue(value);}, this.props.field, value); } else { this.props.field.setValue(value); } } }, { key: "renderOptions", value: function renderOptions( field) {var _this3 = this;var options = field.options,mappingLabel = field.mappingLabel,defaultLabel = field.defaultLabel,optionsMap = field.optionsMap; var renderedOptions = options.map(function (option) { var label = void 0; if (isString(option) || isNumber(option)) { label = option; } else { label = option[mappingLabel]; if (label == null) { throw "Option mapping not defined correctly for field: " + _this3.props.name + "\n Mapping is defined as mappingLabel: " + mappingLabel + "\n option defined as: " + JSON.stringify(option); } } return React.createElement("option", { key: optionsMap.get(option), value: optionsMap.get(option) }, label); }); if (defaultLabel) { renderedOptions.unshift( React.createElement("option", { key: "SelectField_defaultValueKey", value: "" }, defaultLabel)); } return renderedOptions; } }, { key: "render", value: function render() {var _this4 = this; if (this.props.formStore.readMode || this.props.field.readMode) { return this.renderReadMode(); }var _props$field = this.props.field,disabled = _props$field.disabled,readOnly = _props$field.readOnly,value = _props$field.value,optionsMap = _props$field.optionsMap,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState; //We need to read that value to update the component on value changes //TODO: Maybe find a proper solution for that to be more explicit value; return ( React.createElement(FormGroup, { className: "quickfire-field-select " + (!value ? "quickfire-empty-field" : "") + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : ""), validationState: validationState }, React.createElement(FieldLabel, { field: this.props.field }), React.createElement(FormControl, { disabled: disabled, readOnly: readOnly, onChange: this.handleChange, inputRef: function inputRef(ref) {return _this4.inputRef = ref;}, value: value === null ? "" : optionsMap.get(value), componentClass: "select" }, this.renderOptions(this.props.field)), React.createElement(FormControl.Feedback, null), validationErrors && React.createElement(Alert, { bsStyle: "danger" }, validationErrors.map(function (error) {return React.createElement("p", { key: error }, error);})))); } }, { key: "renderReadMode", value: function renderReadMode() {var _this5 = this;var _props$field2 = this.props.field,valueLabel = _props$field2.value,mappingLabel = _props$field2.mappingLabel,disabled = _props$field2.disabled,readOnly = _props$field2.readOnly;var classes = this.props.classes; if (valueLabel && !isString(valueLabel) && !isNumber(valueLabel)) { valueLabel = valueLabel[mappingLabel]; } return ( React.createElement("div", { className: "quickfire-field-select " + (!valueLabel ? "quickfire-empty-field" : "") + " quickfire-readmode " + classes.readMode + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : "") }, React.createElement(FieldLabel, { field: this.props.field }), isFunction(this.props.readModeRendering) ? this.props.readModeRendering(this.props.field) : React.createElement("span", { className: "quickfire-readmode-value" }, valueLabel), React.createElement("input", { style: { display: "none" }, type: "text", ref: function ref(_ref2) {return _this5.inputRef = _ref2;} }))); } }]);return SelectField;}(React.Component)) || _class) || _class) || _class);export { SelectField as default };