hbp-quickfire
Version:
A library of useful user-interface components built with React on top of React Bootstrap and MobX
175 lines (118 loc) • 19.6 kB
JavaScript
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 Checkbox from "react-bootstrap/lib/Checkbox";import Radio from "react-bootstrap/lib/Radio";import Alert from "react-bootstrap/lib/Alert";import find from "lodash/find";import isFunction from "lodash/isFunction";
import FieldLabel from "./FieldLabel";
var styles = {
readMode: {
"& .quickfire-label:after": {
content: "':\\00a0'" },
"& .quickfire-readmode-item:not(:last-child):after": {
content: "',\\00a0'" } } };
/**
* Form component allowing to select on/multiple values from a group of checkboxes/radioboxes
* @class GroupSelectField
* @memberof FormFields
* @namespace GroupSelectField
*/var
GroupSelectField = (_dec = injectStyles(styles), _dec2 = inject("formStore"), _dec(_class = _dec2(_class = observer(_class = function (_React$Component) {_inherits(GroupSelectField, _React$Component);
function GroupSelectField(props) {_classCallCheck(this, GroupSelectField);var _this = _possibleConstructorReturn(this, (GroupSelectField.__proto__ || Object.getPrototypeOf(GroupSelectField)).call(this,
props));_this.
triggerOnLoad = function () {
if (_this.hiddenInputRef && _this.hiddenInputRef.parentNode) {
var event = new Event("load", { bubbles: true });
_this.hiddenInputRef.dispatchEvent(event);
}
};_this.
triggerOnChange = function () {
Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set.
call(_this.hiddenInputRef, JSON.stringify(_this.props.field.getValue(false)));
var event = new Event("input", { bubbles: true });
_this.hiddenInputRef.dispatchEvent(event);
};_this.initField();return _this;}_createClass(GroupSelectField, [{ 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;}() //The only way to trigger an onChange event in React is to do the following
//Basically changing the field value, bypassing the react setter and dispatching an "input"
// event on a proper html input node
//See for example the discussion here : https://stackoverflow.com/a/46012210/9429503
}, { key: "handleChange", value: function handleChange(option, e) {var field = this.props.field;e.stopPropagation();
if (field.disabled || field.readOnly) {
return;
}
//Case of radio group
if (field.max === 1) {
this.beforeSetValue(option);
} else if (find(field.value, option)) {
this.beforeRemoveValue(option);
} else {
this.beforeAddValue(option);
}
this.triggerOnChange();
} }, { key: "beforeAddValue", value: function beforeAddValue(
value) {var _this2 = this;
if (isFunction(this.props.onBeforeAddValue)) {
this.props.onBeforeAddValue(function () {_this2.props.field.addValue(value);}, this.props.field, value);
} else {
this.props.field.addValue(value);
}
} }, { key: "beforeRemoveValue", value: function beforeRemoveValue(
value) {var _this3 = this;
if (isFunction(this.props.onBeforeRemoveValue)) {
this.props.onBeforeRemoveValue(function () {_this3.props.field.removeValue(value);}, this.props.field, value);
} else {
this.props.field.removeValue(value);
}
} }, { key: "beforeSetValue", value: function beforeSetValue(
value) {var _this4 = this;
if (isFunction(this.props.onBeforeSetValue)) {
this.props.onBeforeSetValue(function () {_this4.props.field.setValue([value]);}, this.props.field, value);
} else {
this.props.field.setValue([value]);
}
} }, { key: "render", value: function render()
{var _this5 = this;
if (this.props.formStore.readMode || this.props.field.readMode) {
return this.renderReadMode();
}var _props$field =
this.props.field,options = _props$field.options,values = _props$field.value,max = _props$field.max,mappingLabel = _props$field.mappingLabel,disabled = _props$field.disabled,readOnly = _props$field.readOnly,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState;
var FieldComponent = max === 1 ? Radio : Checkbox;
return (
React.createElement(FormGroup, { className: "quickfire-field-group-select " + (!values.length ? "quickfire-empty-field" : "") + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : ""), validationState: validationState },
React.createElement(FieldLabel, { field: this.props.field }),
React.createElement("input", { style: { display: "none" }, type: "text", ref: function ref(_ref2) {return _this5.hiddenInputRef = _ref2;} }),
React.createElement("div", null,
options.map(function (option) {
var isChecked = !!find(values, option);
return (
React.createElement(FieldComponent, {
checked: isChecked,
disabled: disabled || max > 1 && !isChecked && values.length >= max,
readOnly: readOnly,
name: max === 1 ? _this5.props.field.path : undefined,
key: _this5.props.formStore.getGeneratedKey(option, "quickfire-groupselect-option"),
onChange: _this5.handleChange.bind(_this5, option),
inline: _this5.props.field.displayInline },
option[mappingLabel]));
})),
validationErrors && React.createElement(Alert, { bsStyle: "danger" },
validationErrors.map(function (error) {return React.createElement("p", { key: error }, error);}))));
} }, { key: "renderReadMode", value: function renderReadMode()
{var _this6 = this;var _props$field2 =
this.props.field,value = _props$field2.value,mappingLabel = _props$field2.mappingLabel,disabled = _props$field2.disabled,readOnly = _props$field2.readOnly;var
classes = this.props.classes;
return (
React.createElement("div", { className: "quickfire-field-group-select " + (!value.length ? "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-list" },
value.map(function (value) {
return (
React.createElement("span", { key: _this6.props.formStore.getGeneratedKey(value, "dropdown-read-item"), className: "quickfire-readmode-item" },
value[mappingLabel]));
})),
React.createElement("input", { style: { display: "none" }, type: "text", ref: function ref(_ref3) {return _this6.hiddenInputRef = _ref3;} })));
} }]);return GroupSelectField;}(React.Component)) || _class) || _class) || _class);export { GroupSelectField as default };