"use client"; import * as React from "react"; import { CircleAlertIcon, InfoIcon } from "lucide-react"; import { Label } from "../ui/label"; import { SegmentedControl } from "../ui/segmented-control"; import { cn } from "../lib/cn"; /** The native form controls a field's ARIA state means something on. */ const FORM_CONTROLS = new Set(["input", "textarea", "select"]); /** * A host element that is not a form control: a `div` or a `span` put around * the field to position something beside it. */ function isPlainWrapper(element: React.ReactElement): boolean { return typeof element.type === "string" && !FORM_CONTROLS.has(element.type); } /** * One field of the statement form: its label, the control, then what is wrong * with it and what it is for. The error is said in words beside a red icon, * in the text's own colour: red alone would not say it, and would not hold on * every ground. */ export function StatementField( props: React.PropsWithChildren<{ id: string; label: string; hint?: string; /** * What the field's current value means for the published document, said * under the hint and read with it. Informative, never an error: it does * not mark the control invalid. */ note?: string; required?: boolean; /** Marks the control as wrong (`aria-invalid`) and shows the reason. */ invalid?: boolean; /** Why it is wrong; shown when `invalid` is true. */ errorMessage?: string; className?: string; }>, ) { const { id, label, hint, note, required, invalid, errorMessage, className, children, } = props; const errorId = invalid && errorMessage ? `${id}-error` : undefined; const hintId = hint ? `${id}-hint` : undefined; const noteId = note ? `${id}-note` : undefined; // Forward `aria-required`, `aria-invalid`, and `aria-describedby` to the // single child input so screen readers actually announce required + error // state. The asterisk in the label is `aria-hidden` because it conveys // the same info redundantly for sighted users. // cloneElement works for direct Input/Textarea children. It does NOT // reach the trigger of Radix `` // wrapped in a positioning div. Those fields wire aria-* manually on // their SelectTrigger / Input. // // A plain wrapper element is left alone (ACC-516): on a positioning `div` // `aria-required` is not allowed on an element with no role (axe // `aria-allowed-attr`) and describes nothing. // // Only what the field has to say is handed down: a key passed as // `undefined` would still replace what the control says of itself. const joinedDescribedBy = [errorId, hintId, noteId].filter(Boolean).join(" ") || undefined; const control = React.isValidElement(children) && !isPlainWrapper(children) ? (children as React.ReactElement>) : null; const child = control ? React.cloneElement(control, { ...(required ? { "aria-required": true } : {}), ...(invalid ? { "aria-invalid": true } : {}), ...(control.props["aria-describedby"] === undefined && joinedDescribedBy ? { "aria-describedby": joinedDescribedBy } : {}), }) : children; return (
{child} {invalid && errorMessage && (

)} {hint && (

{hint}

)} {/* Not a live region: it changes as the user types, and announcing it on every keystroke would drown out the typing. It is read with the field instead, through `aria-describedby`, the next time it gets focus. */} {note && (

)}
); } /** * A Yes/No question as a row: the question on the left, the two answers on * the right. Both answers are always on show, because the published document * prints the answer either way: "No" is a choice the customer should see * being made, not the absence of a tick. * * Never blank: a statement starts on "No", which is what the document printed * before these were asked, so there is nothing to validate. */ export function StatementYesNo(props: { id: string; label: string; value: boolean; onChange: (value: boolean) => void; yesLabel: string; noLabel: string; disabled?: boolean; }) { const { id, label, value, onChange, yesLabel, noLabel, disabled } = props; const labelId = `${id}-label`; return (
{label} onChange(next === "yes")} disabled={disabled} />
); }