UNPKG

infinity-forge

Version:
55 lines 2.99 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.Textarea = Textarea; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var formik_1 = require("formik"); var input_control_1 = require("../input-control/index.js"); var handler_1 = require("../handler/index.js"); function Textarea(props) { var _a, _b; var _c = (0, formik_1.useField)({ name: props.name }), field = _c[0], state = _c[1], helpers = _c[2]; var _d = (0, react_1.useState)(field.value), tempValue = _d[0], setTempValue = _d[1]; var formHandler = (0, handler_1.useFormHandler)(); (0, react_1.useEffect)(function () { if (field.value === undefined) { helpers.setValue(""); } }, [field.value]); (0, react_1.useEffect)(function () { if ((props === null || props === void 0 ? void 0 : props.onChangeMode) === 'blur') { setTempValue(field.value); } }, [field.value]); var value = (0, react_1.useMemo)(function () { return (props.onChangeMode === 'blur' ? tempValue : field.value); }, [field.value, tempValue]); var handleBlur = function () { var _a; if (tempValue !== field.value) { helpers.setValue(tempValue); (_a = props === null || props === void 0 ? void 0 : props.onChangeBlur) === null || _a === void 0 ? void 0 : _a.call(props, tempValue, undefined, { state: state, utils: helpers }); } }; return ((0, jsx_runtime_1.jsx)(input_control_1.InputControl, __assign({}, props, { fieldValue: value, children: (0, jsx_runtime_1.jsx)("textarea", __assign({ id: props.name, className: "font-16-regular" }, props, field, { readOnly: (props === null || props === void 0 ? void 0 : props.readOnly) || ((_b = (_a = formHandler === null || formHandler === void 0 ? void 0 : formHandler.formHandlerProps) === null || _a === void 0 ? void 0 : _a.inputs) === null || _b === void 0 ? void 0 : _b.readOnly), rows: props.rows || 5, maxLength: props.maxLength, value: value, onChange: function (ev) { var _a; var newValue = ev.target.value; if (props.onChangeMode === 'blur') { setTempValue(newValue); } else { field.onChange(ev); } (_a = props.onChangeInput) === null || _a === void 0 ? void 0 : _a.call(props, newValue, undefined, { state: state, utils: helpers }); }, onBlur: props.onChangeMode === 'blur' ? handleBlur : field.onBlur })) }))); } //# sourceMappingURL=index.js.map