Form
A form wrapper with a submit helper, an error summary that links to invalid fields, and an actions footer.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- 2 parts, named exports
- A native form; give it an accessible name with aria-label or aria-labelledby when the page has several.
- The error summary is a role=alert region at the top; each item is a link that moves focus to the invalid control.
- Pass each error once, in the Form's errors; a Field with the same name shows it inline and sets aria-invalid and aria-describedby on its control. The summary announces it, so the inline text is not announced twice.
- Styles in one file:
form.css
Import
import '@monoline/css';
import {Form} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add form
Usage
import {useState} from 'react';
import {Button, Field, Form, FormActions, Input, type FormError} from '@monoline/react';
function Example() {
const [errors, setErrors] = useState<FormError[]>([]);
return (
<Form
aria-label="Contact"
noValidate
preventDefault
errors={errors}
onSubmit={(e) => {
const data = new FormData(e.currentTarget);
const next: FormError[] = [];
if (!data.get('name')) next.push({name: 'name', message: 'Enter your name.'});
if (!data.get('email')) next.push({name: 'email', message: 'Enter your email.'});
setErrors(next);
}}
>
<Field label="Name" name="name"><Input name="name" /></Field>
<Field label="Email" name="email"><Input name="email" type="email" /></Field>
<FormActions><Button type="submit" variant="solid">Send</Button><Button variant="line" type="reset">Clear</Button></FormActions>
</Form>
);
}
<Example />
Anatomy
Import the parts you need and nest them like this.
import {Form, FormActions} from '@monoline/react';
<Form>
<FormActions />
</Form>
Examples
With an error summary
Submit empty. Each error is passed once, in errors; every Field reads its own message by name, and the summary links to each invalid field.
Props
FormProps
| Prop | Type | Default | Description |
|---|---|---|---|
onSubmit | ((event: FormEvent<HTMLFormElement>) => void) | – | Called with the submit event. Combine with `preventDefault` when you handle the form in JS. |
preventDefault | boolean | – | Call `event.preventDefault()` before `onSubmit`, for forms handled in JS. Off by default so native posts still work. |
errors | FormError[] | – | Errors to list in a summary at the top of the form. Give each `Field` the same `name` and it shows its own message, so you pass each error once. A Field's own `error` prop still wins. |
errorsTitle | ReactNode | 'Fix these to continue' | Heading of the summary. Default "Fix these to continue". |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
This component sets no state attributes.
CSS classes
| Class | Stylesheet |
|---|---|
.form | form.css |
.form-actions | form.css |
.form-errors | form.css |
.form-errors-title | form.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.form {
border-color: var(--accent);
}
Accessibility
- A native form; give it an accessible name with aria-label or aria-labelledby when the page has several.
- The error summary is a role=alert region at the top; each item is a link that moves focus to the invalid control.
- Pass each error once, in the Form's errors; a Field with the same name shows it inline and sets aria-invalid and aria-describedby on its control. The summary announces it, so the inline text is not announced twice.