Form
Form recipe: grid layout, sections, required fields, inline validation on blur, error summary, submit with loading. Includes useForm(), FormRow and FormSection.
Code
import * as React from 'react';
import {
Badge,
Button,
Card,
Checkbox,
Form,
FormRow,
Select,
TextField,
Textarea,
useForm,
} from '@fruit-labs/design';
export default function Demo() {
const f = useForm({
initialValues: {
first: 'Ada',
last: '',
email: '',
role: '',
bio: '',
terms: false,
},
rules: {
first: {
required: 'Enter your first name.',
},
last: {
required: 'Enter your last name.',
},
email: {
required: 'Enter your email.',
pattern: /^[^@\s]+@[^@\s]+\.[^@\s]+$/,
patternMessage: 'Enter a full email address, like name@company.com.',
},
role: {
required: 'Choose a role.',
},
terms: {
required: 'Accept the terms to continue.',
},
},
onSubmit: (v) => new Promise((r) => setTimeout(r, 800)),
});
return (
<Card>
<Form
columns={2}
title="Create account"
description="Tell us about you."
onSubmit={f.handleSubmit}
error={f.errors._form}
actions={
<>
{f.submitted && (
<Badge tone="success" dot>
Saved
</Badge>
)}
<Button variant="gray" onClick={f.reset}>
Cancel
</Button>
<Button type="submit" loading={f.submitting}>
Create account
</Button>
</>
}
>
<TextField label="First name" autoComplete="given-name" {...f.field('first')} />
<TextField label="Last name" autoComplete="family-name" {...f.field('last')} />
<FormRow span="full">
<TextField
label="Work email"
type="email"
icon="mail"
autoComplete="email"
{...f.field('email')}
/>
</FormRow>
<Select
label="Role"
placeholder="Choose a role"
options={['Designer', 'Engineer', 'Product manager', 'Other']}
{...f.field('role', 'value')}
/>
<TextField label="Company" optional placeholder="Acme Inc." />
<FormRow span="full">
<Textarea label="Bio" optional maxLength={200} showCount {...f.field('bio')} />
</FormRow>
<FormRow span="full">
<Checkbox {...f.field('terms', 'checked')}>I accept the terms</Checkbox>
</FormRow>
</Form>
</Card>
);
}Install
From npm:
npm install @fruit-labs/design
import { Form } from '@fruit-labs/design';Or copy the TypeScript source into your project:
npx shadcn@latest add https://design.fruitlabs.online/r/form.jsonGuidelines
useForm({ initialValues, rules, onSubmit })returnsfield(name)bindings (value, onChange, onBlur, error, required),handleSubmit,submitting,submitted,errors.- Rules per field:
required(true or message),minLength,pattern+patternMessage,validate(value, values)→ message. - Errors appear after a field is blurred or on submit; submit focuses the first invalid field. A rejected
onSubmitshowserrors._form(pass it to<Form error>). - Layout:
<Form columns={2}>;<FormRow span="full">for wide fields;<FormSection title>groups related fields. Collapses to one column under 640px. - Actions: Cancel (gray) then Submit (filled,
type="submit",loading={submitting}).
Props
| Prop | Type | Required |
|---|---|---|
columns | number | No |
gap | number | No |
title | React.ReactNode | No |
description | React.ReactNode | No |
error | React.ReactNode | No |
actions | React.ReactNode | No |
onSubmit | (e: React.FormEvent) => void | No |
className | string | No |
children | React.ReactNode | No |