Form
Coordinate fields: one effect at a time, the first invalid field, the button ready to submit.
Resources- Frames
- 0
- Active effects
- 0
- Renderer
- none
form.ts
import "@shader-ui/core/styles.css";
import { watchForm } from "@shader-ui/core";
const pay = () => new Promise<void>((resolve) => setTimeout(resolve, 1400));
document.body.insertAdjacentHTML(
"beforeend",
`<form id="order">
<label for="name">Full name</label>
<input id="name" autocomplete="name" required>
<label for="address">Delivery address</label>
<input id="address" autocomplete="street-address" required>
<fieldset>
<legend>Delivery</legend>
<label><input type="radio" name="delivery" value="standard" required> Standard, free</label>
<label><input type="radio" name="delivery" value="express" required> Express, €9.90</label>
</fieldset>
<label><input type="checkbox" required> I accept the terms of sale</label>
<button type="submit">Pay €49.00</button>
</form>`,
);
watchForm(document.querySelector<HTMLFormElement>("#order")!, {
guide: true,
onSubmit: (event) => {
event.preventDefault();
return pay();
},
});Order.tsx
import { Button, Checkbox, Form, Input, Radio } from "@shader-ui/react";
const pay = () => new Promise<void>((resolve) => setTimeout(resolve, 1400));
export function Order() {
return (
<Form
guide
onSubmit={(e) => {
e.preventDefault();
return pay();
}}
>
<label htmlFor="name">Full name</label>
<Input id="name" autoComplete="name" required />
<label htmlFor="address">Delivery address</label>
<Input id="address" autoComplete="street-address" required />
<fieldset>
<legend>Delivery</legend>
<label>
<Radio name="delivery" value="standard" required /> Standard, free
</label>
<label>
<Radio name="delivery" value="express" required /> Express, €9.90
</label>
</fieldset>
<label>
<Checkbox required /> I accept the terms of sale
</label>
<Button type="submit">Pay €49.00</Button>
</Form>
);
}When to use it
- To group fields and a submission. With guide, on a form people discover: sign-up, checkout.
- guide is useless on a short, familiar form (search, sign-in): the glow teaches nothing there.
Recommendations
- Return a promise from onSubmit: sending, success and failure play with no extra code.
- ready for what the library cannot read: captcha, payment.
- Do not disable the button while the form is incomplete.
Accessibility
The library
- Native <form> element: Enter submits, native validation kept.
- On submit, focus moves to the first invalid field; the error wave follows the form order, counts as a single effect and lasts under 5 s.
- Double submit ignored, without disabling the button.
- The "required" glow is CSS and its space is reserved: it never shifts the layout.
- No effect on a field that cannot be seen.
Integration
- The requirement written in the label (an explained asterisk, or "required"): the glow doubles it, never carries it.
- Tied error texts, and the success or failure of the submission announced with aria-live.
WCAG 2.2 criteria: Use of Color · Pause, Stop, Hide · Error Identification · Labels or Instructions · Status Messages
Props
| Prop | Type | Purpose |
|---|---|---|
onSubmit | (e) => unknown | A returned promise: busy button, double submit ignored, then success or failure. |
guide | boolean | On first interaction, a glow after the label of each required field. |
ready | boolean | What the library cannot read (captcha, payment): false until ready. |
… | FormHTMLAttributes | The attributes of a native <form>. |
Spec
The normative spec for this component: Formulaire
Availability and resources
- Status
- available · open source
- Packages
- @shader-ui/react (React, Next.js)
@shader-ui/core (TypeScript, plain HTML)