Field
A component that provides labeling and validation for form controls.
View as MarkdownVisible on your profile
Anatomy
Import the component and assemble its parts:
import { Field } from '@base-ui/react/field';
<Field.Root>
<Field.Label />
<Field.Control />
<Field.Description />
<Field.Item />
<Field.Error />
<Field.Validity />
</Field.Root>API reference
Root
Groups all parts of the field.
Renders a <div> element.
namestring—
string- Name
- Description
Identifies the field when a form is submitted. Takes precedence over the
nameprop on the<Field.Control>component.- Type
string | undefined
actionsRef| React.RefObject<Field.Root.Actions
| null>—
| React.RefObject<Field.Root.Actions
| null>- Name
- Description
A ref to imperative actions.
validate: Validates the field when called.
- Type
React.RefObject<Field.Root.Actions | null> | undefined
dirtyboolean—
boolean- Name
- Description
Whether the field’s value has been changed from its initial value. Useful when the field state is controlled by an external library.
- Type
boolean | undefined
touchedboolean—
boolean- Name
- Description
Whether the field has been touched. Useful when the field state is controlled by an external library.
- Type
boolean | undefined
disabledbooleanfalse
boolean- Name
- Description
Whether the component should ignore user interaction. Takes precedence over the
disabledprop on the<Field.Control>component.- Type
boolean | undefined- Default
false
invalidboolean—
boolean- Name
- Description
Whether the field is invalid. Useful when the field state is controlled by an external library.
- Type
boolean | undefined
validatefunction—
function- Name
- Description
A function for custom validation. Return a string or an array of strings with the error message(s) if the value is invalid, or
nullif the value is valid. Asynchronous functions are supported, but they do not prevent form submission when usingvalidationMode="onSubmit".- Type
((value: unknown, formValues: Form.Values<string, any>) => string | string[] | Promise<string | string[] | null> | null) | undefined
validationModeForm.ValidationMode'onSubmit'
Form.ValidationMode- Name
- Description
Determines when the field should be validated. This takes precedence over the
validationModeprop on<Form>.onSubmit: triggers validation when the form is submitted, and re-validates on change after submission.onBlur: triggers validation when the control loses focus.onChange: triggers validation on every change to the control value.
- Type
Form.ValidationMode | undefined- Default
'onSubmit'
validationDebounceTimenumber0
number- Description
How long to wait between
validatecallbacks ifvalidationMode="onChange"is used. Specified in milliseconds.- Type
number | undefined- Default
0
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
data-disabled
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Additional Types
Field.Root.Props
Re-Export of Root props as FieldRootProps
Field.Root.State
type FieldRootState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Field.Root.Actions
type FieldRootActions = { validate: () => void };Label
An accessible label that is automatically associated with the field control.
Renders a <label> element.
nativeLabelbooleantrue
boolean- Name
- Description
Whether the component renders a native
<label>element when replacing it via therenderprop. Set tofalseif the rendered element is not a label (for example,<div>).This is useful to avoid inheriting label behaviors on
<button>controls (such as<Select.Trigger>and<Combobox.Trigger>), including avoiding:hoveron the button when hovering the label, and preventing clicks on the label from firing on the button.- Type
boolean | undefined- Default
true
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
data-disabled
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Additional Types
Field.Label.Props
Re-Export of Label props as FieldLabelProps
Field.Label.State
type FieldLabelState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Control
The form control to label and validate.
Renders an <input> element.
You can omit this part and use any Base UI input component instead. For example, Input, Checkbox, or Select, among others, will work with Field out of the box.
defaultValueUnion—
Union- Name
- Type
string | number | string[] | undefined
onValueChangefunction—
function- Name
- Description
Callback fired when the
valuechanges. Use when controlled.- Type
| (( value: string, eventDetails: Field.Control.ChangeEventDetails, ) => void) | undefined
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
data-disabled
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Additional Types
Field.Control.Props
Re-Export of Control props as FieldControlProps
Field.Control.State
type FieldControlState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Field.Control.ChangeEventReason
type FieldControlChangeEventReason = 'none';Field.Control.ChangeEventDetails
type FieldControlChangeEventDetails = {
reason: 'none';
event: Event;
cancel: () => void;
allowPropagation: () => void;
isCanceled: boolean;
isPropagationAllowed: boolean;
trigger: Element | undefined;
};Description
A paragraph with additional information about the field.
Renders a <p> element.
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
data-disabled
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Additional Types
Field.Description.Props
Re-Export of Description props as FieldDescriptionProps
Field.Description.State
type FieldDescriptionState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Item
Groups individual items in a checkbox group or radio group with a label and description.
Renders a <div> element.
disabledbooleanfalse
boolean- Name
- Description
Whether the wrapped control should ignore user interaction. The
disabledprop on<Field.Root>takes precedence over this.- Type
boolean | undefined- Default
false
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
Additional Types
Field.Item.Props
Re-Export of Item props as FieldItemProps
Field.Item.State
type FieldItemState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Error
An error message displayed if the field control fails validation.
Renders a <div> element.
matchUnion—
Union- Name
- Description
Determines whether to show the error message according to the field’s ValidityState. Specifying
truewill always show the error message, and lets external libraries control the visibility.- Type
| boolean | 'valid' | 'badInput' | 'customError' | 'patternMismatch' | 'rangeOverflow' | 'rangeUnderflow' | 'stepMismatch' | 'tooLong' | 'tooShort' | 'typeMismatch' | 'valueMissing' | undefined
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Field.Root.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Field.Root.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Field.Root.State, ) => ReactElement) | undefined
data-disabled
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Additional Types
Field.Error.Props
Re-Export of Error props as FieldErrorProps
Field.Error.State
type FieldErrorState = {
disabled: boolean;
touched: boolean;
dirty: boolean;
valid: boolean | null;
filled: boolean;
focused: boolean;
};Validity
Used to display a custom message based on the field’s validity.
Requires children to be a function that accepts field validity state as an argument.
children*((state: Field.Validity.State) => React.ReactNode)—
((state: Field.Validity.State) => React.ReactNode)- Name
- Description
A function that accepts the field validity state as an argument.
<Field.Validity> {(validity) => { return <div>...</div> }} </Field.Validity>- Type
((state: Field.Validity.State) => React.ReactNode)
Additional Types
Field.Validity.Props
Re-Export of Validity props as FieldValidityProps
Field.Validity.State
type FieldValidityState = {
validity: {
badInput: boolean;
customError: boolean;
patternMismatch: boolean;
rangeOverflow: boolean;
rangeUnderflow: boolean;
stepMismatch: boolean;
tooLong: boolean;
tooShort: boolean;
typeMismatch: boolean;
valueMissing: boolean;
valid: boolean | null;
};
errors: string[];
value: unknown;
error: string;
initialValue: unknown;
};