UNPKG

styled-hook-form

Version:

React form library for styled-components based on grommet and react-hook-form

41 lines (40 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TextEditor = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const react_hook_form_1 = require("react-hook-form"); const grommet_1 = require("grommet"); const context_1 = require("../../../../context"); const TextEditor = react_1.forwardRef((props, ref) => { let vrules = props.validationRules || {}; const { translate: T } = context_1.useFormBuilderContext(); let { name, label, defaultValue: initialValue, shouldUnregister, minLength, maxLength, required, methods, inputProps } = props; let control = methods === null || methods === void 0 ? void 0 : methods.control; if (required) { vrules.required = { value: required, message: T("required-msg", { name: label }), }; } if (minLength) { vrules.minLength = { value: minLength, message: T("text-input-min-length-msg", { name: label, value: minLength, }), }; } if (maxLength) { vrules.maxLength = { value: maxLength, message: T("text-input-max-length-msg", { name: label, value: maxLength }), }; } return (jsx_runtime_1.jsx(react_hook_form_1.Controller, { name: name, defaultValue: initialValue, shouldUnregister: shouldUnregister, rules: vrules, control: control, render: ({ field }) => { var _a; return (jsx_runtime_1.jsx(grommet_1.TextInput, Object.assign({}, inputProps, { ref: ref, onChange: (e) => field.onChange(e), value: (_a = field.value) !== null && _a !== void 0 ? _a : "" }), void 0)); } }, void 0)); }); exports.TextEditor = TextEditor;