UNPKG

@gpa-gemstone/react-forms

Version:
66 lines (65 loc) 3.9 kB
"use strict"; // ****************************************************************************************************** // EnumCheckBoxes.tsx - Gbtc // // Copyright © 2020, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 01/22/2020 - Billy Ernest // Generated original version of source code. // // ****************************************************************************************************** var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = EnumCheckBoxes; var React = require("react"); /** * EnumCheckBoxes Component. * Renders a set of checkboxes based on an enumeration, allowing multiple selection. */ function EnumCheckBoxes(props) { // Determine if an enum flag is set. /* tslint:disable-next-line:no-bitwise */ var EquateFlag = function (index) { return ((props.Record[props.Field] / Math.pow(2, index)) & 1) !== 0; }; // Turn off a flag in the enumeration. var DecrementFlag = function (index) { return props.Record[props.Field] - Math.pow(2, index); }; // Turn on a flag in the enumeration. var IncrementFlag = function (index) { return props.Record[props.Field] + Math.pow(2, index); }; return (React.createElement("div", { className: "form-group" }, React.createElement("label", null, props.Label == null ? props.Field : props.Label), React.createElement("br", null), React.createElement("div", { className: "form-check form-check-inline" }, React.createElement("input", { className: "form-check-input", type: "checkbox", checked: props.Record[props.Field] === (Math.pow(2, props.Enum.length) - 1), onChange: function (evt) { var _a; return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = evt.target.checked ? Math.pow(2, props.Enum.length) - 1 : 0, _a))); } }), React.createElement("label", { className: "form-check-label" }, "All")), props.Enum.map(function (flag, i) { return (React.createElement("div", { key: i, className: "form-check form-check-inline" }, React.createElement("input", { className: "form-check-input", type: "checkbox", checked: EquateFlag(i), disabled: props.IsDisabled !== undefined ? props.IsDisabled(flag) : false, onChange: function (evt) { var _a; return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = evt.target.checked ? IncrementFlag(i) : DecrementFlag(i), _a))); } }), React.createElement("label", { className: "form-check-label" }, flag))); }))); }