import {ReactNode, useEffect, useRef} from 'react'; import {useFormContext} from 'react-hook-form'; import clsx from 'clsx'; interface Props { children: (isInvalid: boolean) => ReactNode; name: string; separatorBottom?: boolean; separatorTop?: boolean; } export function SettingsErrorGroup({ children, name, separatorBottom = true, separatorTop = true, }: Props) { const { formState: {errors}, } = useFormContext>(); const ref = useRef(null); const error = errors[name]; useEffect(() => { if (error) { ref.current?.scrollIntoView({behavior: 'smooth'}); } }, [error]); return (
{children(!!error)} {error && (
)}
); }