hbp-quickfire
Version:
A library of useful user-interface components built with React on top of React Bootstrap and MobX
175 lines (128 loc) • 14.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 _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 Alert from "react-bootstrap/lib/Alert";import isFunction from "lodash/isFunction";import uniqueId from "lodash/uniqueId";
import FieldLabel from "./FieldLabel";
var styles = {
readMode: {
"& .quickfire-label:after": {
content: "':\\00a0'" } },
formGroup: {
"&.inline": {
display: "flex",
alignItems: "center",
"& .quickfire-label": {
marginBottom: "0px" } } },
react_switch_checkbox: {
height: 0,
width: 0,
visibility: "hidden",
"&:checked + label span": {
left: "calc(100% - 2px)",
transform: "translateX(-100%)" } },
react_switch_label: {
display: "flex",
alignItems: "center",
justifyContent: "space-between",
cursor: "pointer",
position: "relative",
transition: "background-color .2s",
marginBottom: "0px",
"&.disabled": {
cursor: "not-allowed" },
"&.readonly": {
cursor: "default" },
"&.inline": {
marginLeft: "10px" },
"&.large": {
width: "70px",
height: "34px",
borderRadius: "70px" },
"&.medium": {
width: "50px",
height: "24px",
borderRadius: "50px" },
"&.small": {
width: "30px",
height: "15px",
borderRadius: "30px" } },
react_switch_button: {
content: "",
position: "absolute",
top: "2px",
left: "3px",
borderRadius: "100%",
background: "#fff",
transition: "0.2s",
boxShadow: "0 0 2px 0 rgba(10, 10, 10, 0.29)",
"&.large": {
width: "30px",
height: "30px" },
"&.medium": {
width: "20px",
height: "20px" },
"&.small": {
width: "11px",
height: "11px",
left: "2px" } },
switch_label_txt: {
marginRight: "7px",
cursor: "pointer" } };
/**
* A simple toggle
* @class ToggleField
* @memberof FormFields
* @namespace ToggleField
*/var
ToggleField = (_dec = injectStyles(styles), _dec2 = inject("formStore"), _dec(_class = _dec2(_class = observer(_class = function (_React$Component) {_inherits(ToggleField, _React$Component);
function ToggleField(props) {_classCallCheck(this, ToggleField);var _this = _possibleConstructorReturn(this, (ToggleField.__proto__ || Object.getPrototypeOf(ToggleField)).call(this,
props));_this.
handleChange = function () {
var field = _this.props.field;
if (!field.disabled && !field.readOnly && !(_this.props.formStore.readMode || _this.props.field.readMode)) {
_this.beforeSetValue(!field.value);
}
};_this.state = { htmlId: null };return _this;}_createClass(ToggleField, [{ key: "componentDidMount", value: function componentDidMount() {// generate random id
this.setState({ htmlId: uniqueId('toggle-') });} }, { 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
classes = this.props.classes;var _props$field =
this.props.field,value = _props$field.value,disabled = _props$field.disabled,readOnly = _props$field.readOnly,_props$field$onColor = _props$field.onColor,onColor = _props$field$onColor === undefined ? "#06D6A0" : _props$field$onColor,_props$field$offColor = _props$field.offColor,offColor = _props$field$offColor === undefined ? "#808080" : _props$field$offColor,_props$field$size = _props$field.size,size = _props$field$size === undefined ? "medium" : _props$field$size,_props$field$inline = _props$field.inline,inline = _props$field$inline === undefined ? false : _props$field$inline,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState;
// force readOnly mode for readMode
if (this.props.formStore.readMode || this.props.field.readMode) {
readOnly = true;
}
var bgColor = value ? onColor : offColor;
return (
React.createElement(FormGroup, {
className: classes.formGroup + " " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : "") + " " + (inline ? "inline" : ""),
validationState: validationState },
React.createElement(FieldLabel, { field: this.props.field }),
React.createElement("input", {
disabled: disabled,
readOnly: readOnly,
onChange: this.handleChange,
checked: value,
className: classes.react_switch_checkbox,
id: this.state.htmlId,
type: "checkbox" }),
React.createElement("label", {
style: { background: bgColor },
className: classes.react_switch_label + " " + size + " " + (disabled ? "disabled" : "") + " " + (readOnly ? "readonly" : "") + " " + (inline ? "inline" : ""),
htmlFor: this.state.htmlId },
React.createElement("span", { className: classes.react_switch_button + " " + size })),
validationErrors &&
React.createElement(Alert, { bsStyle: "danger" },
validationErrors.map(function (error) {return React.createElement("p", { key: error }, error);}))));
} }]);return ToggleField;}(React.Component)) || _class) || _class) || _class);export { ToggleField as default };