UNPKG

@gpa-gemstone/react-forms

Version:
107 lines (106 loc) 7.02 kB
"use strict"; //****************************************************************************************************** // MultiSearchableSelect.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: // ---------------------------------------------------------------------------------------------------- // 07/01/2025 - 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 SearchableSelect_1 = require("./SearchableSelect"); 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 MultiSearchableSelect(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: "bottom" }, props.Help)) : null, fieldArray.map(function (r, index) { var _a, _b; return (React.createElement("div", { className: 'row align-items-center', key: index }, React.createElement("div", { className: 'col' }, React.createElement(SearchableSelect_1.default, { Record: fieldArray, Field: index, Label: index === 0 ? props.Label : '', Help: index === 0 ? props.Help : undefined, Feedback: props.Feedback, Valid: function () { return props.Valid != null ? props.Valid(props.Field) : true; }, Style: props.Style, Disabled: props.Disabled, Setter: function (record) { var _a; var newArray = __spreadArray([], fieldArray, true); newArray[index] = record[index]; props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = newArray, _a))); }, Search: props.Search, BtnStyle: props.BtnStyle, GetLabel: props.GetLabel, ResetSearchOnSelect: props.ResetSearchOnSelect, AllowCustom: props.AllowCustom })), React.createElement("div", { className: 'col-auto' }, React.createElement("button", { className: 'btn', style: ((_a = props.Disabled) !== null && _a !== void 0 ? _a : 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-auto' }, React.createElement("button", { className: 'btn', style: ((_b = props.Disabled) !== null && _b !== void 0 ? _b : 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 = MultiSearchableSelect;