Field
Slant / Forms
A label, a control, a description and an error, wired together with no ids to manage.
Demo
Usage
import { CheckboxGroup, Field, Input, Switch } from '@tuttn/slant'
// The parts need no ids: the field generates them and wires the control inside.
<Field invalid={!valid} required>
<Field.Label>Workspace</Field.Label>
<Input value={name} onChange={event => setName(event.target.value)} />
<Field.Description>Lowercase letters, digits and dashes.</Field.Description>
<Field.Error>Use three characters or more.</Field.Error>
</Field>
// Field.Label takes Label's hint and action.
<Field>
<Field.Label hint="Optional">Display name</Field.Label>
<Input />
</Field>
// Horizontal: the label beside a switch or a short input.
<Field orientation="horizontal">
<Field.Label>Auto-save</Field.Label>
<Switch defaultChecked onCheckedChange={save} />
</Field>
// The field's states reach the control. A state on the control itself wins.
<Field disabled>
<Field.Label>Region</Field.Label>
<Input defaultValue="eu-west" />
</Field>
// A control's own id is kept, and the label follows it.
<Field><Field.Label>Email</Field.Label><Input id="signup-email" type="email" /></Field>
// A group control is named by the label and described by the description and error.
<Field invalid={channels.length === 0} required>
<Field.Label>Notify me by</Field.Label>
<CheckboxGroup options={channelOptions} value={channels} onValueChange={setChannels} />
<Field.Error>Choose a channel.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
invalid | boolean | false | Marks the control invalid (aria-invalid) and shows `Field.Error`. |
disabled | boolean | false | Disables the control and dims the label. |
required | boolean | false | Makes the control required, and marks the label. |
readOnly | boolean | false | Makes the control read-only. |
orientation | 'vertical' | 'horizontal' | 'vertical' | The label above the control, or beside it (for a switch or a short input). |
Field.Label | LabelProps without htmlFor, required, disabled | — | The label. It points at the control with `for` (or, for a group control, names the group through aria-labelledby), shows the required mark while the field is required, and dims while the control is disabled. `hint` and `action` work as on Label. |
Field.Description | HTMLAttributes<HTMLParagraphElement> | — | Hint text under the control, in its aria-describedby while it is mounted. |
Field.Error | HTMLAttributes<HTMLParagraphElement> | — | The error message. It renders only while the field is invalid, and then joins the control's aria-describedby after the description. |
…rest | HTMLAttributes | — | className, style, other attributes and the ref go on each part's own element: the field's div, the <label>, and the two paragraphs. A part given its own id keeps it, and the control points at that id instead. |
Accessibility
- The parts need no ids.
Field.Labelpoints at the control withfor, so a click on it focuses the control or flips a switch. The control'saria-describedbylists the description, then the error while the field is invalid, then any ids of its own, and names only the parts that are mounted. invalidsetsaria-invalidon the control and mountsField.Error, so the error is read with the control only while it applies. Its square mark is decorative.- The required mark after the label is
aria-hidden. The control getsrequired, and that is what a screen reader announces. - Group controls (CheckboxGroup, RadioGroup, Rating, ToggleGroup, ThemeSelector, CodeInput) can't take a
for. The label drops it and names the group througharia-labelledby, the description and error describe the group, and the items inside don't take the field's id.
What the control gets
Every Slant form control inside a Field (Input, Textarea, NumberInput, SearchField, Switch, Checkbox, FileDrop and the group controls) takes the field's id, aria-describedby and aria-invalid, and its disabled, required and readOnly states where the control has them. A state given to the control itself wins over the field's: <Input disabled={false} /> stays enabled in a disabled field, and the label dims with the control, not with the field.