import * as React from 'react' import { Box, Text } from '@mantine/core' interface FormFieldProps { label?: string helperText?: string error?: string children: React.ReactNode className?: string } function FormField({ label, helperText, error, children, className }: FormFieldProps) { const id = React.useId() const inputId = `${id}-input` const helperId = `${id}-helper` const errorId = `${id}-error` const describedBy = [helperText ? helperId : null, error ? errorId : null].filter(Boolean).join(' ') || undefined const childWithProps = React.Children.map(children, (child) => { if (React.isValidElement(child)) { return React.cloneElement(child as React.ReactElement>, { id: inputId, 'aria-invalid': error ? true : undefined, 'aria-describedby': describedBy, error: error || undefined, }) } return child }) return ( {label && ( {label} )} {childWithProps} {helperText && !error && ( {helperText} )} {error && ( {error} )} ) } export { FormField } export type { FormFieldProps }