UNPKG

@gpa-gemstone/react-forms

Version:
101 lines (100 loc) 5.95 kB
"use strict"; //****************************************************************************************************** // MultiNodeForm.tsx - Gbtc // // Copyright © 2026, 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 may license 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: // ---------------------------------------------------------------------------------------------------- // 04/23/2026 - Preston Crawford // 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 }); var React = require("react"); var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols"); var HelpIcon_1 = require("./HelpIcon"); function MultiNodeForm(props) { var _a, _b, _c; var items = (_a = props.Record[props.Field]) !== null && _a !== void 0 ? _a : []; if (items.constructor !== Array) { console.warn("MultiNodeForm: ".concat(props.Field.toString(), " is not of type array.")); return React.createElement(React.Fragment, null); } // Variables to control the rendering of label and help icon on the empty state. var showLabel = props.Label !== ""; var label = props.Label === undefined ? props.Field : props.Label; var setItemAt = function (index, updated) { var _a; var newArray = __spreadArray([], items, true); newArray[index] = updated; props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newArray, _a))); }; var removeItemAt = function (index) { var _a; var newArray = items.filter(function (_, i) { return i !== index; }); props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newArray, _a))); }; var appendItem = function () { var _a; props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = __spreadArray(__spreadArray([], items, true), [props.DefaultValue], false), _a))); }; return (React.createElement(React.Fragment, null, showLabel ? React.createElement("label", { className: "d-flex align-items-center" }, React.createElement("span", null, label), React.createElement(HelpIcon_1.default, { Help: props.Help }), items.length === 0 ? React.createElement("button", { className: 'btn', style: ((_b = props.DisableAdd) !== null && _b !== void 0 ? _b : false) || ((_c = props.Disabled) !== null && _c !== void 0 ? _c : false) ? { display: 'none' } : undefined, onClick: function () { var _a; return props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = [props.DefaultValue], _a))); } }, React.createElement(gpa_symbols_1.ReactIcons.CirclePlus, null)) : null) : null, items.map(function (item, index) { var _a, _b, _c; return (React.createElement("div", { className: 'row no-gutters', key: index }, React.createElement("div", { className: 'col-10' }, props.Item(item, index, items, function (updated) { return setItemAt(index, updated); })), React.createElement("div", { className: "col-".concat(index === items.length - 1 ? 1 : 2, " ").concat(index === 0 ? 'd-flex align-items-center justify-content-center' : '') }, React.createElement("button", { className: 'btn', style: ((_a = props.Disabled) !== null && _a !== void 0 ? _a : false) ? { display: 'none' } : undefined, onClick: function () { return removeItemAt(index); } }, React.createElement(gpa_symbols_1.ReactIcons.TrashCan, { Color: 'red' }))), index === items.length - 1 ? React.createElement("div", { className: "col-1 ".concat(index === 0 ? 'd-flex align-items-center justify-content-center' : '') }, React.createElement("button", { className: 'btn', style: ((_b = props.DisableAdd) !== null && _b !== void 0 ? _b : false) || ((_c = props.Disabled) !== null && _c !== void 0 ? _c : false) ? { display: 'none' } : undefined, onClick: appendItem }, React.createElement(gpa_symbols_1.ReactIcons.CirclePlus, null))) : null)); }))); } exports.default = MultiNodeForm;