import { cloneElement, useId, type ReactElement, type ReactNode } from "react"; // подпись над контролом; связь через htmlFor/id, подсказка и ошибка — через aria-describedby (имя поля — только подпись) export function Field({ label, hint, error, children }: { label: string; hint?: ReactNode; error?: ReactNode; children: ReactElement> }) { const id = useId(); const described = [hint && `${id}-hint`, error && `${id}-err`].filter(Boolean).join(" ") || undefined; return (
{cloneElement(children, { id, "aria-describedby": described, ...(error ? { "aria-invalid": "true" } : {}) })} {hint && {hint}} {error && {error}}
); }