UNPKG

hbp-quickfire

Version:

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

321 lines (252 loc) 22.3 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 _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 FormGroup from "react-bootstrap/lib/FormGroup";import Button from "react-bootstrap/lib/Button";import Glyphicon from "react-bootstrap/lib/Glyphicon";import Alert from "react-bootstrap/lib/Alert"; import injectStyles from "react-jss";import isFunction from "lodash/isFunction"; import clipboard from "../Stores/ClipboardStore"; import FieldLabel from "./FieldLabel"; var styles = { values: { height: "auto", paddingBottom: "3px", "& .btn.value-tag": { marginRight: "3px", marginBottom: "3px" }, "&.withVirtualClipboard": { paddingRight: "36px" }, "& :disabled": { pointerEvents: "none" }, "& [readonly] .quickfire-remove": { pointerEvents: "none" }, "&[readonly] .quickfire-user-input, &[disabled] .quickfire-user-input": { display: "none" } }, valueDisplay: { display: "inline-block", maxWidth: "200px", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", verticalAlign: "bottom" }, remove: { fontSize: "0.8em", opacity: 0.5, marginLeft: "3px", "&:hover": { opacity: 1 } }, pasteButton: { position: "absolute", top: 0, right: 0, height: "100%", margin: "0", borderTopLeftRadius: 0, borderBottomLeftRadius: 0, borderRight: "none", borderTop: "none", borderBottom: "none" }, userInput: { background: "transparent", border: "none", color: "currentColor", outline: "none", width: "200px", maxWidth: "33%", marginBottom: "3px", "&:disabled": { cursor: "not-allowed" } }, readMode: { "& .quickfire-label:after": { content: "':\\00a0'" }, "& .quickfire-readmode-item:not(:last-child):after": { content: "',\\00a0'" } } }; /** * Allows the input of multiple values * @class InputTextMultipleField * @memberof FormFields * @namespace InputTextMultipleField */var InputTextMultipleField = (_dec = inject("formStore"), _dec2 = injectStyles(styles), _dec(_class = _dec2(_class = observer(_class = function (_React$Component) {_inherits(InputTextMultipleField, _React$Component);function InputTextMultipleField() {var _ref;var _temp, _this, _ret;_classCallCheck(this, InputTextMultipleField);for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = InputTextMultipleField.__proto__ || Object.getPrototypeOf(InputTextMultipleField)).call.apply(_ref, [this].concat(args))), _this), _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. handleChange = function (e) { e.stopPropagation(); }, _this. handlePaste = function () { ("" + clipboard.selection).split("\n").forEach(function (value) { _this.beforeAddValue(value.trim()); }); _this.triggerOnChange(); }, _this. handleNativePaste = function (e) { e.preventDefault(); e.clipboardData.getData("text").split("\n").forEach(function (value) { _this.beforeAddValue(value.trim()); }); _this.triggerOnChange(); }, _this. handleKeyStrokes = function (e) { var field = _this.props.field; if (field.disabled || field.readOnly) { return; } if (field.value.length < field.max && e.keyCode === 13) { //User pressed "Enter" while focus on input and we have not reached the maximum number of values _this.beforeAddValue(e.target.value.trim()); e.target.value = ""; _this.triggerOnChange(); } else if (!e.target.value && field.value.length > 0 && e.keyCode === 8) { // User pressed "Backspace" while focus on input, and input is empty, and values have been entered e.preventDefault(); var valueToRemove = e.target.value = field.value[field.value.length - 1]; _this.beforeRemoveValue(valueToRemove); } }, _this. handleBlur = function (e) {var field = _this.props.field;var value = e.target.value; if (!field.disabled && !field.readOnly && value) { _this.beforeAddValue(e.target.value.trim()); e.target.value = ""; _this.triggerOnChange(); } }, _this. handleFocus = function () { _this.inputRef.focus(); }, _temp), _possibleConstructorReturn(_this, _ret);} //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 _createClass(InputTextMultipleField, [{ key: "handleRemove", value: function handleRemove(value, e) {var field = this.props.field;e.stopPropagation(); if (field.disabled || field.readOnly) { return; } this.beforeRemoveValue(value); this.triggerOnChange(); } }, { key: "handleRemoveBackspace", value: function handleRemoveBackspace( value, e) { var field = this.props.field; if (field.disabled || field.readOnly) { return; } //User pressed "Backspace" while focus on a value if (e.keyCode === 8) { this.beforeRemoveValue(value); this.handleFocus(); this.triggerOnChange(); } } }, { key: "handleValueTagInteraction", value: function handleValueTagInteraction( interaction, value, event) { if (isFunction(this.props["onValue" + interaction])) { this.props["onValue" + interaction](this.props.field, value, event); } else if (!this.props.field.disabled && !this.props.field.readOnly && !this.props.field.readAndDeleteOnly && interaction === "Click") { this.handleRemove(value, event); this.inputRef.value = value; this.handleFocus(); this.triggerOnChange(); } } }, { key: "handleDrop", value: function handleDrop( droppedVal, e) { var field = this.props.field; if (field.disabled || field.readOnly || field.readAndDeleteOnly) { return; } e.preventDefault(); field.removeValue(this.draggedValue); field.addValue(this.draggedValue, field.value.indexOf(droppedVal)); this.handleFocus(); 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: "render", value: function render() {var _this4 = this; if (this.props.formStore.readMode || this.props.field.readMode) { return this.renderReadMode(); }var classes = this.props.classes;var _props$field = this.props.field,value = _props$field.value,disabled = _props$field.disabled,readOnly = _props$field.readOnly,readAndDeleteOnly = _props$field.readAndDeleteOnly,useVirtualClipboard = _props$field.useVirtualClipboard,max = _props$field.max,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState; var withVirtualClipboardClass = useVirtualClipboard ? "withVirtualClipboard" : ""; return ( React.createElement(FormGroup, { onClick: this.handleFocus, className: "quickfire-field-input-text-multiple " + (!value.length ? "quickfire-empty-field" : "") + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : ""), validationState: validationState }, React.createElement(FieldLabel, { field: this.props.field }), React.createElement("div", { disabled: disabled, readOnly: readOnly, className: "form-control input-group " + classes.values + " " + withVirtualClipboardClass }, value.map(function (val) { return ( React.createElement("div", { key: val, tabIndex: "0", disabled: disabled, readOnly: readOnly, readAndDeleteOnly: readAndDeleteOnly, title: val, className: "value-tag quickfire-value-tag btn btn-xs btn-default " + (disabled || readOnly ? "disabled" : ""), draggable: true, onDragEnd: function onDragEnd() {return _this4.draggedValue = null;}, onDragStart: function onDragStart() {return _this4.draggedValue = val;}, onDragOver: function onDragOver(e) {return e.preventDefault();}, onDrop: _this4.handleDrop.bind(_this4, val), onKeyDown: _this4.handleRemoveBackspace.bind(_this4, val), onClick: _this4.handleValueTagInteraction.bind(_this4, "Click", val), onFocus: _this4.handleValueTagInteraction.bind(_this4, "Focus", val), onBlur: _this4.handleValueTagInteraction.bind(_this4, "Blur", val), onMouseOver: _this4.handleValueTagInteraction.bind(_this4, "MouseOver", val), onMouseOut: _this4.handleValueTagInteraction.bind(_this4, "MouseOut", val), onMouseEnter: _this4.handleValueTagInteraction.bind(_this4, "MouseEnter", val), onMouseLeave: _this4.handleValueTagInteraction.bind(_this4, "MouseLeave", val) }, React.createElement("span", { className: classes.valueDisplay }, isFunction(_this4.props.valueLabelRendering) ? _this4.props.valueLabelRendering(_this4.props.field, val) : val), React.createElement(Glyphicon, { className: [classes.remove, "quickfire-remove"], glyph: "remove", onClick: _this4.handleRemove.bind(_this4, val) }))); }), React.createElement("input", { ref: function ref(_ref2) {return _this4.inputRef = _ref2;}, type: "text", className: "quickfire-user-input " + classes.userInput, disabled: readOnly || readAndDeleteOnly || disabled || value.length >= max, onDrop: this.handleDrop.bind(this, null), onDragOver: function onDragOver(e) {return e.preventDefault();}, onKeyDown: this.handleKeyStrokes, onBlur: this.handleBlur, onChange: this.handleChange, onPaste: this.handleNativePaste }), React.createElement("input", { style: { display: "none" }, type: "text", ref: function ref(_ref3) {return _this4.hiddenInputRef = _ref3;} }), useVirtualClipboard ? React.createElement(Button, { className: "quickfire-paste-button " + classes.pasteButton, onClick: this.handlePaste }, React.createElement(Glyphicon, { glyph: "paste" })) : 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,value = _props$field2.value,disabled = _props$field2.disabled,readOnly = _props$field2.readOnly;var classes = this.props.classes; return ( React.createElement("div", { className: "quickfire-field-input-text-multiple " + (!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: value, className: "quickfire-readmode-item" }, isFunction(_this5.props.valueLabelRendering) ? _this5.props.valueLabelRendering(_this5.props.field, value) : value)); })), React.createElement("input", { style: { display: "none" }, type: "text", ref: function ref(_ref4) {return _this5.hiddenInputRef = _ref4;} }))); } }]);return InputTextMultipleField;}(React.Component)) || _class) || _class) || _class);export { InputTextMultipleField as default };