Documentation / Forms
FormField
The frame of a field the library does not draw: a colour picker, an editor, a control of your application. FormField writes the label, the description, the help and the error message as Input and Select write them, and hands the control, through a function, the attributes that tie it to them: id (which the label points at), aria-describedby, aria-invalid and aria-required. Spreading them on the control is enough.
error is read as on every field of the library: true marks the field invalid, a text does too and becomes the message, written under the control in place of helperText; false and "" are no error. The message is not an alert: it describes the field, read when the field takes the focus — where a form library sends it after a submit that failed.
No directive and no hook but useId: it renders on the server too, and a Server Component can pass it its function.
Examples
A native control
The label points at the id it receives; helperText is written under the control.
import { FormField } from "@synertech/ui";
export function Basic() {
return (
<FormField label="Couleur des factures" helperText="Utilisée pour l’en-tête et les totaux.">
{(control) => (
<input
{...control}
type="color"
defaultValue="#2563eb"
className="h-10 w-20 cursor-pointer rounded-lg border border-gray-300 bg-gray-50 p-1 dark:border-gray-600 dark:bg-gray-700"
/>
)}
</FormField>
);
}Validation on submit
error takes the message: it replaces the help, and the control turns aria-invalid.
import { useState, type FormEvent } from "react";
import { Button, FormField } from "@synertech/ui";
export function Validation() {
const [size, setSize] = useState("");
const [error, setError] = useState<string>();
const submit = (event: FormEvent) => {
event.preventDefault();
setError(size ? undefined : "Choisissez une taille.");
};
return (
<form onSubmit={submit} noValidate className="flex max-w-sm flex-col gap-4">
<FormField
label="Taille du t-shirt"
description="Elle figure sur l’étiquette de votre badge."
helperText="Les tailles suivent la grille européenne."
error={error}
required
>
{(control) => (
<select
{...control}
value={size}
onChange={(event) => setSize(event.target.value)}
className="w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 aria-[invalid=true]:border-red-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white"
>
<option value="">Choisir…</option>
<option value="s">S</option>
<option value="m">M</option>
<option value="l">L</option>
</select>
)}
</FormField>
<div>
<Button type="submit" label="Enregistrer" />
</div>
</form>
);
}A control with no native element
A <label for> only names a native element: the group takes aria-labelledby={field.labelId}.
import { useState } from "react";
import { FormField } from "@synertech/ui";
const SWATCHES = [
{ value: "blue", label: "Bleu", className: "bg-blue-600" },
{ value: "green", label: "Vert", className: "bg-green-700" },
{ value: "amber", label: "Ambre", className: "bg-amber-500" },
{ value: "rose", label: "Rose", className: "bg-rose-600" },
];
export function CustomControl() {
const [colour, setColour] = useState<string>();
return (
<FormField
label="Couleur de l’étiquette"
error={colour === "amber" ? "L’ambre ne se lit pas sur le fond de l’étiquette." : undefined}
required
>
{(control, field) => (
<div {...control} role="radiogroup" aria-labelledby={field.labelId} className="flex gap-2">
{SWATCHES.map((swatch) => (
<label key={swatch.value} className="cursor-pointer">
<input
type="radio"
name="label-colour"
value={swatch.value}
checked={colour === swatch.value}
onChange={() => setColour(swatch.value)}
className="peer sr-only"
/>
<span
className={`block h-8 w-8 rounded-full ring-offset-2 peer-checked:ring-2 peer-checked:ring-gray-900 peer-focus-visible:ring-2 peer-focus-visible:ring-synertech-primary-500 dark:ring-offset-gray-900 dark:peer-checked:ring-white ${swatch.className}`}
/>
<span className="sr-only">{swatch.label}</span>
</label>
))}
</div>
)}
</FormField>
);
}Inline or hidden label
inline puts the label left of the control; hideLabel keeps it for screen readers.
import { FormField } from "@synertech/ui";
export function Layout() {
const inputClasses =
"w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 placeholder-gray-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-300";
return (
<div className="flex max-w-md flex-col gap-4">
<FormField label="Site web" inline helperText="Avec https://">
{(control) => (
<input {...control} type="url" placeholder="https://" className={inputClasses} />
)}
</FormField>
<FormField label="Rechercher une facture" hideLabel>
{(control) => (
<input
{...control}
type="search"
placeholder="Rechercher une facture…"
className={inputClasses}
/>
)}
</FormField>
</div>
);
}API reference
Components
The label, the help and the error of a field the library does not draw: a colour picker, a rich text editor, a control of the application. It reads error as Input, Select and the others do, writes the same frame around the control, and hands the control the attributes that tie it to that frame. No hooks but useId, and no handler: it renders on the server too, and a Server Component can pass it its function.
import { FormField } from "@synertech/ui";Props — FormFieldProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
(control: FormFieldControlProps, field: FormFieldState) => ReactNode |
— | Renders the control, given the attributes to spread on it and what the field knows. A function rather than an element: the field never writes over a prop of the control. |
description |
ReactNode |
— | Written under the label, above the control. |
error |
boolean | string |
— |
|
helperText |
ReactNode |
— | Written under the control; a message passed as |
hideLabel |
boolean |
false |
Keeps the label for screen readers only: it is still the field's name. |
id |
string |
— | The id of the control. Generated when left out. |
inline |
boolean |
false |
The label beside the control rather than above it. |
label |
ReactNode |
— | The field's label, above the control (beside it with |
required |
boolean |
false |
Draws the asterisk after the label and sets |
Types
FormFieldControlProps
InterfaceWhat a control spreads to be the field's: the id the label points at, and the attributes that tell a screen reader what the field says around it.
interface FormFieldControlProps| Field | Type | Default | Description |
|---|---|---|---|
id• |
string |
— | The id the label points at ( |
aria-describedby |
string |
— | The description, the help or the message on screen, in that order. |
aria-invalid |
true |
— | Set while the field is invalid, never written |
aria-required |
true |
— | Set when the field is |
FormFieldState
InterfaceWhat the field knows, for a control that needs more than its attributes.
interface FormFieldState| Field | Type | Default | Description |
|---|---|---|---|
invalid• |
boolean |
— | The field is invalid: |
labelId• |
string | undefined |
— | The id of the label. A |