import { cloneElement } from "react"; import { VuiLabel } from "../form/label/Label"; import { VuiSpacer } from "../spacer/Spacer"; import { VuiText } from "../typography/Text"; import { VuiTextColor } from "../typography/TextColor"; import { createId } from "../../utils/createId"; type Props = { labelFor: string; label: string; children: React.ReactElement; helpText?: React.ReactNode; errors?: string[]; isRequired?: boolean; }; export const VuiFormGroup = ({ children, labelFor, helpText, label, errors, isRequired }: Props) => { const ariaProps: Record = { "aria-describedby": "" }; const ariaDescribedByLabel = `help-${createId()}`; const errorMessageIds: string[] = []; const errorMessages = errors?.map((error, index) => { const id = `error-${createId()}`; errorMessageIds.push(id); return ( <> {index > 0 && }

{error}

); }); if (helpText) { ariaProps["aria-describedby"] += ariaDescribedByLabel; } if (errorMessages?.length) { ariaProps["aria-describedby"] += " " + errorMessageIds.join(" "); } const content = cloneElement(children, { ...ariaProps, isInvalid: errors && errors.length > 0, id: labelFor, required: isRequired }); return ( <> {label} {isRequired && " (required)"} {content} {(helpText || errorMessages) && } {helpText && (

{helpText}

)} {errorMessages} ); };