"use client"; import { flip, offset, shift, useFloating } from "@floating-ui/react"; import { clsx } from "clsx"; import { PropsWithChildren } from "react"; import { FieldPath, FieldValues, useFormContext } from "react-hook-form"; import { ErrorIcon } from "../../icons/ErrorIcon.js"; type Props< TValues extends FieldValues, TName extends FieldPath = FieldPath, > = { name: TName; }; export function WithRHFError< TValues extends FieldValues, TName extends FieldPath = FieldPath, >({ name, children }: PropsWithChildren>) { const { getFieldState, formState, clearErrors } = useFormContext(); const { error } = getFieldState(name, formState); const isErrorOpen = Boolean(error); const { refs, floatingStyles, placement } = useFloating({ placement: "bottom-start", // TODO - make configurable open: isErrorOpen, middleware: [shift(), offset(2), flip()], }); return (
{children}
{isErrorOpen && (
clearErrors(name)} >
{error?.message || error?.type || String(error)}
)}
); }