Raft Design System

Components · Inputs

Field

The label, help text and error around a form control, wired to it for assistive technology.

Stable
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.

field/compositions/preview

When to use

SituationUse
A labeled control in a form: an input, a textarea, a selectField
A checkbox, radio or switch with its label beside itField with FieldItem
A setting row with a switchA settings row (ListRow) with a Switch
A section of a large form, with a heading and a descriptionFormSection

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 FieldControl for another native control.
  • FieldDescription: a helper text below the control. It takes only a HelperText, made with helperText(content, { why, about }), and renders its why and about as data-why and data-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 same size as 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

VariantUse for
FieldLabel defaultForm fields
FieldLabel size="sm"Dense layouts where the default label is too large for the space
field/compositions/compact-label

States

field/compositions/error
  • Invalid: set invalid on Field and data-invalid on the control. The control's border turns danger-strong, and FieldError takes the place of FieldDescription, in danger-strong. When to check and how the message reads are in Forms.
  • Disabled: disabled on Field turns the label, help text and error foreground-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. Keep aria-required (or required) on a required control, so a screen reader still says so. FieldLabel's required prop, 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 optional in place of true, and FieldLabel leaves 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. FieldItem lets 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 FieldLabel an htmlFor that matches the control's id when the control is not a Base UI one, such as Textarea, and point aria-describedby at the description.
  • FieldError has role="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

PropTypeDefault
actionsRefA ref to imperative actions. - `validate`: Validates the field when called.RefObject<FieldRootActions | null>
dirtyWhether the field's value has been changed from its initial value. Useful when the field state is controlled by an external library.boolean
disabledWhether the component should ignore user interaction. Takes precedence over the `disabled` prop on the `<Field.Control>` component.booleanfalse
invalidWhether the field is invalid. Useful when the field state is controlled by an external library.boolean
nameIdentifies the field when a form is submitted. Takes precedence over the `name` prop on the `<Field.Control>` component.string
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
touchedWhether the field has been touched. Useful when the field state is controlled by an external library.boolean
validateA function for custom validation. Return a string or an array of strings with the error message(s) if the value is invalid. Returning nothing, `null`, an empty string, or an empty array means the value is valid. Asynchronous functions are supported, but they do not prevent form submission when using `validationMode="onSubmit"`.(value: unknown, formValues: Form.Values) => string | string[] | null | void | Promise<string | string[] | null | void>
validationDebounceTimeHow long to wait between `validate` callbacks if `validationMode="onChange"` is used. Specified in milliseconds.number0
validationModeDetermines when the field should be validated. This takes precedence over the `validationMode` prop on `<Form>`.FormValidationMode'onSubmit'

Also takes the attributes of its element.

FieldControl

PropTypeDefault
defaultValuestring | number | readonly string[]
onValueChangeCallback fired when the `value` changes. Use when controlled.(value: string, eventDetails: BaseField.Control.ChangeEventDetails) => void
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

FieldDescription

PropTypeDefault
children (required)HelperText
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

FieldError

PropTypeDefault
matchDetermines whether to show the error message according to the field's [ValidityState](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState). Specifying `true` will always show the error message, and lets external libraries control the visibility.boolean | keyof ValidityState
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

FieldItem

PropTypeDefault
size"sm" | "md""md"
disabledWhether the wrapped control should ignore user interaction. The `disabled` prop on `<Field.Root>` takes precedence over this.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

FieldLabel

PropTypeDefault
optionalMarks the field optional. `true` adds "(optional)", 4px after the label; pass the marker's text to write it in another language, such as "(选填)". A marker that opens with a full-width bracket gets no margin before it: the bracket already carries its space.ReactNode
requiredDeprecated. Draws an asterisk after the label.boolean
size"sm" | "md""md"
nativeLabelWhether the component renders a native `<label>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a label (for example, `<div>`).booleantrue
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

FieldValidity

PropTypeDefault
children (required)A function that accepts the field validity state as an argument.(state: FieldValidityState) => React.ReactNode

Changes

  • 0.103.0 · Breaking ChangesExplanation slots take only a HelperText: FieldDescription, FormSection's description, a ChoiceList item's description, SelectItemDescription, DialogDescription, AlertDialogDescription, DrawerDescription, PopoverDescription, EmptyStateDescription, CardDescription, CardFooter, BannerDescription, DialogFooterNote (work in progress), MessageActionCardHint, MessageActionCardHeaderDescription, MessageActionCardScopeDescription and MessageActionCardActionsNote. A HelperText is made only by helperText(content, { why, about }): content is any ReactNode (a string, a formatted message, a TextLink inside a sentence), why is "consequence", "audience" or "recovery", and about names the thing the line is about. Each slot renders the content with data-why and data-about, and in development warns once when it is given anything else, which it still renders. ToastDescription, NotificationCenterItemBody, EmptyStateTitle, ListRowDescription and the status and data parts are unchanged.
  • 0.101.0 · ChangedFieldLabel's required prop and LabelAsterisk are 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 @deprecated in the types (Label.Asterisk too), and the API tables of Field and Label say so. Mark an optional field with <FieldLabel optional> (or LabelOptional), leave a required one unmarked, and put required or aria-required on the control.
  • 0.8.0 · Breaking ChangesFieldLabel no longer carries leading-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 · AddedFieldItem takes size (sm, md), to match the size of its label.