UNPKG

@gpa-gemstone/react-forms

Version:
72 lines (71 loc) 4.02 kB
"use strict"; 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 = TextArea; // ****************************************************************************************************** // TextArea.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. // 11/03/2023 - C Lackner // Added internal state to avoid cursor jumping. // // ****************************************************************************************************** var React = require("react"); var HelpIcon_1 = require("./HelpIcon"); function TextArea(props) { var _a; var _b = React.useState(''), heldVal = _b[0], setHeldVal = _b[1]; var _c = React.useState(false), isFocused = _c[0], setIsFocused = _c[1]; //Effect to update heldVal when the record value changes, but only if the textarea is not currently focused to avoid cursor jumping. React.useEffect(function () { if (!isFocused) { setHeldVal(props.Record[props.Field] == null ? '' : props.Record[props.Field].toString()); } }, [props.Record[props.Field]]); // Handle value change of the textarea. function valueChange(value) { var _a; props.Setter(__assign(__assign({}, props.Record), (_a = {}, _a[props.Field] = value !== '' ? value : null, _a))); setHeldVal(value); } // 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("div", { className: "form-group" }, showLabel ? React.createElement("label", { className: "d-flex align-items-center" }, React.createElement("span", null, showLabel ? label : ''), React.createElement(HelpIcon_1.default, { Help: props.Help })) : null, React.createElement("textarea", { ref: props.TextAreaRef, rows: props.Rows, className: props.Valid(props.Field) ? 'form-control' : 'form-control is-invalid', onChange: function (evt) { return valueChange(evt.target.value); }, value: heldVal == null ? '' : heldVal, disabled: props.Disabled == null ? false : props.Disabled, spellCheck: (_a = props.SpellCheck) !== null && _a !== void 0 ? _a : true, onFocus: function () { return setIsFocused(true); }, onBlur: function () { setIsFocused(false); setHeldVal(props.Record[props.Field] == null ? '' : props.Record[props.Field].toString()); } }), React.createElement("div", { className: "invalid-feedback" }, props.Feedback == null ? props.Field + ' is a required field.' : props.Feedback))); }