UNPKG

@ducor/form

Version:
25 lines (24 loc) 2.08 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useFormContext, Controller } from "react-hook-form"; import { twMerge } from "tailwind-merge"; import { uuidv4 } from "./utils"; import Errors from "./components/errors"; import Label from "./components/label"; import HelperText from "./components/helperText"; export const Email = ({ label, withAsterisk, direction = "vertical", name, helperText, id, placeholder, defaultValue = "", className, disabled, }) => { const { control, formState: { errors }, } = useFormContext(); const controlId = id || uuidv4(); return (_jsxs("div", { className: twMerge("flex w-full", direction === "horizontal" ? "flex-row items-center gap-4" : "flex-col gap-2"), children: [label && (_jsx(Label, { id: controlId, label: label, withAsterisk: withAsterisk })), _jsxs("div", { className: 'w-full', children: [_jsx("div", { className: 'relative', children: _jsx(Controller, { name: name, control: control, defaultValue: defaultValue, rules: { required: withAsterisk ? "This field is required" : false, pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: "Invalid email address", }, }, render: ({ field }) => (_jsx("input", Object.assign({ id: controlId, type: 'email', placeholder: placeholder, disabled: disabled }, field, { className: twMerge("w-full p-2 rounded-lg outline-none text-gray-700 dark:text-gray-200 border ", disabled ? "bg-gray-100 cursor-not-allowed border-gray-300" : errors[name] ? "border-red-500 focus:border-red-500" : "border-gray-300 focus:border-black", className) }))) }) }), helperText && _jsx(HelperText, { helperText: helperText }), errors[name] && _jsx(Errors, { value: errors[name] })] })] })); };