Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Field for Vue UI SDK

Form field layout for Vue on UI SDK: a label, the control, a hint and an error message, stacked or side by side, plus fieldsets and groups.

On this page9 sections

Field lays out one form control with its label, a line of help and an error message, and its other parts arrange many fields into a form: FieldGroup spaces them, FieldSet gathers the ones that answer one question. It works around any control, from an input to a slider. It doesn't validate or hold values, so pair it with your form library.

For one caption beside one control with nothing else around them, a Label is enough.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Field anatomy: root, label, input, description, error

Usage

<script setup lang="ts">
import {
  Field, FieldDescription, FieldLabel,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";
</script>

Field links the label to the input by itself, so the label's for and the input's matching id below are optional on UI SDK.

<template>
  <Field class="max-w-xs">
    <FieldLabel for="name">Name</FieldLabel>
    <Input id="name" placeholder="Evil Rabbit" type="text" />
    <FieldDescription>This is your public display name.</FieldDescription>
  </Field>
</template>

Accessibility

Field is a div with role="group" and adds no keys of its own, so the control inside keeps its usual keyboard. FieldSet renders a <fieldset>, and a screen reader announces its FieldLegend as people move into the group. Field links FieldLabel to the UI SDK control inside and names FieldDescription and a showing FieldError in its aria-describedby, as the description answer details. invalid on Field gives the control aria-invalid, disabled disables it, and a FieldError that appears while focus stays put is read out. A plain <input>, <select> or <textarea> in the field is linked too: it takes the label's name unless it has one of its own, and the descriptions, a showing error and aria-invalid reach it.

Demos

Input

A password field with its rule, at least 8 characters, between the label and the box. Put FieldDescription before the control when people need the constraint before they type, and after it, as the preview at the top of the page does, for a note they can skip. The type is password, so the characters stay masked.

Preview width

Invalid

An email field holding jordan@example, marked invalid with invalid on Field, which turns the label and the box's border to the destructive color and passes aria-invalid to the input. The FieldError under it says what to fix. Set invalid from your form library's error for the field, and keep the message to the one change people need to make.

Preview width

Textarea

Feedback, as a label over a Textarea with no description, so the placeholder is the only prompt. That's enough for a free-form box whose label says it all. Anything people still need once they start typing, such as a length limit, belongs in a FieldDescription, because the placeholder disappears.

Preview width

Select

Department, picked from Engineering, Design or Marketing in a Select that shows a placeholder until something is chosen. Field links the label to the select. Use a select for a short closed list, and a text input for an answer people make up.

Preview width

Slider

Volume at 40, with the field's label above a Slider. The Slider also carries an aria-label of Volume, which is the name its thumb announces when it takes focus. Keep that name the same as the visible label, so what a screen reader says matches what people see.

Preview width

Fieldset

Street and city under one Address legend. FieldSet renders a <fieldset> and FieldLegend its <legend>, so a screen reader says Address as people move into the group, and each input keeps its own label. Use a fieldset when several inputs answer one question, such as an address or a date split into parts.

Preview width

Checkbox

Same as shipping address, checked from the start, with the box and its label in one row through orientation set to horizontal. font-normal keeps the label at body weight, since it reads as the choice rather than a heading. Field links the label to the checkbox, so clicking the text toggles it.

Preview width

Radio

A Subscription plan question with Monthly and Yearly, one item to each horizontal Field, in a FieldSet whose FieldLegend has variant set to label for smaller text. Each item's text sits inside the item and is its own label, and the legend names the fieldset around the group. Use radios when exactly one answer is allowed.

Preview width

Switch

Multi-factor authentication, with the label first and the switch after it in one row, the usual shape for a settings list. Field links the label to the switch. When turning it on has a consequence, put a FieldDescription under the label, with both inside a FieldContent.

Preview width

Choice card

Monthly and Yearly as two bordered cards, where a click anywhere on a card selects its radio. Each FieldLabel wraps a horizontal Field, which the label's styles turn into a card with a border and a tint when checked. FieldTitle gives each card a title styled like a label, since the card is already the label. Keep it to a few options that need room for a price.

Preview width

Group

Email and Name in one FieldGroup, with a FieldSeparator line between them. FieldGroup gives every field in it the same gap, and the separator marks a break between parts of a form that don't share a legend. Pass the separator a short word as its child, such as or, and it prints it in the middle of the line.

Preview width

RTL

The public name field in Arabic, inside a wrapper with dir set to rtl. The label, the box and the description start from the right edge, since the field's alignment is logical, with text-start on the description. Check a translated form this way before it ships, because a physical margin you added yourself lands on the wrong side.

Preview width

Responsive

Name, with its label and a description grouped in FieldContent, then the input. orientation set to responsive stacks the two, then puts them side by side once the FieldGroup around the field is 28rem wide, through a container query on the group rather than the viewport. The demo's group is capped at max-w-lg, 32rem, so it shows a row on a wide screen and stacks on a phone.

Preview width

FAQ

How do I label a Field in Vue?

Put a FieldLabel and the control inside Field, and Field links them: the label points at the control's id, giving it one when it has none, and the control takes its name from the label. That works for the UI SDK controls here, such as Input, Select, Checkbox and Switch, and a plain <input>, <select> or <textarea> with no name of its own takes the label's name too, so the label's for and the control's id are optional.

FieldTitle looks like a label without being one. Use it for a heading inside a choice card, where the card itself is the label.

How do I put a Field label beside the control?

Set orientation on Field. vertical, the default, stacks the label over the control, horizontal puts them in a row, and responsive stacks them until the FieldGroup around the field is 28rem wide, then uses a row. responsive only switches inside a FieldGroup, because the breakpoint is a container query on that group.

In a row, wrap the label and its description in FieldContent so they stay together beside the control. Set reverse to swap the label and the control on screen, which leaves the reading order of the markup as it is.

How do I show an error message on a Field?

Set invalid on Field and put the message in a FieldError, as the Invalid demo does. It takes your text as children or a form library's errors array, showing one message as text, several as a list, and a repeated message once. While it shows, the control names it in its aria-describedby, and one that appears while focus stays put is read out.

Inside a Field, FieldError shows only while the field is invalid, so it can stay in your markup. Leave invalid off and it shows once a submit finds the field invalid, with the browser's own message when it has no text of its own, such as for a required input left blank.

How do I use Field with a form library in Vue?

Field holds no form state. Wire the library to the control as usual, set invalid on Field from the library's error for that field, and show the message in FieldError. Keep a FieldDescription for the hint and FieldError for the failure, since people need both while they fix it.

FieldError takes the library's error objects directly through errors, so a field with several failed rules lists each one.

How do I disable a whole Field or a group of fields?

Set disabled on Field, which disables the UI SDK control inside and fades the label. On FieldSet, disabled disables every control in the set and marks each field in it disabled, since the set is a real <fieldset>.

A disabled control's value is left out when the form submits. Make a value people can't change read-only instead when it still has to be sent.

When should I use FieldSet, FieldGroup or FieldSeparator?

Use FieldSet with a FieldLegend when several controls answer one question, such as an address or a choice between plans, because the legend names the whole group for a screen reader. Use FieldGroup to stack any fields with an even gap, with or without a legend.

FieldSeparator draws a line between two parts of a FieldGroup, as the Group demo does. Give it a short word as its child to print in the middle of the line. A FieldGroup inside another one takes a tighter gap.

How do I make a choice card with Field?

Wrap a horizontal Field in a FieldLabel, put a FieldTitle and the radio or checkbox inside, and repeat it for each option, as the Choice card demo does. The label's styles draw the border and tint the card when its control is checked, and a click anywhere on the card selects it. Inside a FieldLabel, Field drops its group role, so the card's text names the radio for a screen reader.

Use FieldTitle rather than another FieldLabel for the card's title, since the card is already the label and a second one would sit inside it. Put a FieldDescription under the title inside a FieldContent for a price or a line of detail.

Does a screen reader read a FieldDescription with its control?

Yes, for a UI SDK control and for a plain <input>, <select> or <textarea>. Field gives each FieldDescription inside it an id and names it in each control's aria-describedby, after any ids the control lists of its own, so the hint is read when the control takes focus. A FieldDescription directly inside a FieldSet, outside any Field, describes the whole set instead.

While it shows, FieldError is named in the control's aria-describedby too, so the error is read along with the hint. One that appears while focus stays in place is also read out on its own.

API Reference

Field

<div> data-slot="field"

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables the control, and marks every part.

Default
(markedDisabled || undefined)
invalid?boolean

Marks the field invalid, as a form library reports it: the field turns destructive and its control gets aria-invalid. Leave it out (or false) and the browser's own validation shows after a submit.

Default
"true"`.
name?string

The name the control submits under, when the control doesn't set its own.

orientation?"horizontal" | "responsive" | "vertical"

Stacks the label over the control (vertical), puts them side by side (horizontal), or side by side on wide containers only (responsive). The widths skip a popup rendered in place ([popover]), which sizes itself.

Type
"horizontal" | "responsive" | "vertical"
Default
"vertical"
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field is disabled.

data-invalid?string

Present while the field shows as invalid: with invalid, or once a submit finds the control invalid.

FieldSet

<fieldset> data-slot="field-set"

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables every control inside it, except those in its legend, and leaves them out of the form.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field set is disabled, by its own prop or a <fieldset> around it.

FieldLegend

<legend> data-slot="field-legend"

PropType
variant?"legend" | "label"

The visual style.

Type
"legend" | "label"
Default
"legend"
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field set is disabled, by its own prop or a <fieldset> around it.

FieldGroup

<div> data-slot="field-group"

PropType
class?string

Merged with the part's classes.

AttributeType
data-invalid"true"
Default
"true"

FieldContent

<div> data-slot="field-content"

PropType
class?string

Merged with the part's classes.

FieldLabel

<label> data-slot="field-label"

PropType
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field is disabled.

data-invalid?string

Present while the field shows as invalid: with invalid, or once a submit finds the control invalid.

FieldTitle

<div> data-slot="field-label"

PropType
class?string

Merged with the part's classes.

FieldDescription

<p> data-slot="field-description"

PropType
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field is disabled.

data-invalid?string

Present while the field shows as invalid: with invalid, or once a submit finds the control invalid.

FieldSeparator

<div> data-slot="field-separator"

PropType
class?string

Merged with the part's classes.

AttributeType
data-content?string

FieldError

<ul> data-slot="field-error"

PropType
errors?Array<{ message?: string }>

Errors to show, such as a form library's ({ message } each): one as * text, several as a list, repeats once.

Type
Array<{ message?: string }>
match?boolean | ValidityKey

When this error shows. true shows it always and false never. A validity flag, like "valueMissing", shows it only for that problem of the browser's own validation. Without it, any problem shows it.

Type
boolean | ValidityKey
Default
(content ? true : undefined)
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when the field is disabled.

data-invalid?string

Present while the field shows as invalid: with invalid, or once a submit finds the control invalid.