Form field
FormField groups a label, a control, and its description and error text, and shares that wiring with its children through a Vue injection context. Label reads the context automatically, so it gets the right for without you repeating an id. Every form control — Input, Textarea, Password, FileUpload, Select, Combobox, Checkbox, Switch, RadioGroup, InputNumber, SelectButton, DatePicker, TreeSelect, and TransferList — consumes the same context through the shared useFieldControl composable, so id, aria-describedby, and (where the element supports them) aria-invalid and required are wired the same way everywhere.
<script setup lang="ts">
import { ref } from 'vue'
import { FormField, Input } from '@myghf/ui'
const email = ref('')
</script>
<template>
<FormField label="Email" description="We never share your address." required>
<Input v-model="email" type="email" placeholder="[email protected]" />
</FormField>
</template>Every piece is optional: render a label, a control, a description, an error, or any combination. A field with no label renders no <label>; a field with no description or error renders no helper text.
Examples
Label, description, and error
Pass label, description, and error as props. Label receives the generated id for for; the description and error render below the control with ids matching aria-describedby. Setting error also marks the context invalid unless you override it with invalid.
As it appears on your record.
Enter a valid email address.
<script setup lang="ts">
import { ref } from 'vue'
import { FormField } from '@myghf/ui'
const name = ref('')
const email = ref('')
</script>
<template>
<div class="grid max-w-sm gap-6">
<FormField
id="fd-name"
label="Full name"
required
description="As it appears on your record."
>
<input
id="fd-name"
v-model="name"
type="text"
placeholder="Ada Lovelace"
class="h-9 w-full rounded-md border border-border bg-surface px-3 text-sm text-foreground placeholder:text-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500"
/>
</FormField>
<FormField id="fd-email" label="Email" error="Enter a valid email address.">
<input
id="fd-email"
v-model="email"
type="email"
placeholder="[email protected]"
aria-invalid="true"
aria-describedby="fd-email-error"
class="h-9 w-full rounded-md border border-error-500 bg-surface px-3 text-sm text-foreground placeholder:text-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-error-500"
/>
</FormField>
</div>
</template>Slots and standalone use
Each piece also has a slot (label, description, error), so you can render rich content instead of plain text. Label works on its own too — pass for directly or let a surrounding FormField supply it.
Shown on your public profile.
An organisation is required.
<script setup lang="ts">
import { FormField, Label } from '@myghf/ui'
</script>
<template>
<div class="grid max-w-sm gap-6">
<FormField id="fd-org" invalid>
<template #label>Organisation</template>
<input
id="fd-org"
type="text"
placeholder="MYGHF"
aria-invalid="true"
aria-describedby="fd-org-description fd-org-error"
class="h-9 w-full rounded-md border border-error-500 bg-surface px-3 text-sm text-foreground placeholder:text-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-error-500"
/>
<template #description>Shown on your public profile.</template>
<template #error>An organisation is required.</template>
</FormField>
<div class="grid gap-2">
<Label for="fd-plain">Standalone label</Label>
<input
id="fd-plain"
type="text"
placeholder="Works outside a FormField"
class="h-9 w-full rounded-md border border-border bg-surface px-3 text-sm text-foreground placeholder:text-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500"
/>
</div>
</div>
</template>FormField props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Label text. Can also be supplied through the label slot. |
description | string | — | Helper text rendered below the control. Can also be supplied through the description slot. |
error | string | — | Error text rendered as an alert below the control. Can also be supplied through the error slot. |
required | boolean | false | Marks the field required; adds the label's aria-hidden marker and sets the context flag. |
invalid | boolean | Boolean(error) | Overrides the invalid state provided to controls. |
id | string | generated | Base control id. When omitted, a stable id is generated with Vue's useId(). |
FormField slots
| Slot | Description |
|---|---|
default | The control (and any other content) for the field. |
label | Replaces the label text with custom content. |
description | Replaces the description text with custom content. |
error | Replaces the error text with custom content. The field is invalid whenever this slot has content. |
Label props
| Prop | Type | Default | Description |
|---|---|---|---|
for | string | field context id | The id of the control the label belongs to. An explicit for wins over the context. |
required | boolean | false | Renders an aria-hidden * marker after the label text. |
The default slot holds the label text (or markup).
FormDescription
FormDescription renders <p class="text-sm text-muted">. It renders nothing when its default slot has no content, and accepts an id (a FormField passes `${id}-description`). Use it standalone if you are not using FormField.
FormMessage
FormMessage renders <p role="alert" class="text-sm text-error-600">. It renders nothing when its default slot has no content, and accepts an id (a FormField passes `${id}-error`). The role="alert" makes the message announce when it appears.
useFormField
useFormField() returns the nearest FormField context, or null when there is no provider — it never throws. This is what lets Label (and the form controls) fall back to today's behaviour outside a FormField.
import { useFormField } from '@myghf/ui'
const field = useFormField()
if (field) {
field.id.value // the control id
field.describedBy.value // "id-description id-error", or undefined
field.invalid.value // boolean
field.required.value // boolean
}FormFieldContext is exported as a type:
interface FormFieldContext {
id: ComputedRef<string>
describedBy: ComputedRef<string | undefined>
invalid: ComputedRef<boolean>
required: ComputedRef<boolean>
}Because the context is built from computed refs, it stays reactive when the FormField props change after mount.
useFieldControl
useFieldControl is the shared resolver every form control uses to wire itself to the nearest FormField. Pass reactive readers for the control's own props; each reader falls back to the context (then a default) when it returns undefined, so an explicit prop always wins:
import { useFieldControl } from '@myghf/ui'
const { id, invalid, required, describedBy } = useFieldControl({
id: () => props.id,
invalid: () => props.invalid,
required: () => props.required,
})Build a custom control with the same contract: bind id, :aria-invalid="invalid || undefined", :aria-describedby="describedBy", and :required/:aria-required onto its focusable element. Pass readers (not plain values) so reactivity is kept, and so a prop named differently still works — DatePicker passes id: () => props.inputId.
id and describedBy are ComputedRef<string | undefined>; invalid and required are ComputedRef<boolean>.
Accessibility
Labelis a real<label>; itsforpoints at the control id from the field context (or the explicitforprop), so clicking the label focuses the control.FormFieldcomputesdescribedByas the space-joined ids of the description and error only when that content exists, so controls never pointaria-describedbyat a missing element.FormMessageusesrole="alert", so an error that appears after submission is announced without moving focus.- The required marker is
aria-hidden="true"— assistive technology hears the control's ownrequiredstate, not a stray asterisk. - Controls whose focusable element is a
<button>(DatePicker,TreeSelect) exposeidandaria-describedbybut notaria-invalid/aria-required, which are not valid on a button; they surface the error through the description association and the error border.TransferListexposes the same pair on arole="group"wrapper.
Dark mode & RTL
- The label, description, and error use semantic tokens (
text-foreground,text-muted,text-error-600), which adapt to dark mode automatically. - The layout is direction-safe: the wrapper is a
gridcolumn, the label usestext-start, and no fixed left/right offsets are used.
