Raft 设计系统

组件 · 输入

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.

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: help text below the control. 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.
  • 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.

API

Field

Prop类型默认值
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

Prop类型默认值
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

Prop类型默认值
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

Prop类型默认值
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

Prop类型默认值
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

Prop类型默认值
optionalboolean
requiredboolean
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

Prop类型默认值
children (required)A function that accepts the field validity state as an argument.(state: FieldValidityState) => React.ReactNode

改动

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