"use client"; import * as React from "react"; import { CircleAlertIcon, TriangleAlertIcon, XIcon } from "lucide-react"; import { Input } from "./input"; import { Label } from "./label"; import { Textarea } from "./textarea"; import { cn } from "../lib/cn"; // The design system's Input as a product form uses it (ACC-534): the label // above, the field, and under it a hint and the reason it is wrong. The bare // `Input` of this folder is the field alone; this is the same field with what // the design system's `label`, `error` and `hint` put around it, so a form // does not write that by hand for each of its fields. /** * The reason a field is wrong, as the design system writes it: the words in * black with a red icon beside them, so the error is never said by colour * alone and holds on every ground a field sits on. Exported for a control * that is not a text field and says its error the same way (a required * checkbox). */ export function FieldError({ id, className, children, }: { id?: string; className?: string; // Optional for the type checker only: the dashboard's React types do not // read a JSX child as this prop. children?: React.ReactNode; }) { return (

); } /** * Advice about a field that is not wrong (ACC-652 U1): the server takes the * value, but something about it deserves a look (a description search * engines will cut). Drawn as `FieldError` with the warning tone's icon, as * the design system's Notice draws a warning, and tied to the field without * `aria-invalid`, so a screen reader never says "invalid" about it. */ export function FieldWarning({ id, className, children, }: { id?: string; className?: string; children?: React.ReactNode; }) { return (

); } export type TextFieldProps = Omit, "id"> & { id: string; label: string; error?: string; hint?: string; /** The id of a note outside the field that is about it too. */ describedBy?: string; /** Classes of the field as a whole (its place in a grid). */ fieldClassName?: string; /** * A button that acts on the field («Cerca», «Applica il coupon»): drawn on * the input's own row, as tall as it, with the label above the row and the * hint and the error under it. Under the input on a phone. */ action?: React.ReactNode; }; /** * A labelled input: the label above, the field, and under it a hint and the * reason it is wrong. In error the field takes the design system's red * border and its "x", which the bare `Input` has no room to draw. A field * that cannot be edited (`readOnly`) is drawn on grey, so it does not read as * one waiting to be filled. * * The error is tied to the input (`aria-describedby`), not announced by * itself: the form says once that something needs checking and moves the * focus to the first field in error, which reads its own reason. */ export function TextField({ id, label, error, hint, describedBy, className, fieldClassName, action, ...props }: TextFieldProps) { const described = [error && `${id}-error`, hint && `${id}-hint`, describedBy] .filter(Boolean) .join(" ") || undefined; const input = (
{error && (
); return (
{/* A label can carry an address ("Per confermare scrivi "), one word up to 253 characters: it breaks rather than widen the form. */} {action ? ( // The row holds the input and its button only, so the button lines // up with the input, never with the label or a hint.
{input} {action}
) : ( input )} {hint && (

{hint}

)} {error && {error}}
); } type TextAreaFieldProps = Omit, "id"> & { id: string; label: string; error?: string; hint?: string; /** Said at the end of the hint's line, as a counter ("61/500 caratteri"). */ hintEnd?: string; /** Advice on a value that is not wrong (`FieldWarning`); an `error` wins. */ warning?: string; }; /** * `TextField` for a few lines: the same label, hint and reason around the * design system's Textarea (a motive, a note). */ export function TextAreaField({ id, label, error, hint, hintEnd, warning, className, ...props }: TextAreaFieldProps) { const advice = error ? undefined : warning; const described = [ error && `${id}-error`, advice && `${id}-warning`, (hint || hintEnd) && `${id}-hint`, ] .filter(Boolean) .join(" ") || undefined; return (