import * as React from "react"
import { FormControlProvider, type FormControlState } from "../lib/form-control"
import { cn } from "../lib/utils"
import { Label } from "../ui/label"
/** Подзаголовок группы полей формы. */
export function FormSection({ title, children }: { title: string; children: React.ReactNode }) {
return (
)
}
/**
* Подпись, поле, подсказка или ошибка под ним.
* Сам выдаёт id поля и id текста под ним: Input, DateInput, TimeInput, SnilsInput, PhoneInput
* и SearchInput подхватывают их и ставят aria-invalid / aria-describedby.
* htmlFor — когда контрол сам не читает контекст (список, своя кнопка): подпись укажет на его id.
*/
export function FormField({
label,
htmlFor,
hint,
error,
className,
children,
}: {
label: string
htmlFor?: string
hint?: string
error?: string
className?: string
children: React.ReactNode
}) {
const generatedId = React.useId()
const descriptionId = React.useId()
const id = htmlFor ?? generatedId
const message = error || hint
const control = React.useMemo(
() => ({ id, descriptionId: message ? descriptionId : undefined, invalid: Boolean(error) }),
[id, descriptionId, message, error],
)
return (
{children}
{error ? (
{error}
) : hint ? (
{hint}
) : null}
)
}