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>Use the React primitive adapter when Field state participates in React rendering.
import { Field } from "@starwind-ui/react/field";
export function Example() { return ( <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> );}Use the Vue components to build Field.
<script setup lang="ts">import Field from "@starwind-ui/vue/field";</script>
<template> <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></template>Use the Svelte components to build Field.
<script lang="ts"> import { Field } from "@starwind-ui/svelte/field";</script>
<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>Render the Field data-sw-* contract yourself, then initialize createField.
<div data-sw-field> <label data-sw-field-label>Email</label> <input data-sw-field-control data-sw-input /> <p data-sw-field-description>Use your work email.</p> <div data-sw-field-error hidden>Enter a valid email.</div></div>
<script type="module"> import { createField } from "@starwind-ui/runtime/field";
const root = document.querySelector("[data-sw-field]"); if (root) { createField(root); }</script>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
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.
dirtysetDirty
- 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.
touchedsetTouched
- 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
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.
dirtysetDirty
- 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.
touchedsetTouched
- Runtime / HTML
call setTouched for later updates.
setTouched
Data Attributes
Runtime hooks
State
Metadata
Label
Text label associated with Field.
- Default element
- label
- Discovery hook
- data-sw-field-label
- Role
- -
Data Attributes
Runtime hooks
Control
Groups the interactive controls for Field.
- Default element
- input
- Discovery hook
- data-sw-field-control
- Role
- -
Props
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
Metadata
Description
Supporting description text for Field.
- Default element
- p
- Discovery hook
- data-sw-field-description
- Role
- -
Data Attributes
Runtime hooks
Item
An interactive item inside the Field collection.
- Default element
- div
- Discovery hook
- data-sw-field-item
- Role
- -
Data Attributes
Runtime hooks
Error
Error message content for Field.
- Default element
- div
- Discovery hook
- data-sw-field-error
- Role
- -
Props
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
Metadata
Validity
Validation state container for Field.
- Default element
- div
- Discovery hook
- data-sw-field-validity
- Role
- -
Props
match FieldErrorMatch true
- Description
- Controls how Field filters matching items.
- Kind
- attribute
- Targets
- validity
- Full type
- FieldErrorMatch
Data Attributes
Runtime hooks
Metadata
Runtime API
- Factory
createField- Import
@starwind-ui/runtime/field- Root hook
- root
data-sw-field - Option props
- dirty, disabled, invalid, name, touched
Runtime Setters
Related Styled Components
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.