@gpa-gemstone/react-forms
Version:
React Form modules for gpa webapps
109 lines (108 loc) • 7.59 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 ToolTip_1 = require("./ToolTip");
var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols");
var helper_functions_1 = require("@gpa-gemstone/helper-functions");
//Only supporting string/number arrays for now
function MultiInput(props) {
var _a;
var guid = React.useState((0, helper_functions_1.CreateGuid)())[0];
var _b = React.useState(false), showHelp = _b[0], setShowHelp = _b[1];
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);
}
return (React.createElement(React.Fragment, null,
fieldArray.length === 0 ?
React.createElement(React.Fragment, null,
React.createElement("label", { className: 'd-flex align-items-center' },
React.createElement("span", null, (_a = props.Label) !== null && _a !== void 0 ? _a : props.Field),
props.Help != null ?
React.createElement("span", { className: "ml-2 d-flex align-items-center", onMouseEnter: function () { return setShowHelp(true); }, onMouseLeave: function () { return setShowHelp(false); }, "data-tooltip": guid },
React.createElement(gpa_symbols_1.ReactIcons.QuestionMark, { Color: "var(--info)", Size: 20 }))
: null,
React.createElement("button", { className: 'btn', 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))),
React.createElement(ToolTip_1.default, { Show: showHelp && props.Help != null, Target: guid, Class: "info", Position: "top" }, props.Help))
: null,
fieldArray.map(function (r, index) {
var _a, _b, _c, _d, _e, _f;
return (React.createElement("div", { className: 'row', 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' : '') },
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' : '') },
React.createElement("button", { className: 'btn', style: ((_f = (((_d = props.DisableAdd) !== null && _d !== void 0 ? _d : false) || ((_e = props.Disabled) !== null && _e !== void 0 ? _e : false))) !== null && _f !== void 0 ? _f : 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;
;