Field

A component that provides labeling and validation for form controls.

Base UI React 1.8.0

Visible on your profile

FineSoft Stencil
Visible on your profile

The Stencil example uses fs-field-root, fs-field-label, fs-field-control, fs-field-error, and fs-field-description with the same required-input validation. The migrated Form supplies inherited validation mode, consolidated values, form-level errors, submit validation, and first-invalid-control focus.

The native input is shared by fs-field-control and fs-input. Existing Stencil Checkbox, Checkbox Group, Radio, Switch, Slider, Number Field and OTP Field also inherit Field state, labels, descriptions and validation. Use inputRef for the actual native Field Control input. actionsRef.current.validate() (or the Root element’s validateField() method) triggers validation. fs-field-validity exposes its snapshot through validity / getValidity() and the validityChange event instead of React’s function child; in React, listen with onvalidityChange and read event.detail. Error mounts only while its condition matches, retaining the last message until its exit animation finishes.

Root, Item, and Description use Shadow DOM and expose ::part(field), ::part(item), and ::part(description). The Base UI theme is built in; set data-fs-theme="base-ui" on an ancestor to apply the default appearance without component classes. CSS variables and these parts support custom styling. Control and Label retain native input and label elements in Light DOM for form ownership and label activation. Error keeps its existing content and animation boundary; Validity remains a transparent state API.

Anatomy

Import the component and assemble its parts:

Anatomy

API reference

Root

Groups all parts of the field. Renders a <div> element.

namestring
Name
Description

Identifies the field when a form is submitted. Takes precedence over the name prop on the <Field.Control> component.

Type
actionsRefReact.RefObject<Field.Root.Actions | null>
Description

A ref to imperative actions.

  • validate: Validates the field when called.
Type
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
touchedboolean
Description

Whether the field has been touched. Useful when the field state is controlled by an external library.

Type
disabledbooleanfalse
Description

Whether the component should ignore user interaction. Takes precedence over the disabled prop on the <Field.Control> component.

Type
Default
false
invalidboolean
Description

Whether the field is invalid. Useful when the field state is controlled by an external library.

Type
validatefunction
Description

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

Type
validationModeForm.ValidationMode'onSubmit'
Description

Determines when the field should be validated. This takes precedence over the validationMode prop 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
Default
'onSubmit'
validationDebounceTimenumber0
Description

How long to wait between validate callbacks if validationMode="onChange" is used. Specified in milliseconds.

Type
Default
0
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

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

Attribute
Description
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.

Field.Root.PropsHide

Re-Export of Root props as FieldRootProps

Field.Root.StateHide

Field.Root.ActionsHide

Label

An accessible label that is automatically associated with the field control. Renders a <label> element.

nativeLabelbooleantrue
Description

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

This is useful to avoid inheriting label behaviors on <button> controls (such as <Select.Trigger> and <Combobox.Trigger>), including avoiding :hover on the button when hovering the label, and preventing clicks on the label from firing on the button.

Type
Default
true
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Attribute
Description
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Field.Label.PropsHide

Re-Export of Label props as FieldLabelProps

Field.Label.StateHide

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
Type
onValueChangefunction
Description

Callback fired when the value changes. Use when controlled.

Type
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Attribute
Description
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Field.Control.PropsHide

Re-Export of Control props as FieldControlProps

Field.Control.StateHide

Field.Control.ChangeEventReasonHide

Field.Control.ChangeEventDetailsHide

Description

A paragraph with additional information about the field. Renders a <p> element.

classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Attribute
Description
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Field.Description.PropsHide

Re-Export of Description props as FieldDescriptionProps

Field.Description.StateHide

Item

Groups individual items in a checkbox group or radio group with a label and description. Renders a <div> element.

disabledbooleanfalse
Description

Whether the wrapped control should ignore user interaction. The disabled prop on <Field.Root> takes precedence over this.

Type
Default
false
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Attribute
Description
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

Field.Item.PropsHide

Re-Export of Item props as FieldItemProps

Field.Item.StateHide

Error

An error message displayed if the field control fails validation. Renders a <div> element.

matchUnion
Name
Description

Determines whether to show the error message according to the field’s ValidityState. Specifying true will always show the error message, and lets external libraries control the visibility.

Type
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
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 ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

data-starting-style

Present when the error message begins animating in.

data-ending-style

Present when the error message is animating out.

Attribute
Description
data-disabled

Present when the field is disabled.

data-valid

Present when the field is in a valid state.

data-invalid

Present when the field is in an 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.

data-starting-style

Present when the error message begins animating in.

data-ending-style

Present when the error message is animating out.

Field.Error.PropsHide

Re-Export of Error props as FieldErrorProps

Field.Error.StateHide

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
Description

A function that accepts the field validity state as an argument.

Type

Field.Validity.PropsHide

Re-Export of Validity props as FieldValidityProps

Field.Validity.StateHide