Field
Puts a name above a control, help under it, and the error next to it when something is wrong. Every control in a form goes inside one, so nobody has to guess what to write.
import { Field, FieldSkeleton } from '@sypnia/ui/react'
Label and hint
A name, and help that stays visible. The optional field says so beside its label.
Shown in the sidebar and in invitations.
Invoices go here. Without it, they go to the owner.
Code
import { Field, FormGrid, Input } from '@sypnia/ui/react';
export default function LabelAndHint() {
return (
<FormGrid>
<Field label="Project name" hint="Shown in the sidebar and in invitations.">
<Input defaultValue="Storefront" />
</Field>
<Field label="Billing email" optional hint="Invoices go here. Without it, they go to the owner.">
<Input type="email" placeholder="billing@northwind.io" />
</Field>
</FormGrid>
);
} Error
Type in the field: the error appears while the name is not valid and leaves when it is.
Use only lowercase letters, numbers and underscores, starting with a letter: order_items.
It becomes part of the API address.
Code
import { useState } from 'react';
import { Field, FormGrid, Input } from '@sypnia/ui/react';
// Type a capital letter or a space: the error says what is wrong and how to fix it.
export default function WithError() {
const [name, setName] = useState('Order Items');
const error = name === '' ? 'Write a name for the table.'
: /^[a-z][a-z0-9_]*$/.test(name) ? undefined
: 'Use only lowercase letters, numbers and underscores, starting with a letter: order_items.';
return (
<FormGrid>
<Field label="Table name" hint="It becomes part of the API address." error={error}>
<Input machine value={name} onChange={(e) => setName(e.target.value)} />
</Field>
</FormGrid>
);
} Around any control
A select, an input with a unit and a textarea, with the same label, hint and error.
Where the data is stored. It cannot be changed later.
Write a number between 60 and 6,000.
Code
import { Field, FormGrid, InputGroup, Input, NativeSelect, Textarea } from '@sypnia/ui/react';
// The same label, hint and error work around any control.
export default function AnyControl() {
return (
<FormGrid>
<Field label="Region" hint="Where the data is stored. It cannot be changed later.">
<NativeSelect defaultValue="eu-central">
<option value="eu-central">Frankfurt</option>
<option value="us-east">Virginia</option>
<option value="sa-east">São Paulo</option>
</NativeSelect>
</Field>
<Field label="Request limit" error="Write a number between 60 and 6,000.">
<InputGroup end="per minute"><Input type="number" defaultValue={12000} /></InputGroup>
</Field>
<Field label="Notes" optional wide>
<Textarea placeholder="What is this project for?" />
</Field>
</FormGrid>
);
} Skeleton
The real piece and its skeleton: the same box, so nothing moves when the data arrives.
Shown to everyone in the team.
Code
import type { CSSProperties } from 'react';
import { Field, FieldSkeleton, Input } from '@sypnia/ui/react';
// A field whose value is loading: the same box, the size of its control. The skeleton has the same box as the real one, so nothing moves when the data arrives.
export default function Skeleton() {
return (
<div className="sy-grid" style={{ '--min': '16rem' } as CSSProperties}>
<Field label="Project name" hint="Shown to everyone in the team."><Input defaultValue="storefront" /></Field>
<FieldSkeleton hint />
</div>
);
} When to use it
Use it when
- Any control in a form: Input, Textarea, NativeSelect, InputGroup, PasswordInput, CopyField.
- The person needs to know the format before typing: write it in
hint, which is always visible. - Something is wrong: pass
errorwith what happened and how to fix it. Its presence marks the control as invalid.
Do not use it when
- A single Checkbox, Radio or Switch: they carry their own label and description.
- A group of choices with one question: that is a Fieldset.
- The label would be hidden and a placeholder would stand in for it: a placeholder disappears as soon as the person types.
For everyone
- The label is tied to the control: pressing it moves the focus there, and screen readers say it.
- The hint and the error are tied with
aria-describedby; the error is read first. The control getsaria-invalid. - An error is never colour alone: it has an icon and words, and it says how to fix the problem.
- Required is the default and is not marked. Mark the few fields that are
optional.
Props
| Field | Type | Default |
|---|---|---|
labelrequired | ReactNode | |
className | string | |
errorWhat is wrong and how to fix it. Its presence marks the control as invalid. | ReactNode | |
hintHelp that is always visible: what to write, in what format. | ReactNode | |
optionalAdds «optional» beside the label. Required is the default and is not marked. | boolean | |
wideSpans both columns of a FormGrid. | boolean |
| FieldSkeleton | Type | Default |
|---|---|---|
hint | boolean | |
rows | number | |
size | "sm" | "md" | "lg" | md |