UNPKG

@gpa-gemstone/react-forms

Version:
113 lines (112 loc) 6.55 kB
"use strict"; // ****************************************************************************************************** // InputWithButton.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 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: // ---------------------------------------------------------------------------------------------------- // 05/02/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); }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var ToolTip_1 = require("./ToolTip"); var helper_functions_1 = require("@gpa-gemstone/helper-functions"); var gpa_symbols_1 = require("@gpa-gemstone/gpa-symbols"); function InputWithButton(props) { var internal = React.useRef(false); var _a = React.useState(""), guid = _a[0], setGuid = _a[1]; var _b = React.useState(false), showHelp = _b[0], setShowHelp = _b[1]; var _c = React.useState(''), heldVal = _c[0], setHeldVal = _c[1]; // Need to buffer tha value because parseFloat will throw away trailing decimals or zeros React.useEffect(function () { setGuid((0, helper_functions_1.CreateGuid)()); }, []); React.useEffect(function () { if (!internal.current) { setHeldVal(props.Record[props.Field] == null ? '' : props.Record[props.Field].toString()); } internal.current = false; }, [props.Record[props.Field]]); function onBlur() { var _a; var _b; var allowNull = props.AllowNull === undefined ? false : props.AllowNull; if (!allowNull && (props.Type === 'number' || props.Type === 'integer') && heldVal === '') { internal.current = false; props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = (_b = props.DefaultValue) !== null && _b !== void 0 ? _b : 0, _a))); } } function valueChange(value) { var _a, _b, _c; internal.current = true; var allowNull = props.AllowNull === undefined ? false : props.AllowNull; if (props.Type === 'number') { var v = (value.length > 0 && value[0] === '.' ? ("0" + value) : value); if ((0, helper_functions_1.IsNumber)(v) || (v === '' && allowNull)) { props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = v !== '' ? parseFloat(v) : null, _a))); setHeldVal(v); } else if (v === '') { setHeldVal(v); } } else if (props.Type === 'integer') { if ((0, helper_functions_1.IsInteger)(value) || (value === '' && allowNull)) { props.Setter(__assign(__assign({}, props.Record), (_b = {}, _b[props.Field] = value !== '' ? parseFloat(value) : null, _b))); setHeldVal(value); } else if (value === '') { setHeldVal(value); } } else { props.Setter(__assign(__assign({}, props.Record), (_c = {}, _c[props.Field] = value !== '' ? value : null, _c))); setHeldVal(value); } } var showLabel = props.Label !== ""; var showHelpIcon = props.Help !== undefined; var label = props.Label === undefined ? props.Field : props.Label; return (React.createElement("div", { className: "form-group " + (props.Size === 'large' ? 'form-group-lg' : '') + (props.Size === 'small' ? 'form-group-sm' : ''), style: props.InputStyle }, showHelpIcon || showLabel ? React.createElement("label", { className: 'd-flex align-items-center' }, React.createElement("span", null, showLabel ? label : ''), showHelpIcon ? 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) : null, showHelpIcon ? React.createElement(ToolTip_1.default, { Show: showHelp, Target: guid, Class: "info", Position: "top" }, props.Help) : null, React.createElement("div", { className: "input-group" }, React.createElement("input", { type: props.Type === undefined ? 'text' : props.Type, className: props.Valid(props.Field) ? 'form-control' : 'form-control is-invalid', onChange: function (evt) { return valueChange(evt.target.value); }, value: heldVal, disabled: props.InputDisabled == null ? false : props.InputDisabled, onBlur: onBlur, step: 'any' }), React.createElement("div", { className: "input-group-prepend" }, React.createElement("button", { className: props.BtnClass != null ? props.BtnClass : "btn btn-outline-secondary", style: props.BtnStyle, disabled: props.BtnDisabled == null ? false : props.BtnDisabled, type: "button", onClick: function (evt) { return props.OnBtnClick(evt); } }, props.BtnLabel)), React.createElement("div", { className: "invalid-feedback" }, props.Feedback == null ? props.Field.toString() + ' is a required field.' : props.Feedback)))); } exports.default = InputWithButton;