@ducor/form
Version:
form package
21 lines (20 loc) • 1.9 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Controller, useFormContext } from "react-hook-form";
import { twMerge } from "tailwind-merge";
import { uuidv4 } from "./utils";
import Label from "./components/label";
import Errors from "./components/errors";
import HelperText from "./components/helperText";
export const Number = ({ name, label, withAsterisk, helperText, id, placeholder, direction = "vertical", defaultValue, disabled, minLength, maxLength, className, }) => {
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 space-y-2 ', children: [_jsxs("div", { className: 'relative', children: [_jsx(Controller, { name: name, control: control, defaultValue: defaultValue, rules: {
required: withAsterisk ? "This field is required" : false,
}, render: ({ field }) => (_jsx("input", Object.assign({ type: 'number' }, field, { min: minLength, max: maxLength, placeholder: placeholder, 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] })] })] }));
};