A small form
import { Button } from "@senvenli/react/action";
import { Form, FormFooter, FormRow, FormSection, Input, TextArea } from "@senvenli/react/form";
export default function Example() {
return (
<Form onSubmit={(event) => event.preventDefault()}>
<FormSection legend="Contact">
<FormRow>
<Input label="First name" name="firstName" autoComplete="given-name" defaultValue="Ada" isRequired />
<Input label="Last name" name="lastName" autoComplete="family-name" defaultValue="Lovelace" isRequired />
</FormRow>
<Input label="Email" name="email" type="email" autoComplete="email" isRequired />
<Input type="tel" label="Phone" name="phone" defaultCountry="US" suggestedCountries={["US", "CA", "GB"]} />
</FormSection>
<FormSection legend="Billing">
<Input label="Company" name="company" autoComplete="organization" />
<TextArea label="Notes" name="notes" maxLength={200} />
</FormSection>
<FormFooter>
<Button type="reset" variant="subtle">Cancel</Button>
<Button type="submit" variant="primary">Add customer</Button>
</FormFooter>
</Form>
);
}import { Form, FormSection, FormRow, FormFooter } from "@senvenli/react/form";Examples
Validation on submit
An invalid field takes focus and `onSubmit` is not called; entered values are kept.import { Button } from "@senvenli/react/action";
import { Banner } from "@senvenli/react/feedback";
import { Form, FormFooter, Input } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";
export default function Example() {
const [project, setProject] = useState<string | null>(null);
return (
<Stack space="space-300">
{project && <Banner tone="success" title={`${project} was created`} onDismiss={() => setProject(null)} />}
<Form
onSubmit={(event) => {
event.preventDefault();
setProject(String(new FormData(event.currentTarget).get("name")));
}}
>
<Input
label="Project name"
name="name"
isRequired
validate={(value) => (value.trim().length < 3 ? "Use at least 3 characters, so the team can tell projects apart." : null)}
/>
<Input label="Client email" name="email" type="email" isRequired errorMessage="Enter a valid email: name@example.com" />
<Input type="number" label="Budget" name="budget" currency="USD" minValue={0} isOptional />
<FormFooter>
<Button type="submit" variant="primary">Create project</Button>
</FormFooter>
</Form>
</Stack>
);
}Server errors
`validationErrors` marks fields by name until the person edits them.import { Button } from "@senvenli/react/action";
import { Form, FormFooter, Input } from "@senvenli/react/form";
import { useState } from "react";
const TAKEN = "ada@example.com";
export default function Example() {
const [errors, setErrors] = useState<Record<string, string>>({});
return (
<Form
validationErrors={errors}
onSubmit={(event) => {
event.preventDefault();
// Stands in for the server's answer.
const email = new FormData(event.currentTarget).get("email");
setErrors(email === TAKEN ? { email: "That email already has an account. Sign in or use another one." } : {});
}}
>
<Input label="Full name" name="name" autoComplete="name" defaultValue="Ada Lovelace" isRequired />
<Input label="Email" name="email" type="email" autoComplete="email" defaultValue={TAKEN} isRequired />
<FormFooter>
<Button type="submit" variant="primary">Send invite</Button>
</FormFooter>
</Form>
);
}Guidelines
When to use it
- Collecting input that is submitted together.
Use something else when
Do and avoid
- One primary submit button in the FormFooter.
- Mark the exception: required when most fields are optional, optional when most are required.
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Enter | Submits the form from a text field. |
Built in
- On submit, focus moves to the first invalid field.
- FormSection is a fieldset named by its legend.
You provide
- A legend for each FormSection.
API reference
Form
| Prop | Type | Default | Description |
|---|---|---|---|
width | "full" | "narrow" | "narrow" |
|
width"full" | "narrow"Default"narrow"narrow(default) caps single-column forms at 560px.
Also takes 77 standard DOM and ARIA attributes (id, aria-label, event handlers…).
FormSection
| Prop | Type | Default | Description |
|---|---|---|---|
legendRequired | React.ReactNode | – | The section title ( |
childrenRequired | React.ReactNode | – | |
isDisabled | boolean | – |
legendRequiredReact.ReactNodeThe section title (
heading-small).childrenRequiredReact.ReactNodeisDisabledboolean
FormRow
| Prop | Type | Default | Description |
|---|---|---|---|
childrenRequired | React.ReactNode | – |
childrenRequiredReact.ReactNode