Components · Inputs
Field
The label, help text and error around a form control, wired to it for assistive technology.
import { Field } from "@botiverse/raft-ui";A field puts a label above a form control and help text below it, and replaces the help text with an error when the value is invalid. Every input, textarea and select in a form sits in a field.
When to use
Anatomy
Field: the column that holds the parts. Required.FieldLabel: the label, above the control. Required unless the control has its own accessible name.- The control: Input, InputGroup, Textarea, Select or
FieldControlfor another native control. FieldDescription: a helper text below the control. It takes only aHelperText, made withhelperText(content, { why, about }), and renders itswhyandaboutasdata-whyanddata-about. Only for a consequence (consequence), who sees the value (audience) or how to recover (recovery); otherwise leave it out (Writing › Helper text). Optional.FieldError: the error, in place of the help text. Shown only while the field is invalid.FieldItem: a row that puts a checkbox, radio or switch beside its label and description, 8px apart. It centres the control on the label's first line by moving it, so the row keeps its height: a switch overhangs the label's line rather than making the row taller. It works from its own line height, so give it the samesizeas the label:<FieldItem size="sm">with<FieldLabel size="sm">. A mismatch leaves the control 1px off (Alignment).FieldValidity: read access to the field's validity state, for custom messages.
Variants
States
- Invalid: set
invalidonFieldanddata-invalidon the control. The control's border turnsdanger-strong, andFieldErrortakes the place ofFieldDescription, indanger-strong. When to check and how the message reads are in Forms. - Disabled:
disabledonFieldturns the label, help text and errorforeground-disabled.
Content
Where the label and help text go is in Forms.
- Keep the label visible at all times. Don't use a floating label or a placeholder in its place.
- Write the label in sentence case: Agent name.
- Mark an optional field with
optional, which adds "(optional)". Don't mark required fields: marking the optional ones is enough. Keeparia-required(orrequired) on a required control, so a screen reader still says so.FieldLabel'srequiredprop, which draws an asterisk, is deprecated for that reason and will be removed: don't pass it. - In Chinese, the marker is in full-width brackets, straight after the label, with no margin before it: the full-width bracket already carries its space, and the 4px a Latin "(optional)" gets would double it. Pass the Chinese marker's text to
optionalin place oftrue, andFieldLabelleaves out the margin for a marker that opens with a full-width bracket. - Make the help text one line about what the value is for: Shown in chat and on tasks.
- Make the error say how to fix the value, with a correct example: Use lowercase and dashes: launch-week (Writing).
- A label, help text or error of any length wraps, and breaks inside a word that is wider than the line, so it never pushes the field, a dialog or the screen wider.
FieldItemlets its label column shrink for the same reason (Forms).
Accessibility
Field wraps Base UI Field. It connects the label, the description and the error to a Base UI control (Input, Select, Checkbox, Switch, RadioGroup) and passes the field's invalid and disabled state to it.
- Give
FieldLabelanhtmlForthat matches the control'sidwhen the control is not a Base UI one, such as Textarea, and pointaria-describedbyat the description. FieldErrorhasrole="alert", so the error is announced when it appears.- Put an action that belongs to the field at the end of its label row, such as Forgot password, after the control in the markup and place it beside the label with layout. Tab and a screen reader then reach the field before its action: email, password, Forgot password, Sign in.
API
Field
Also takes the attributes of its element.
FieldControl
Also takes the attributes of its element.
FieldDescription
Also takes the attributes of its element.
FieldError
Also takes the attributes of its element.
FieldItem
Also takes the attributes of its element.
FieldLabel
Also takes the attributes of its element.
FieldValidity
Related
Changes
- 0.103.0 · Breaking ChangesExplanation slots take only a
HelperText:FieldDescription,FormSection'sdescription, aChoiceListitem'sdescription,SelectItemDescription,DialogDescription,AlertDialogDescription,DrawerDescription,PopoverDescription,EmptyStateDescription,CardDescription,CardFooter,BannerDescription,DialogFooterNote(work in progress),MessageActionCardHint,MessageActionCardHeaderDescription,MessageActionCardScopeDescriptionandMessageActionCardActionsNote. AHelperTextis made only byhelperText(content, { why, about }):contentis anyReactNode(a string, a formatted message, aTextLinkinside a sentence),whyis"consequence","audience"or"recovery", andaboutnames the thing the line is about. Each slot renders the content withdata-whyanddata-about, and in development warns once when it is given anything else, which it still renders.ToastDescription,NotificationCenterItemBody,EmptyStateTitle,ListRowDescriptionand the status and data parts are unchanged. - 0.101.0 · Changed
FieldLabel'srequiredprop andLabelAsteriskare deprecated: a required field carries no asterisk ([Field › Content](https://raft-design.botiverse.build/components/field/#content), [Forms › Fields](https://raft-design.botiverse.build/patterns/forms/#fields)). Both are marked@deprecatedin the types (Label.Asterisktoo), and the API tables of Field and Label say so. Mark an optional field with<FieldLabel optional>(orLabelOptional), leave a required one unmarked, and putrequiredoraria-requiredon the control. - 0.8.0 · Breaking Changes
FieldLabelno longer carriesleading-none. tailwind-merge already dropped it beside the type class, so most labels don't change, but a label whose class list kept it is now a full line high. - 0.8.0 · Added
FieldItemtakessize(sm,md), to match the size of its label.