Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Field Primitive

Use Field to give an input a label, a description, and a place to show errors. It connects those parts to the control and tracks when to validate it.

We use this address to send your receipt.

Anatomy

Use the Astro primitive adapter to render Field anatomy with the Runtime wiring included.

---
import { Field } from "@starwind-ui/astro/field";
---
<Field.Root>
<Field.Label>Email</Field.Label>
<Field.Control />
<Field.Description>Use your work email.</Field.Description>
<Field.Error>Enter a valid email.</Field.Error>
</Field.Root>

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

Validation policy values

The timing props accept semantic change, blur, submit, or manual causes. Field values override the owning Form; otherwise they inherit its submit/change/submit defaults and post-submit replacement policy.

API Reference

Root

The main element that owns the Field Runtime instance.

Default element
div
Discovery hook
data-sw-field
Role
-

Props

Prop Type Default Toggle
dirty boolean -
Description
Marks whether the field value has changed.
Kind
control
Targets
-
Full type
boolean
React

Use dirty for controlled state.

  • dirty
Astro

Use dirty for initial state and call setDirty for later updates.

  • dirty
  • setDirty
Runtime / HTML

call setDirty for later updates.

  • setDirty
disabled boolean false
Description
Disables the Root part.
Kind
option
Targets
-
Full type
boolean
data-error-visibility "blur" | "change" | "manual" | "submit" -
Description
Low-level errorVisibility form for semantic change, blur, submit, or manual validation; overrides the owning Form for this Field.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"
data-revalidation-timing "blur" | "change" | "manual" | "submit" -
Description
Low-level revalidationTiming form that replaces validationTiming after a submission attempt; overrides the owning Form for this Field.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"
data-validation-timing "blur" | "change" | "manual" | "submit" -
Description
Low-level validationTiming form for semantic change, blur, submit, or manual validation before submission; overrides the owning Form for this Field.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"
errorVisibility "blur" | "change" | "manual" | "submit" submit
Description
Selects whether semantic change, blur, submit, or manual validation reveals this Field's errors; inherits the owning Form policy when omitted.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"
invalid boolean -
Description
Marks the field as invalid for validation styling and state.
Kind
control
Targets
-
Full type
boolean
name string -
Description
Sets the submitted form field name.
Kind
option
Targets
-
Full type
string
revalidationTiming "blur" | "change" | "manual" | "submit" change
Description
After an owning Form submission attempt, replaces validationTiming with semantic change, blur, submit, or manual validation; inherits the owning Form policy when omitted.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"
touched boolean -
Description
Marks whether the field has been visited.
Kind
control
Targets
-
Full type
boolean
React

Use touched for controlled state.

  • touched
Astro

Use touched for initial state and call setTouched for later updates.

  • touched
  • setTouched
Runtime / HTML

call setTouched for later updates.

  • setTouched
validationTiming "blur" | "change" | "manual" | "submit" submit
Description
Selects semantic change, blur, submit, or manual validation before an owning Form submission attempt; inherits the owning Form policy when omitted.
Kind
option
Targets
root
Full type
"blur" | "change" | "manual" | "submit"

State

State Type Props Toggle
dirty boolean dirty
Description
Tracks whether the Field value has changed.
Initial attribute
-
Runtime getter
-
Runtime setter
setDirty
React

Use dirty for controlled state.

  • dirty
Astro

Use dirty for initial state and call setDirty for later updates.

  • dirty
  • setDirty
Runtime / HTML

call setDirty for later updates.

  • setDirty
touched boolean touched
Description
Tracks whether Field has been visited.
Initial attribute
-
Runtime getter
-
Runtime setter
setTouched
React

Use touched for controlled state.

  • touched
Astro

Use touched for initial state and call setTouched for later updates.

  • touched
  • setTouched
Runtime / HTML

call setTouched for later updates.

  • setTouched

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-dirty - Reflects the dirty state on the Root part.
data-invalid - Reflects the invalid state on the Root part.
data-touched - Reflects the touched state on the Root part.
Metadata
Attribute Value Description
data-disabled - Reflects the disabled prop on the Root part.
data-error-visibility - Reflects the error visibility prop on the Root part.
data-name - Reflects the name prop on the Root part.
data-revalidation-timing - Reflects the revalidation timing prop on the Root part.
data-validation-timing - Reflects the validation timing prop on the Root part.

Label

Text label associated with Field.

Default element
label
Discovery hook
data-sw-field-label
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-label - Marks the Label part so Starwind Runtime can find it.

Control

Groups the interactive controls for Field.

Default element
input
Discovery hook
data-sw-field-control
Role
-

Props

Prop Type Default Toggle
value InputValue -
Description
Controls the current Field value.
Kind
control
Targets
control
Full type
InputValue
defaultValue InputValue -
Description
Sets the initial Field value for uncontrolled usage.
Kind
control
Targets
control
Full type
InputValue
onValueChange InputValueChangeDetails -
Description
Runs when the Field value changes.
Kind
callback
Targets
control
Full type
InputValueChangeDetails

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-control - Marks the Control part so Starwind Runtime can find it.
Metadata
Attribute Value Description
data-sw-input - Identifies Control metadata for styling and selectors.

Description

Supporting description text for Field.

Default element
p
Discovery hook
data-sw-field-description
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-description - Marks the Description part so Starwind Runtime can find it.

Item

An interactive item inside the Field collection.

Default element
div
Discovery hook
data-sw-field-item
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-item - Marks the Item part so Starwind Runtime can find it.

Error

Error message content for Field.

Default element
div
Discovery hook
data-sw-field-error
Role
-

Props

Prop Type Default Toggle
match FieldErrorMatch false
Description
Controls how Field filters matching items.
Kind
attribute
Targets
error
Full type
FieldErrorMatch
messageSource "children" | "validation" -
Description
Sets the message source attribute on the Error part.
Kind
attribute
Targets
error
Full type
"children" | "validation"

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-error - Marks the Error part so Starwind Runtime can find it.
Metadata
Attribute Value Description
data-match - Reflects the match prop on the Error part.
data-message-source - Reflects the message source prop on the Error part.

Validity

Validation state container for Field.

Default element
div
Discovery hook
data-sw-field-validity
Role
-

Props

Prop Type Default Toggle
match FieldErrorMatch true
Description
Controls how Field filters matching items.
Kind
attribute
Targets
validity
Full type
FieldErrorMatch

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-field-validity - Marks the Validity part so Starwind Runtime can find it.
Metadata
Attribute Value Description
data-match - Reflects the match prop on the Validity part.

Runtime API

Factory
createField
Import
@starwind-ui/runtime/field
Root hook
root data-sw-field
Option props
dirty, disabled, invalid, name, touched

Runtime Setters

Method Target Description
setDirty prop: dirty Updates whether Field is dirty from Runtime code.
setDisabled prop: disabled Updates whether Field is disabled from Runtime code.
setInvalid prop: invalid Updates whether Field is invalid from Runtime code.
setName prop: name Updates the Field form field name from Runtime code.
setTouched prop: touched Updates whether Field is touched from Runtime code.
Component Relationship
Field Composite

Changelog

View version history v1.1.1 8 releases

v1.1.1

  • In Svelte, `Field.Control` no longer clears the text that the user typed when the parent changes another prop, such as `class` or `disabled`. This happened when it had no `value` prop or used `bind:value`.
  • In Svelte, Field parts no longer print a default `data-slot`, as in the other frameworks. If your selectors use it, pass `data-slot` to the part.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Astro, an attribute that you set on a Field part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • A `name` on a control now wins over the `name` on its `Field`, in all frameworks. If you set different names on a `Field` and its control, and your server reads the `Field` name, remove the `name` from the control.
  • In React, Select and Combobox inside a `Field` with a `name` now send their value when the form submits, and they call `onValueChange`, `onOpenChange`, and `onInputValueChange`. Before, the form often left the value out, and the callbacks did not run.
  • Inside a `Field` in Astro, Checkbox and Switch now clear when the form resets. Before, they stayed checked.
  • In Svelte, a defined `value` on `Field.Control` now stays under your control, as it does on Input.
  • Field no longer loads Color Picker code unless the Field holds a Color Picker. If your page calls `createField` and imports no Runtime code for a control inside that Field, Field now loads the code for all ten controls it can hold, about 47 KB gzipped in total. To skip that, import the control's own entry, such as `@starwind-ui/runtime/select`.

v1.0.3

  • When the first Field with an error holds a Select or Radio Group, Form submit and `validate({ focus: true })` now put focus on the Select trigger or on a radio. A Radio Group puts focus on its checked radio when one is checked. Before, focus did not go to any control that the user could see.
  • In Svelte apps that render on the server, a form reset now puts a Field control that uses Input back to its default value. Before, a reset after the page loaded cleared it.

v1.0.2

  • Installing the Field Primitive through the CLI now includes its required Input files.
  • Form controls now load Field's automatic setup code only when Field is used. Labels, validation, and native form behavior remain supported.
  • Reduced repeated JavaScript when Astro Field is used with other Starwind components.

v1.0.1

  • Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.1

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.

v0.1.0

  • First release. Field connects labels, descriptions, validation state, and messages to its control, so screen readers can read them.