"use client"; import * as React from "react"; import { cn } from "./lib/utils"; export type FieldErrorProps = React.HTMLAttributes & { children?: React.ReactNode; }; export type FieldGroupProps = React.HTMLAttributes & { children?: React.ReactNode; }; export type FieldSetProps = React.FieldsetHTMLAttributes & { legend?: React.ReactNode; children?: React.ReactNode; }; export type FieldLabelProps = React.LabelHTMLAttributes; export type FieldControlProps = React.HTMLAttributes; export type FieldDescriptionProps = React.HTMLAttributes & { children?: React.ReactNode; }; export type FieldLegendProps = React.HTMLAttributes & { children?: React.ReactNode; }; export type FieldProps = Omit, "title"> & { fieldClassName?: string; label?: React.ReactNode; htmlFor?: string; description?: React.ReactNode; error?: React.ReactNode; required?: boolean; children?: React.ReactNode; }; const Field = React.forwardRef( ({ fieldClassName, label, htmlFor, description, error, required = false, children, className, ...props }, ref) => { const uid = React.useId(); const controlId = htmlFor ?? `${uid}-control`; const descId = description !== undefined ? `${uid}-desc` : undefined; const errorId = error ? `${uid}-err` : undefined; const describedBy = [descId, errorId].filter(Boolean).join(" ") || undefined; let enhancedChildren = children; if (React.isValidElement(children)) { const childProps = children.props as Record; const existingDescribedBy = typeof childProps["aria-describedby"] === "string" ? childProps["aria-describedby"] : undefined; const mergedDescribedBy = [existingDescribedBy, describedBy].filter(Boolean).join(" ") || undefined; enhancedChildren = React.cloneElement(children as React.ReactElement>, { ...(childProps.id === undefined && htmlFor ? { id: controlId } : {}), ...(mergedDescribedBy ? { "aria-describedby": mergedDescribedBy } : {}), ...(error ? { "aria-invalid": true } : {}), }); } return (
{label !== undefined && ( {label} {required && ( )} )}
{enhancedChildren} {error ? ( {error} ) : null}
{description !== undefined && ( {description} )}
); } ); Field.displayName = "Field"; export function FieldLabel({ className, ...props }: FieldLabelProps) { return