UNPKG

@gpa-gemstone/react-forms

Version:
82 lines (81 loc) 4.57 kB
"use strict"; // ****************************************************************************************************** // ArrayCheckBoxes.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); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ArrayCheckBoxes; var React = require("react"); var helper_functions_1 = require("@gpa-gemstone/helper-functions"); function ArrayCheckBoxes(props) { // Remove an ID from the array var Remove = function (cb) { var a = __spreadArray([], props.Record[props.Field], true); var i = a.indexOf(cb.ID); a.splice(i, 1); return a; }; // Add an ID to the array making sure there are no duplicates and array is sorted var Add = function (cb) { var a = __spreadArray([], props.Record[props.Field], true); var i = a.indexOf(cb.ID); if (i < 0) a.push(cb.ID); a.sort(); return a; }; var id = React.useRef("array-checkbox-" + (0, helper_functions_1.CreateGuid)()); 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", { id: "".concat(id.current, "-all"), className: "form-check-input", type: "checkbox", checked: JSON.stringify(props.Record[props.Field]) === JSON.stringify(props.Checkboxes.map(function (x) { return x.ID; })), onChange: function (evt) { var _a; return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = evt.target.checked ? props.Checkboxes.map(function (x) { return x.ID; }) : [], _a))); } }), React.createElement("label", { htmlFor: "".concat(id.current, "-all"), className: "form-check-label" }, "All")), props.Checkboxes.map(function (cb, i) { return (React.createElement("div", { key: i, className: "form-check form-check-inline" }, React.createElement("input", { id: "".concat(id.current, "-").concat(i), className: "form-check-input", type: "checkbox", checked: props.Record[props.Field].find(function (x) { return cb.ID === x; }) !== undefined, onChange: function (evt) { var _a; return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = evt.target.checked ? Add(cb) : Remove(cb), _a))); } }), React.createElement("label", { htmlFor: "".concat(id.current, "-").concat(i), className: "form-check-label" }, cb.Label))); }))); }