UNPKG

@gpa-gemstone/react-forms

Version:
120 lines (119 loc) 6.74 kB
"use strict"; // ****************************************************************************************************** // Input.tsx - Gbtc // // Copyright © 2020, 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: // ---------------------------------------------------------------------------------------------------- // 01/22/2020 - Billy Ernest // Generated original version of source code. // 05/05/2021 - C. Lackner // Added Help Message. // // ****************************************************************************************************** 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 }); exports.default = Input; 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 Input(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 // Effect to generate a unique ID for the component. React.useEffect(function () { setGuid((0, helper_functions_1.CreateGuid)()); }, []); // Handle changes to the record's field value. React.useEffect(function () { var rawValue = props.Record[props.Field] == null ? '' : props.Record[props.Field].toString(); if ((props.Type === 'number' || props.Type === 'integer') && !internal.current) setHeldVal(rawValue); else if (rawValue !== heldVal) setHeldVal(rawValue); internal.current = false; }, [props.Record[props.Field]]); // Handle blur event (loss of focus) on the input. 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))); } } // Handle value change of the input. function valueChange(value) { var _a, _b, _c; var _d; 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 { if (props.Type === 'text' && ((_d = props.AllowNull) !== null && _d !== void 0 ? _d : false)) console.warn("Input component: Empty strings are set to null for Type='text' and AllowNull=true to maintain current functionality."); props.Setter(__assign(__assign({}, props.Record), (_c = {}, _c[props.Field] = value !== '' ? value : null, _c))); setHeldVal(value); } } // Variables to control the rendering of label and help icon. 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.Style }, 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, showHelpIcon ? React.createElement(ToolTip_1.default, { Show: showHelp, Target: guid, Class: "info", Position: "top" }, props.Help) : null, 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.Disabled == null ? false : props.Disabled, onBlur: onBlur, step: 'any' }), React.createElement("div", { className: "invalid-feedback" }, props.Feedback == null ? props.Field.toString() + ' is a required field.' : props.Feedback))); }