hbp-quickfire
Version:
A library of useful user-interface components built with React on top of React Bootstrap and MobX
197 lines (102 loc) • 18.3 kB
JavaScript
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 _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, _class2, _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;} /*
* 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 InputGroup from "react-bootstrap/lib/InputGroup";import Button from "react-bootstrap/lib/Button";import Glyphicon from "react-bootstrap/lib/Glyphicon";import Alert from "react-bootstrap/lib/Alert";
import autosize from "autosize";
import getLineHeight from "line-height";import isFunction from "lodash/isFunction";import isString from "lodash/isString";
import FieldLabel from "./FieldLabel";
import clipboard from "../Stores/ClipboardStore";
var styles = {
readMode: {
"& .quickfire-label:after": {
content: "':\\00a0'" } } };
/**
* A simple text input
* @class InputTextField
* @memberof FormFields
* @namespace InputTextField
*/var
InputTextField = (_dec = injectStyles(styles), _dec2 = inject("formStore"), _dec(_class = _dec2(_class = observer(_class = (_temp2 = _class2 = function (_React$Component) {_inherits(InputTextField, _React$Component);function InputTextField() {var _ref;var _temp, _this, _ret;_classCallCheck(this, InputTextField);for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = InputTextField.__proto__ || Object.getPrototypeOf(InputTextField)).call.apply(_ref, [this].concat(args))), _this), _this.
handleChange = function (e) {
var field = _this.props.field;
//This shouldn't be necessary although some inputType don't behave well with readOnly => see inputType color
if (!field.disabled && !field.readOnly) {
_this.beforeSetValue(e.target.value);
}
}, _this.
triggerOnChange = function () {
Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set.
call(_this.inputRef, _this.props.field.value);
var event = new Event("input", { bubbles: true });
_this.inputRef.dispatchEvent(event);
}, _this.
handlePaste = function () {
_this.beforeSetValue(clipboard.selection);
_this.triggerOnChange();
}, _this.
getStyle = function () {var _this$props$style =
_this.props.style,style = _this$props$style === undefined ? {} : _this$props$style;var _this$props$field =
_this.props.field,maxRows = _this$props$field.maxRows,resizable = _this$props$field.resizable;
var maxHeight = maxRows && _this.lineHeight ? _this.lineHeight * (maxRows + 1) : null;
var resize = resizable ? null : "none";
style = _extends({},
style,
maxHeight ? { maxHeight: maxHeight } : {},
resize ? { resize: resize } : {});
return style;
}, _temp), _possibleConstructorReturn(_this, _ret);}_createClass(InputTextField, [{ key: "handleAutosize", value: function handleAutosize() {if (!this.props.field.autosize) {return;}if (this.inputRef) {if (this.inputRef != this.autosizedRef) {// the input ref may change over time. For instance when readmode is toggled. Therefore we might have to autosize the input multiple times over the component lifecycle.
autosize(this.inputRef);this.autosizedRef = this.inputRef;this.lineHeight = getLineHeight(this.inputRef);} else {autosize.update(this.inputRef);}}} }, { key: "componentDidMount", value: function componentDidMount() {this.handleAutosize();} }, { key: "componentDidUpdate", value: function componentDidUpdate() {this.handleAutosize();} //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: "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: "render", value: function render() {var _this3 = this;if (this.props.formStore.readMode || this.props.field.readMode) {return this.renderReadMode();}var _props$field =
this.props.field,value = _props$field.value,inputType = _props$field.inputType,autoComplete = _props$field.autoComplete,useVirtualClipboard = _props$field.useVirtualClipboard,disabled = _props$field.disabled,readOnly = _props$field.readOnly,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState,placeholder = _props$field.placeholder,rows = _props$field.rows;
var style = this.getStyle();
var formControl = function formControl() {return (
React.createElement(FormControl, {
value: value,
type: inputType,
className: "quickfire-user-input",
componentClass: _this3.props.componentClass,
onChange: _this3.handleChange,
inputRef: function inputRef(ref) {return _this3.inputRef = ref;},
disabled: disabled,
readOnly: readOnly,
placeholder: placeholder,
style: style,
rows: rows,
autoComplete: autoComplete ? "on" : "off" }));};
return (
React.createElement(FormGroup, { className: "quickfire-field-input-text " + (!value ? "quickfire-empty-field" : "") + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : ""), validationState: validationState },
React.createElement(FieldLabel, { field: this.props.field }),
useVirtualClipboard ?
React.createElement(InputGroup, null,
formControl(),
React.createElement(InputGroup.Button, null,
React.createElement(Button, { className: "quickfire-paste-button", onClick: this.handlePaste },
React.createElement(Glyphicon, { glyph: "paste" })))) :
formControl(),
validationErrors && React.createElement(Alert, { bsStyle: "danger" },
validationErrors.map(function (error) {return React.createElement("p", { key: error }, error);}))));
} }, { key: "renderReadMode", value: function renderReadMode()
{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 " + (!value ? "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) :
this.props.componentClass === "textarea" ?
React.createElement("div", { className: "quickfire-readmode-value quickfire-readmode-textarea-value" },
isString(value) && value.split("\n").map(function (line, index) {
return (
React.createElement("p", { key: line + ("" + index) }, line));
})) :
React.createElement("span", { className: "quickfire-readmode-value" }, "\xA0", value)));
} }]);return InputTextField;}(React.Component), _class2.defaultProps = { componentClass: undefined }, _temp2)) || _class) || _class) || _class);export { InputTextField as default };