UNPKG

hbp-quickfire

Version:

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

118 lines (88 loc) 30.8 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 _get = function get(object, property, receiver) {if (object === null) object = Function.prototype;var desc = Object.getOwnPropertyDescriptor(object, property);if (desc === undefined) {var parent = Object.getPrototypeOf(object);if (parent === null) {return undefined;} else {return get(parent, property, receiver);}} else if ("value" in desc) {return desc.value;} else {var getter = desc.get;if (getter === undefined) {return undefined;}return getter.call(receiver);}};var _desc, _value, _class, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5, _descriptor6, _descriptor7, _descriptor8, _descriptor9, _descriptor10, _descriptor11, _descriptor12, _descriptor13, _descriptor14;function _initDefineProp(target, property, descriptor, context) {if (!descriptor) return;Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 });}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;}function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {var desc = {};Object['ke' + 'ys'](descriptor).forEach(function (key) {desc[key] = descriptor[key];});desc.enumerable = !!desc.enumerable;desc.configurable = !!desc.configurable;if ('value' in desc || desc.initializer) {desc.writable = true;}desc = decorators.slice().reverse().reduce(function (desc, decorator) {return decorator(target, property, desc) || desc;}, desc);if (context && desc.initializer !== void 0) {desc.value = desc.initializer ? desc.initializer.call(context) : void 0;desc.initializer = undefined;}if (desc.initializer === void 0) {Object['define' + 'Property'](target, property, desc);desc = null;}return desc;}function _initializerWarningHelper(descriptor, context) {throw new Error('Decorating class property failed. Please ensure that transform-class-properties is enabled.');} /* * 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 { observable, action } from "mobx";import isString from "lodash/isString";import isNumber from "lodash/isNumber";import find from "lodash/find";import union from "lodash/union"; import DefaultField from "./DefaultField"; import optionsStore from "../OptionsStore"; /** * @memberof FormFields.DropdownSelectField * @name Options * @param {string} label "" - The field label * @param {string} labelTooltip "" - The field label tooltip message * @param {string} labelTooltipPlacement "top" - The field label tooltip message position * @param {string} type "DropdownSelect" * @param {array} value [] - The current value of the field * @param {array} defaultValue [] - The defaultValue of the field * @param {array} options [] - The options of the dropdown, must be an array of objects * @param {string} optionsUrl null - url to fetch the select options from * @param {string} cacheOptionsUrl false - whether to cache optionsUrl fetching response * @param {string} path "" - Field path * @param {boolean} allowCustomValues false - if the field should try to accept user inputed values * @param {string} customValueLabel "Add a value:" - Label that gets displayed when entering a new custom value. allowCustomValues need to be true for this to show up. * @param {string | array} mappingValue "value" - The name(s) of the option object field(s) related to the option value, used to match passed in values to actual options * @param {string} mappingLabel "label" - the name of the option object field related to the option label * @param {string | array} mappingReturn null - the property/properties of the option object used to return the value(s) - null will return the whole object * @param {boolean} returnSingle boolean - wether or not to return the first value or an array of values * @param {number} max Infinity - Maximum values that the field can have * @param {boolean} emptyToNull false - Flag that determines if empty values are transformed to null in the value function of the formStore * @param {string} listPosition "bottom" - Can be "top" or "bottom", whether to display the dropdown list above or below the field * @param {boolean} closeDropdownAfterInteraction false - Whether the dropdown should close after adding, removing a value or stay open * @param {boolean} disabled false - Is the field disabled or not, a disabled field won't be editable or processed by FormStore.getValues() * @param {boolean} readOnly false - Is the field readOnly or not, a readOnly field won't be editable but will be processed by FormStore.getValues() * @param {boolean} readMode false - If true, displays the field as label and value without the actual form input */var DropdownSelectField = (_class = function (_DefaultField) {_inherits(DropdownSelectField, _DefaultField);_createClass(DropdownSelectField, null, [{ key: "properties", get: function get() { return union(_get(DropdownSelectField.__proto__ || Object.getPrototypeOf(DropdownSelectField), "properties", this), ["value", "defaultValue", "options", "optionsUrl", "cacheOptionsUrl", "allowCustomValues", "customValueLabel", "mappingValue", "mappingLabel", "mappingReturn", "returnSingle", "max", "listPosition", "closeDropdownAfterInteraction"]); } }]); function DropdownSelectField(fieldData, store, path) {_classCallCheck(this, DropdownSelectField); //Try to checked if cached options already exist var _this = _possibleConstructorReturn(this, (DropdownSelectField.__proto__ || Object.getPrototypeOf(DropdownSelectField)).call(this, fieldData, store, path));_initDefineProp(_this, "value", _descriptor, _this);_initDefineProp(_this, "defaultValue", _descriptor2, _this);_initDefineProp(_this, "options", _descriptor3, _this);_initDefineProp(_this, "optionsUrl", _descriptor4, _this);_initDefineProp(_this, "cacheOptionsUrl", _descriptor5, _this);_initDefineProp(_this, "allowCustomValues", _descriptor6, _this);_initDefineProp(_this, "customValueLabel", _descriptor7, _this);_initDefineProp(_this, "mappingValue", _descriptor8, _this);_initDefineProp(_this, "mappingLabel", _descriptor9, _this);_initDefineProp(_this, "mappingReturn", _descriptor10, _this);_initDefineProp(_this, "returnSingle", _descriptor11, _this);_initDefineProp(_this, "max", _descriptor12, _this);_initDefineProp(_this, "listPosition", _descriptor13, _this);_initDefineProp(_this, "closeDropdownAfterInteraction", _descriptor14, _this);_this.__emptyValue = function () {return [];};if (_this.cacheOptionsUrl && _this.optionsUrl) { var options = optionsStore.getOptions(_this.optionsUrl); if (options !== undefined) { _this.optionsUrl = null; _this.options = options; } } _this.injectValue(_this.value);return _this; }_createClass(DropdownSelectField, [{ key: "injectValue", value: function injectValue( value) {var _this2 = this; if (value !== undefined) { this.registerProvidedValue(value, true); } this.value = this.__emptyValue(); var providedValue = this.getProvidedValue(); providedValue.forEach(function (value) { if (!value || _this2.value.length >= _this2.max) { return; } var match = void 0; //Below are the tests to find matches in the options array for each provided value //If the provided value is scalar then we check against the mappingValue property(ies) of each //option (and stop at the first match). Each mappingValue property has to match the scalar value (edge case) //If the provided value is an object then we check against the mappingValue property(ies) of each //option (and stop at the first match). Each mappingValue property has to match its respective counterpart in the option object if (isString(value) || isNumber(value)) { match = find(_this2.options, function (option) {return ( isString(_this2.mappingValue) || isNumber(_this2.mappingValue) ? option[_this2.mappingValue] === value : _this2.mappingValue.every(function (prop) {return option[prop] === value;}));}); } else if (value != null) { match = find(_this2.options, function (option) {return ( isString(_this2.mappingValue) || isNumber(_this2.mappingValue) ? option[_this2.mappingValue] === value[_this2.mappingValue] : _this2.mappingValue.every(function (prop) {return option[prop] && value[prop] && option[prop] === value[prop];}));}); } if (match) { _this2.addValue(match); } }); } }, { key: "updateOptions", value: function updateOptions( options) { this.options = options; this.injectValue(); } }]);return DropdownSelectField;}(DefaultField), (_descriptor = _applyDecoratedDescriptor(_class.prototype, "value", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor2 = _applyDecoratedDescriptor(_class.prototype, "defaultValue", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor3 = _applyDecoratedDescriptor(_class.prototype, "options", [observable], { enumerable: true, initializer: function initializer() {return [];} }), _descriptor4 = _applyDecoratedDescriptor(_class.prototype, "optionsUrl", [observable], { enumerable: true, initializer: function initializer() {return null;} }), _descriptor5 = _applyDecoratedDescriptor(_class.prototype, "cacheOptionsUrl", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor6 = _applyDecoratedDescriptor(_class.prototype, "allowCustomValues", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor7 = _applyDecoratedDescriptor(_class.prototype, "customValueLabel", [observable], { enumerable: true, initializer: function initializer() {return "Add a value:";} }), _descriptor8 = _applyDecoratedDescriptor(_class.prototype, "mappingValue", [observable], { enumerable: true, initializer: function initializer() {return "value";} }), _descriptor9 = _applyDecoratedDescriptor(_class.prototype, "mappingLabel", [observable], { enumerable: true, initializer: function initializer() {return "label";} }), _descriptor10 = _applyDecoratedDescriptor(_class.prototype, "mappingReturn", [observable], { enumerable: true, initializer: function initializer() {return null;} }), _descriptor11 = _applyDecoratedDescriptor(_class.prototype, "returnSingle", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _descriptor12 = _applyDecoratedDescriptor(_class.prototype, "max", [observable], { enumerable: true, initializer: function initializer() {return Infinity;} }), _descriptor13 = _applyDecoratedDescriptor(_class.prototype, "listPosition", [observable], { enumerable: true, initializer: function initializer() {return "bottom";} }), _descriptor14 = _applyDecoratedDescriptor(_class.prototype, "closeDropdownAfterInteraction", [observable], { enumerable: true, initializer: function initializer() {return false;} }), _applyDecoratedDescriptor(_class.prototype, "injectValue", [action], Object.getOwnPropertyDescriptor(_class.prototype, "injectValue"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "updateOptions", [action], Object.getOwnPropertyDescriptor(_class.prototype, "updateOptions"), _class.prototype)), _class);export { DropdownSelectField as default };