hbp-quickfire
Version:
A library of useful user-interface components built with React on top of React Bootstrap and MobX
87 lines (58 loc) • 12.8 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 Checkbox from "react-bootstrap/lib/Checkbox";import Alert from "react-bootstrap/lib/Alert";import isFunction from "lodash/isFunction";
import FieldLabel from "./FieldLabel";
var styles = {
readMode: {
"& .quickfire-label:after": {
content: "':\\00a0'" } } };
/**
* A simple checkbox
* @class CheckBoxField
* @memberof FormFields
* @namespace CheckBoxField
*/var
CheckBoxField = (_dec = injectStyles(styles), _dec2 = inject("formStore"), _dec(_class = _dec2(_class = observer(_class = function (_React$Component) {_inherits(CheckBoxField, _React$Component);function CheckBoxField() {var _ref;var _temp, _this, _ret;_classCallCheck(this, CheckBoxField);for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = CheckBoxField.__proto__ || Object.getPrototypeOf(CheckBoxField)).call.apply(_ref, [this].concat(args))), _this), _this.
handleChange = function () {
var field = _this.props.field;
if (!field.disabled && !field.readOnly) {
_this.beforeSetValue(!field.value);
}
}, _temp), _possibleConstructorReturn(_this, _ret);}_createClass(CheckBoxField, [{ 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()
{
if (this.props.formStore.readMode || this.props.field.readMode) {
return this.renderReadMode();
}var _props$field =
this.props.field,value = _props$field.value,disabled = _props$field.disabled,readOnly = _props$field.readOnly,validationErrors = _props$field.validationErrors,validationState = _props$field.validationState;
return (
React.createElement(FormGroup, {
className: "quickfire-field-checkbox " + (disabled ? "quickfire-field-disabled" : "") + " " + (readOnly ? "quickfire-field-readonly" : ""),
validationState: validationState },
React.createElement(FieldLabel, { field: this.props.field }),
React.createElement(Checkbox, { disabled: disabled, readOnly: readOnly, onChange: this.handleChange, checked: value }),
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,readOnly = _props$field2.readOnly,disabled = _props$field2.disabled;var
classes = this.props.classes;
return (
React.createElement("div", { className: "quickfire-field-checkbox 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", null, "\xA0", React.createElement("input", { className: "quickfire-readmode-checkbox", type: "checkbox", readOnly: true, checked: value }))));
} }]);return CheckBoxField;}(React.Component)) || _class) || _class) || _class);export { CheckBoxField as default };