Field
A component that provides labelling and validation for form controls.
View as MarkdownVisible on your profile
Anatomy
Import the component and assemble its parts:
import { Field } from '@base-ui-components/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
—
namestring
—- Name
- Description
Identifies the field when a form is submitted. Takes precedence over the
nameprop on the<Field.Control>component.- Type
string | undefined
dirtyboolean
—
dirtyboolean
—- 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
—
touchedboolean
—- Name
- Description
Whether the field has been touched. Useful when the field state is controlled by an external library.
- Type
boolean | undefined
disabledboolean
false
disabledboolean
false
- 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
—
invalidboolean
—- Name
- Description
Whether the field is invalid. Useful when the field state is controlled by an external library.
- Type
boolean | undefined
validateUnion
—
validateUnion
—- 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.- Type
| (( value: unknown, formValues: Record<string, unknown>, ) => | string | string[] | Promise<string | string[] | null> | null) | undefined
validationMode'onBlur' | 'onChange'
'onBlur'
validationMode'onBlur' | 'onChange'
'onBlur'
- Name
- Description
Determines when the field should be validated.
- onBlur triggers validation when the control loses focus
- onChange triggers validation on every change to the control value
- Type
'onBlur' | 'onChange' | undefined- Default
'onBlur'
validationDebounceTimenumber
0
validationDebounceTimenumber
0
- Description
How long to wait between
validatecallbacks ifvalidationMode="onChange"is used. Specified in milliseconds.- Type
number | undefined- Default
0
classNamestring | function
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
data-disabled
Present when the field is disabled.
data-valid
Present when the field is valid.
data-invalid
Present when the field is invalid.
data-dirty
Present when the field's value has changed.
data-touched
Present when the field has been touched.
data-filled
Present when the field is filled.
data-focused
Present when the field control is focused.
Label
An accessible label that is automatically associated with the field control.
Renders a <label> element.
classNamestring | function
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
data-disabled
Present when the field is disabled.
data-valid
Present when the field is in valid state.
data-invalid
Present when the field is in invalid state.
data-dirty
Present when the field's value has changed.
data-touched
Present when the field has been touched.
data-filled
Present when the field is filled.
data-focused
Present when the field control is focused.
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
—
defaultValueUnion
—- Name
- Type
string | number | string[] | undefined
onValueChangefunction
—
onValueChangefunction
—- Name
- Description
Callback fired when the
valuechanges. Use when controlled.- Type
| (( value: string, eventDetails: Field.Control.ChangeEventDetails, ) => void) | undefined
classNamestring | function
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
data-disabled
Present when the field is disabled.
data-valid
Present when the field is in valid state.
data-invalid
Present when the field is in invalid state.
data-dirty
Present when the field's value has changed.
data-touched
Present when the field has been touched.
data-filled
Present when the field is filled.
data-focused
Present when the field control is focused.
Description
A paragraph with additional information about the field.
Renders a <p> element.
classNamestring | function
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
data-disabled
Present when the field is disabled.
data-valid
Present when the field is in valid state.
data-invalid
Present when the field is in invalid state.
data-dirty
Present when the field's value has changed.
data-touched
Present when the field has been touched.
data-filled
Present when the field is filled.
data-focused
Present when the field control is focused.
Item
Groups individual items in a checkbox group or radio group with a label and description.
Renders a <div> element.
disabledboolean
false
disabledboolean
false
- 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
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
Error
An error message displayed if the field control fails validation.
Renders a <div> element.
matchUnion
—
matchUnion
—- 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
—
classNamestring | 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)
renderReactElement | function
—
renderReactElement | 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)
data-disabled
Present when the field is disabled.
data-valid
Present when the field is in valid state.
data-invalid
Present when the field is in invalid state.
data-dirty
Present when the field's value has changed.
data-touched
Present when the field has been touched.
data-filled
Present when the field is filled.
data-focused
Present when the field control is focused.
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) => ReactNode)
—
children*((state: Field.Validity.State) => 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) => ReactNode