Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Field

One form question: it joins a control to its visible label, an optional description, a help text and an error, so people hear them when the control gets focus. It holds no form state and never validates: you decide when a field is invalid.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a Field for every control that asks one question, in a resident form or a staff tool, so the control is never without a visible label.
  • Put what people must read before answering in the description, and a format, an example or a limit in the help text under the control.
  • Set required and invalid from your own form logic. Field only shows the state: the validation is yours.
  • On a form with several fields, add an ErrorSummary that lists the errors with a link to each field.
  • Not for one question answered with several controls, such as a date or a group of options: use a Fieldset with a legend.
  • Not for text that belongs to no control: use Prose.

Example#

Warning: Don't forget

Tell the browser what each field asks for with autoComplete, as the example does for the name. The browser can then fill in the person’s own details, which helps people who find typing hard (1.3.5 Identify Input Purpose). Never block pasting into a field (3.3.8 Accessible Authentication).

Use cases#

Say which questions are optional

Mark required fields required, and the label of every other field ends with “(optional)”. Required fields carry no visible marker. Where the optional text is wrong, such as a lone search field, turn it off.

Props in this example:

Information: Did you know?

Field sets aria-required, not native required, so the browser shows no validation bubbles in its own words. For native validation, pass required on the control too, and keep it on the Field so the label doesn’t say “(optional)”.

Explain the question and show the format

A description is what people must read before answering, so it goes above the control and may hold a few short paragraphs. A help text is a short instruction, example or limit, so it goes under the control. Both are linked to the control, so a screen reader can read them when the control gets focus, in the order they appear.

We send the decision to this address, so check it before you send.

For example name@example.com

Information: Implement like this

Keep a help text to plain text of one or two sentences: no link, list or heading. A screen reader reads the description and the help text as flat text, so anything with structure goes outside the Field.

Show an error after the form is sent

Validate when the form is sent, not on every key. Set invalid, render the error message and move focus to the field, so the error is read with it. Say what is wrong and how to fix it, and repeat the format.

For example name@example.com

Props in this example:

Warning: Don't forget

Field never moves focus. When the form is sent, move it to the first invalid field or to an ErrorSummary. See What you need to do.

A field that can’t be changed

A disabled field turns off its control and marks every part as disabled. A disabled control can’t take keyboard focus, so avoid it in a form for residents. Where you can’t, say why in the help text. A read-only control stays reachable with the keyboard, so use that when people only need to read the value.

You can’t change this here. Contact your case worker to correct it.

Props in this example:

Wire your own elements

For a control KvirnUI doesn’t have, or markup of your own, useField gives the ids, aria-describedby and state attributes to spread. Name the descriptions from the first render, so server-rendered markup is complete.

We only use it to tell you about your case.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: none. There is no APG pattern for form fields. A field is native HTML: <label for>, a native control, and aria-describedby for the description, the help text and the error. Errors go through aria-describedby, not aria-errormessage.
  • Deviations: none from APG. A Prose in a Field is the description (there is no Field.Description), and a help text is its own part, Field.HelpText. Errors are not live, and the optional marker and the error prefix are part of the accessible text.
  • Native elements used: <div> (Field.Root), <label for> (Field.Label), <p> (Field.ErrorMessage), a <div class="kv-prose"> for the description (Field.Prose), a <p class="kv-field-help-text"> for the help text (Field.HelpText), a <span> for the optional marker and one for the error prefix, and a decorative <svg> (the error Icon) in the Field.ErrorMessage.

A Field joins one control to its visible label, an optional description (Field.Prose), an optional help text (Field.HelpText) and an error message. The parts register with their Field, so no DOM query and no global id is involved, and the control's aria-describedby never names a part that isn't rendered. The consumer validates and sets invalid: there is no validation engine.

PartElement / roleARIA / stateNotes
Field.Root<div>, no rolenone. data-invalid, data-required, data-disabledClass kv-field. Props: invalid, required, disabled, controlId, messages. Generates the ids (useId). Inside a group fieldset (Fieldset group) the marker defaults to none
Field.Label<label for>, names the controlhtmlFor is the control's id. id is <control id>-label. data-invalid, data-required, data-disabledClass kv-field-label. Appends <span class="kv-field-optional">(optional)</span> from field.optional after a normal space, inside the label, unless the field is required or marker="none". So the marker is in the name
Field.Prose<div class="kv-prose" id>In the control's aria-describedby, in DOM order, before the error. data-invalid, data-disabledThe description: what to answer, why we ask, where to find it. Read before answering, so it goes above the control. the body text size wherever it sits. A Field.Prose anywhere inside a Field registers itself: no prop and no extra part. A Field can have several, each with its own id. An id is listed only while its Prose is rendered. There is no Field.Description
Field.HelpText<p class="kv-field-help-text" id>. No roleIn the control's aria-describedby, in DOM order (with the Proses), before the error. data-invalid, data-disabled. Never tabindex, role or aria-liveThe help text: a short instruction, format example or limit, plain text, always under the control. a step smaller than the body text. A help text rendered before its control gives the dev warning help-text-before-control once; a Fieldset.HelpText before the group's controls gives the same warning. Registers through the same useDescriptionPart as Prose and FileUpload.Limits, so it has its own id and is listed only while rendered. It is outside the <label>, so it is never part of the name (2.5.3 Label in Name (A)). In an option's Field it describes that option's input, not the group. Outside a Field or Fieldset it warns (help-text-outside-field) and renders a plain <p class="kv-field-help-text"> with no id. Fieldset.HelpText, CheckboxGroup.HelpText and RadioGroup.HelpText are the same part under the group's name
Field.ErrorMessage<p id>, rendered only while the field is invalidIn the control's aria-describedby, after every description. data-invalid, and data-disabled while the field is disabledClass kv-field-error-message. Starts with a decorative error Icon (aria-hidden), then <span class="kv-field-error-prefix">Error:</span> from field.errorPrefix, a space, then the message
the controlTextInput (see the TextInput contract) or your elementid, aria-describedby (description id, then error id), aria-invalid="true" when invalid, aria-required="true" when requiredNot native required: no browser bubbles. Native disabled when the Field is disabled. data-invalid, data-required, data-disabled
useFieldthe same attributes, for your own elementsrootProps, labelProps, descriptionProps, getDescriptionProps(name), errorMessageProps, controlPropsOptions: id, invalid, required, disabled, hasDescription, descriptions (names, in render order), hasErrorMessage (default: invalid), marker, messages. Also returns labelId (the label's id, for aria-labelledby), optionalMarker and errorPrefix

Registration rules:

  • aria-describedby is every description's id (Prose and HelpText alike) in DOM order, then the error id, joined by a space, and only for the parts rendered. The order is the DOM order, whichever order the parts mounted in. With the default order that is the description, the help text, then the error. When none is rendered the attribute is absent, never empty. A screen-reader user hears the description, the help text, then "Error: …" in English, whatever the visual order.
  • A Field.HelpText registers like a Field.Prose, takes data-invalid and data-disabled from the Field and nothing else from its state (the theme never styles them), is never focusable and is no live region. Outside a Field or Fieldset it gives the dev warning help-text-outside-field once and renders a <p class="kv-field-help-text"> with no id. A help text is plain text: no link, list or heading in it. There is no dev warning for rich content.
  • A Field.Prose inside a Field is the description of its control. Registration is automatic and has no opt-out: a Prose that isn't a description goes outside the Field.
  • The description is the Prose's text content. A heading, list or link inside it is read as plain text, without its role or structure, and a link in a description can't be followed from there. Keep a description to plain text and a few short paragraphs, and put anything with structure outside the Field. A help text is plain text only.
  • Each description and help text has its own id (a generated suffix on the control's id), so no two share one. A Prose or HelpText that unmounts leaves aria-describedby at once: it never names a missing id.
  • A Prose outside a Field or Fieldset has no id and gives no warning, unlike a HelpText.
  • A Field with two ErrorMessages gives a dev warning (they share one id): render one, with all the text.
  • Field.ErrorMessage renders nothing while the Field isn't invalid, so a hidden or stale message is never referenced. A Field that is invalid without a Field.ErrorMessage gives a dev warning (3.3.1 Error Identification (A)).
  • A Prose, a HelpText and the Field.ErrorMessage attach to the nearest Field or Fieldset. A Field inside a Fieldset (an option, a date box) therefore owns its own help text and error, and the Fieldset's own Prose, HelpText and Field.ErrorMessage stay the group's.
  • A Fieldset's invalid never passes down to the Fields inside it.
  • useField({ hasDescription: true }) or useField({ descriptions: ['above', 'under'] }) sets aria-describedby from the first render, so server-rendered markup is complete. With descriptions, spread getDescriptionProps(name) on each description or help text: its class is kv-prose (for a help text, set className="kv-field-help-text" after the spread), its id is <controlId>-description-<name>, listed in the order of the array, then the error. The Root component learns about its parts when they mount, so for a moment before hydration the server HTML has the parts but not the association. Use the hook for server-critical markup.

It announces nothing.

What you need to do#

  • Keep a help text short and plain: one or two short sentences or a format example, text only, with no link, list or heading (see above). Anything the user must read before answering is a description (Field.Prose). A Prose that isn't a description, such as an article or a list of links, goes outside the Field. A help text is never the only place a format lives: the error repeats it. Its copy in a disabled or read-only field says why.
  • Give every control a visible label (Field.Label) that describes what to enter (2.4.6 Headings and Labels (AA)). A placeholder is not a label (3.3.2 Labels or Instructions (A)). Put examples in a Field.HelpText, not in a placeholder.
  • Set invalid and render a Field.ErrorMessage together. Write the message so it says what's wrong and how to fix it, in the field's own words, never blaming the user (3.3.1 Error Identification (A), 3.3.3 Error Suggestion (AA)). Plain text only: the icon, the prefix and the text lay out as one line.
  • Set required on every required field. Required fields carry no visible marker, and optional ones say "(optional)". Use marker="none" where that text is wrong, such as a lone search field or a single consent checkbox. If you mark required fields yourself with an asterisk, add your own text that explains it, and use marker="none".
  • Keep a Field's control a direct child of Field.Root when it is a checkbox or radio: the default theme finds its layout from .kv-field:has(> .kv-checkbox, > .kv-radio).
  • Validate on submit, not on every key. Then move focus to the first invalid field, or show an ErrorSummary (3.3.1 Error Identification (A)).
  • Render the parts in the default order: label, description, control, help text, then the error, so the visual order is the aria-describedby order (1.3.2 Meaningful Sequence (A)). A help text always goes under the control: text the user must read before answering is a description (Field.Prose) above it. With the error under the control, keep scroll-padding on the page (the end value leaves room for the message), so the on-screen keyboard or an autocomplete list doesn't hide it when the field gets focus (2.4.11 Focus Not Obscured (Minimum) (AA)). The order is the consumer's: another order keeps working.
  • Use autocomplete on controls that ask for the user's own data (1.3.5 Identify Input Purpose (AA)), and never block paste (3.3.8 Accessible Authentication (Minimum) (AA)).
  • A Field.Label whose text is in another language goes in a nested KvirnProvider with useLocale().localeProps on your own element (3.1.2 Language of Parts (AA)).
  • Give a Field the controlId you need to link to it, for example from an error summary. Don't pass an id to the control inside a Field: the Field's id wins and a dev warning says so.

Warning: Don't forget

Give every control a visible label. A placeholder is not a label: it is gone once people type (3.3.2 Labels or Instructions).

Visual and modes#

  • Focus indicator: on the control (see the TextInput contract). Labels and text parts aren't focusable. An invalid text input keeps its error-coloured edge when it has focus, and shows the focus ring on any focus, a click included.
  • Target size: the label is the click target for choice controls, taller than the 24 by 24 CSS pixel minimum in both densities, and sits above a text control (2.5.8 Target Size (Minimum) (AA)).
  • Colour: the label, description, help text and optional marker use the normal text colour, and a help text keeps it when the field is invalid, disabled or read-only (the error carries the state). The error text has at least 4.5:1 contrast on every background (7:1 in the high-contrast themes), checked by the theme's contrast check. The error has an icon, and a text prefix for screen readers, and the invalid control has a thicker edge, so colour is never the only cue (1.4.1 Use of Color (A)). The invalid edge has at least 3:1 contrast against the page and card backgrounds (1.4.11 Non-text Contrast (AA)).
  • forced-colors behaviour: the error text uses the system's text colour, the icon follows it, and the invalid control keeps its thicker border.
  • reduced-motion behaviour: no layout animation when an error appears. Not checked by an automated sweep.
  • 1.4.12 Text Spacing (AA): no fixed heights. A help text's size and line height are in rem, so it grows with the browser's text size (1.4.4 Resize Text (AA)).
  • Help text size: a help text is a step smaller than a description in both densities. The size belongs to the part, not to where it sits.
  • RTL: logical properties only. The icon doesn't mirror.

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • On error: Field never moves focus. On submit, the form moves focus to the first invalid field, or to an ErrorSummary. The consumer does this, and the docs say so.
  • Never obscured by: Field renders no overlay. Consumers with a sticky header set scroll-padding (2.4.11 Focus Not Obscured (Minimum) (AA)).

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): native <label for>, description and error in the accessible description (field.test.tsx › wiring: name and description per state).
  • 2.4.6 Headings and Labels (AA): the label describes the purpose of the control (consumer's text).
  • 1.3.5 Identify Input Purpose (AA): the consumer's autocomplete passes through (see the TextInput contract).
  • 1.4.1 Use of Color (A): invalid is a thicker edge, an icon and a message that starts with a text prefix.
  • 1.4.3 Contrast (Minimum) (AA), 1.4.11 Non-text Contrast (AA): the pairs in the theme's contrast check (the error colour as text and as a 3:1 edge).
  • 2.5.3 Label in Name (A): the visible label text, including "(optional)", is the name.
  • 3.3.1 Error Identification (A): the message in text, with a text prefix, linked by aria-describedby.
  • 1.3.2 Meaningful Sequence (A): with the default order the visual order is the spoken order: description, help text, then the error.
  • 3.3.2 Labels or Instructions (A): a visible label always, and the description and help text linked to the control.
  • 3.3.3 Error Suggestion (AA): the consumer's message text (the docs show the patterns).
  • 4.1.2 Name, Role, Value (A): the control's name, description, aria-invalid and aria-required.
  • 2.4.7 Focus Visible (AA): the control shows the focus ring on any focus, an invalid one included (Visual / modes; the control's own tests are in the TextInput contract).
  • 2.5.8 Target Size (Minimum) (AA): the label is taller than 24 by 24 CSS pixels (Visual / modes; reviewed in the stories, no named test).
  • 1.4.4 Resize Text (AA): the help text is in rem (Visual / modes; no named test).
  • 3.1.2 Language of Parts (AA): a label in another language is marked by the consumer (see Consumer responsibilities).

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none

Field.Root, Field.Label, the description (Field.Prose), the help text (Field.HelpText) and Field.ErrorMessage handle no keys and move no focus. Focus stays on the control, which owns its keys: see the TextInput and NumberInput contracts. Enter, Space, Escape, the arrow keys, Home and End are not handled by Field.

KeyWhereWhat happens
Tab / Shift+TabFields in a formMoves through the controls in DOM order. Field.Label, description, help text and error text are not Tab stops
Test: field.test.tsx › Tab moves through the controls in DOM order, field.test.tsx › Tab moves through the controls in DOM order in right to left
TabFrom a Field's label, help text or errorMoves to the control, and never stops on the label, the help text or the error
Test: field.test.tsx › Tab goes to the control and never stops on the label, the help text or the error
No keyField.LabelA click on the label focuses the control (native <label for>), also on the optional marker
Test: field.test.tsx › clicking the label focuses the input, also on the optional text
No keyThe description (Field.Prose), the help text (Field.HelpText), Field.ErrorMessageNot focusable and not interactive: no tabindex
Test: field.test.tsx › is never focusable and adds no role, live region or tabindex

API reference#

import { Field, useField } from '@kvirn-ui/react'

Field.Root

Renders <div> with no role. It takes every attribute of a <div> and passes ref to it. It creates the ids and links its label, description, help text, error and control to each other. The default theme is described on Theming.

PropTypeDefaultDescription
invalidbooleanfalseSets aria-invalid on the control and data-invalid on every part, and shows the Field.ErrorMessage. You decide when: KvirnUI holds no form state.
requiredbooleanfalseSets aria-required on the control and removes the optional text from the label. Never native required: pass that on the control too if you want the browser’s validation.
disabledbooleanfalseNative disabled on the control and data-disabled on every part.
controlIdstringgeneratedThe control’s id, for example to link to it from an error summary. The ids of the label, description and error are derived from it.
messagesPartial<KvirnMessages['field']>–Overrides field.optional and field.errorPrefix for this field.
Class or attributeValuesMeaning
kv-fieldalwaysThe part class. The default theme styles it.
data-invalidpresent or absentThe field is invalid.
data-requiredpresent or absentThe field is required.
data-disabledpresent or absentThe field is disabled.

Field.Label

Renders <label for>. It names the control. It ends with the optional text when the field is not required, unless marker="none" or the Field is in a group Fieldset.

PropTypeDefaultDescription
marker'optional' | 'none''optional'Whether the label ends with the optional text when the field is not required. Inside a group Fieldset the Field default is ‘none’.
Class or attributeValuesMeaning
kv-field-labelalwaysThe part class.
kv-field-optionalon the span inside the labelThe optional text, for example “(optional)”. It is part of the accessible name.
data-invalid, data-required, data-disabledpresent or absentThe state of the field.

Field.Prose

Renders <div class="kv-prose">, the description. Anywhere inside a Field it registers itself, gets an id and is listed in the control’s aria-describedby. It is the same Prose as outside a Field.

Class or attributeValuesMeaning
kv-prosealwaysThe class that styles the text for reading.
data-invalidpresent or absentThe field is invalid.
data-disabledpresent or absentThe field is disabled.

Field.HelpText

Renders <p>, the help text. It takes every attribute of a <p> except id. It registers like a description and is listed in DOM order, then the error. Outside a Field or Fieldset it renders a plain paragraph with no id and warns in development.

Class or attributeValuesMeaning
kv-field-help-textalwaysThe part class. The default theme sets it a step smaller than the description.
data-invalidpresent or absentThe field is invalid.
data-disabledpresent or absentThe field is disabled.

Field.ErrorMessage

Renders <p>, rendered only while the field is invalid. It takes every attribute of a <p> except id. It starts with a decorative error icon and the field.errorPrefix text, which the default theme hides from sight but not from screen readers. It is not a live region (a part of the page that a screen reader reads aloud when it changes).

Class or attributeValuesMeaning
kv-field-error-messagealwaysThe part class.
kv-field-error-prefixon a span insideThe prefix text, for example “Error:”. The theme hides it visually, not from screen readers.
data-invalidalwaysThe part only renders while invalid.
data-disabledpresent or absentThe field is disabled.

useField

Options

PropTypeDefaultDescription
idstringgeneratedThe control’s id. The description and error ids are derived from it.
invalidbooleanfalseMarks the field as invalid.
requiredbooleanfalsearia-required on the control, and no optional text in the label.
disabledbooleanfalseNative disabled on the control.
hasDescriptionbooleanfalseYou render one description with descriptionProps, so the control’s aria-describedby lists it from the first render.
descriptionsreadonly string[]–The names of the descriptions you render with getDescriptionProps(name), in render order. aria-describedby lists them in this order, then the error. If you also set hasDescription, that description comes first.
hasErrorMessagebooleantruefalse when you render no error message for an invalid field.
marker'optional' | 'none''optional'Whether the optional text shows. Inside a group Fieldset the default is ‘none’.
messagesPartial<KvirnMessages['field']>–Per-instance message overrides.

Result

PropTypeDefaultDescription
rootPropsFieldRootPartProps–Spread on the container.
labelPropsFieldLabelPartProps–Spread on the <label>: class, id and htmlFor.
descriptionPropsFieldDescriptionPartProps–For the one description (hasDescription).
getDescriptionProps(name: string) => FieldDescriptionPartProps–For the description with this name. Its id is listed in descriptions.
errorMessagePropsFieldErrorMessagePartProps–Spread on the error message, which you render only while invalid.
controlPropsFieldControlPartProps–Spread on the control: id, aria-describedby, aria-invalid, aria-required, disabled and data-* state.
controlIdstring–The control’s id.
labelIdstring–The label’s id, for an aria-labelledby from another part.
descriptionIdstring–The base id the description ids are derived from.
errorMessageIdstring–The error message’s id.
isInvalidboolean–Whether the field is invalid.
isRequiredboolean–Whether the field is required.
isDisabledboolean–Whether the field is disabled.
marker'optional' | 'none'–The resolved marker: your option, else ‘none’ in a group Fieldset.
optionalMarkerstring | undefined–The text to render after the label and a space, or undefined when the label shows none.
errorPrefixstring–The resolved field.errorPrefix text. Start the error message with it.

Strings

These are the default texts in @kvirn-ui/i18n.

For your whole app, pass messages to KvirnProvider. For one Field, pass its messages prop.

field.optional

The text that ends the label of a field that is not required.

LanguageDefault text
Svenska(valfritt)
Suomi(vapaaehtoinen)
Norsk bokmål(valgfritt)
Norsk nynorsk(valfritt)
English(optional)

field.errorPrefix

The text that starts the Field.ErrorMessage.

LanguageDefault text
SvenskaFel:
SuomiVirhe:
Norsk bokmålFeil:
Norsk nynorskFeil:
EnglishError:

Known issues#

  • The description is associated after mount. Server-rendered markup lists a Prose or a HelpText in aria-describedby only after hydration. useField with hasDescription or descriptions doesn't have this gap.
  • Error under the control on phones. The on-screen keyboard can cover the message under a focused field. The mitigation is in the docs (focus on submit, scroll-padding), not in the components, and the AT and usability run checks it. The error is linked from the first render whenever the field is invalid, in server-rendered markup too, and when an app moves focus to the field in an effect after submit. Tests: field.test.tsx › focus on submit.
  • WebKit is not automated. Keyboard rows run in Vitest browser mode on Chromium. WebKit is not covered by the automated tests, so check Safari with your own users and assistive technology.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassfield.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassfield.test.tsx (axe and role, name and state): wiring: name and description per state › label and description: the label names the control, the description describes it
Keyboard contract rows (Vitest browser)passn/apass4 rows, each names a test in field.test.tsx
Focus order and managementpassn/apassfield.test.tsx: keyboard › Tab goes to the control and never stops on the label, the help text or the error
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Tab to the box: the label is read, then the description and the help text.
  • A field that is not required reads "(optional)" at the end of its name.
  • Submit the form empty: "Error:" and the message are read when focus arrives.
  • Click the label: the box takes focus.
  • On a phone with the keyboard open, the error under an invalid field stays visible.