@gpa-gemstone/react-forms
Version:
React Form modules for gpa webapps
106 lines (105 loc) • 7.22 kB
JavaScript
;
//******************************************************************************************************
// MultiInput.tsx - Gbtc
//
// Copyright © 2024, 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:
// ----------------------------------------------------------------------------------------------------
// 12/05/2024 - 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 Input_1 = require("./Input");
var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
var HelpIcon_1 = require("./HelpIcon");
//Only supporting string/number arrays for now
function MultiInput(props) {
var _a, _b;
var fieldArray = props.Record[props.Field];
if ((fieldArray === null || fieldArray === void 0 ? void 0 : fieldArray.constructor) !== Array) {
console.warn("MultiInput: ".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.
var showLabel = props.Label !== "";
var label = props.Label === undefined ? props.Field : props.Label;
return (React.createElement(React.Fragment, null,
fieldArray.length === 0 ?
React.createElement(React.Fragment, null, showLabel ?
React.createElement("label", { className: "d-flex align-items-center" },
React.createElement("span", null, showLabel ? label : ''),
React.createElement(HelpIcon_1.default, { Help: props.Help }),
React.createElement("button", { className: 'btn', style: ((_a = props.DisableAdd) !== null && _a !== void 0 ? _a : false) || ((_b = props.Disabled) !== null && _b !== void 0 ? _b : 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,
fieldArray.map(function (r, index) {
var _a, _b, _c, _d, _e;
return (React.createElement("div", { className: 'row no-gutters', key: index },
React.createElement("div", { className: 'col-10' },
React.createElement(Input_1.default, { Record: fieldArray, Field: index, Label: index === 0 ? props.Label : '', AllowNull: props.AllowNull, Type: props.Type, Help: index === 0 ? props.Help : undefined, Feedback: (_b = (_a = props.ItemFeedback) === null || _a === void 0 ? void 0 : _a.call(props, r, index, fieldArray)) !== null && _b !== void 0 ? _b : undefined, Valid: function () { var _a, _b; return (_b = (_a = props.ItemValid) === null || _a === void 0 ? void 0 : _a.call(props, r, index, fieldArray)) !== null && _b !== void 0 ? _b : true; }, Style: props.Style, Disabled: props.Disabled, Setter: function (record) {
var _a;
var _b;
var newArray = __spreadArray([], fieldArray, true);
if (!((_b = props.AllowNull) !== null && _b !== void 0 ? _b : true) && record[index] === null)
newArray[index] = props.DefaultValue;
else
newArray[index] = record[index];
props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newArray, _a)));
} })),
React.createElement("div", { className: "col-".concat(index === __spreadArray([], fieldArray, true).length - 1 ? 1 : 2, " ").concat(index === 0 ? 'd-flex align-items-center justify-content-center' : '') },
React.createElement("button", { className: 'btn', style: ((_c = props.Disabled) !== null && _c !== void 0 ? _c : false) ? { display: 'none' } : undefined, onClick: function () {
var _a;
var newRecords = __spreadArray([], fieldArray, true).filter(function (_, i) { return i !== index; });
props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newRecords, _a)));
} },
React.createElement(gpa_symbols_1.ReactIcons.TrashCan, { Color: 'red' }))),
index === __spreadArray([], fieldArray, true).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: ((_d = props.DisableAdd) !== null && _d !== void 0 ? _d : false) || ((_e = props.Disabled) !== null && _e !== void 0 ? _e : false) ? { display: 'none' } : undefined, onClick: function () {
var _a;
var newRecords = __spreadArray(__spreadArray([], __spreadArray([], fieldArray, true), true), [props.DefaultValue], false);
props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newRecords, _a)));
} },
React.createElement(gpa_symbols_1.ReactIcons.CirclePlus, null)))
: null));
})));
}
exports.default = MultiInput;