组件 · 输入
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
| Situation | Use |
|---|---|
| A labeled control in a form: an input, a textarea, a select | Field |
| A checkbox, radio or switch with its label beside it | Field with FieldItem |
| A setting row with a switch | A settings row (ListRow) with a Switch |
| A section of a large form, with a heading and a description | FormSection |
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: 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 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
| Variant | Use for |
|---|---|
FieldLabel default | Form fields |
FieldLabel size="sm" | Dense layouts where the default label is too large for the space |
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. - 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.
API
Field
| Prop | 类型 | 默认值 |
|---|---|---|
| A ref to imperative actions. - `validate`: Validates the field when called. | RefObject<FieldRootActions | null> | |
| Whether the field's value has been changed from its initial value. Useful when the field state is controlled by an external library. | boolean | |
| Whether the component should ignore user interaction. Takes precedence over the `disabled` prop on the `<Field.Control>` component. | boolean | false |
| Whether the field is invalid. Useful when the field state is controlled by an external library. | boolean | |
| Identifies the field when a form is submitted. Takes precedence over the `name` prop on the `<Field.Control>` component. | string | |
| Allows you to replace the component's HTML element with a different tag, or compose it with another component. | ReactElement | ((props, state) => ReactElement) | |
| Whether the field has been touched. Useful when the field state is controlled by an external library. | boolean | |
| A 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> | |
| How long to wait between `validate` callbacks if `validationMode="onChange"` is used. Specified in milliseconds. | number | 0 |
| Determines 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 | 类型 | 默认值 |
|---|---|---|
| string | number | readonly string[] | ||
| Callback fired when the `value` changes. Use when controlled. | (value: string, eventDetails: BaseField.Control.ChangeEventDetails) => void | |
| Allows 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 | 类型 | 默认值 |
|---|---|---|
| Allows 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 | 类型 | 默认值 |
|---|---|---|
| Determines 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 | |
| Allows 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 | 类型 | 默认值 |
|---|---|---|
| "sm" | "md" | "md" | |
| Whether the wrapped control should ignore user interaction. The `disabled` prop on `<Field.Root>` takes precedence over this. | boolean | false |
| Allows 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 | 类型 | 默认值 |
|---|---|---|
| boolean | ||
| boolean | ||
| "sm" | "md" | "md" | |
| Whether 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>`). | boolean | true |
| Allows 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 | 类型 | 默认值 |
|---|---|---|
| A function that accepts the field validity state as an argument. | (state: FieldValidityState) => React.ReactNode |
相关组件
改动
- 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.