UNPKG

@ducor/form

Version:
17 lines (16 loc) 1.55 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { Controller, useFormContext } from "react-hook-form"; import { uuidv4 } from "./utils"; import { twMerge } from "tailwind-merge"; import Label from "./components/label"; import Errors from "./components/errors"; import HelperText from "./components/helperText"; export const Rate = ({ name, label, withAsterisk, helperText, defaultValue, id, direction = "vertical", }) => { const { control, formState: { errors }, } = useFormContext(); const controlId = id || uuidv4(); return (_jsxs("div", { className: twMerge("flex", 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: 'flex flex-col gap-2', children: [_jsx("div", { className: 'relative', children: _jsx(Controller, { name: name, control: control, defaultValue: defaultValue, rules: { required: withAsterisk ? "This field is required" : false, }, render: ({ field }) => (_jsx("div", { className: 'flex gap-1', children: [1, 2, 3, 4, 5].map((star) => (_jsx("span", { role: 'rating', onClick: () => field.onChange(star), className: `cursor-pointer ${field.value >= star ? "text-yellow-500" : "text-gray-400"}`, children: "\u2605" }, star))) })) }) }), helperText && _jsx(HelperText, { helperText: helperText }), errors[name] && _jsx(Errors, { value: errors[name] })] })] })); };