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.

Fieldset

A native fieldset that groups related questions, or the controls of one question, under a legend that names the group. It holds no form state, and it never moves focus.

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

When to use it#

  • Use a Fieldset when one question is answered in several boxes, such as a name in a first and a last name box. Set group.
  • Use a plain Fieldset to put related questions under one name, such as an address.
  • Write a legend that asks the question or names the group, and put it first. A screen reader (software that reads the screen aloud) uses it as the name of the group.
  • Don’t wrap every Field in a Fieldset. A screen reader announces a fieldset when focus enters it, so nested ones get noisy.
  • Not for one control: use a Field, whose label names it. Not for checkboxes, radio buttons or a date: a CheckboxGroup, a RadioGroup and a DateInput already include a fieldset.

Information: Did you know?

To show an answer that can’t be changed, show it as text, such as “Applicant: Anna Andersson”, not as a disabled group. A disabled control gets no keyboard focus, so people using a keyboard or a screen reader can’t reach it to read it (see the Keyboard table).

Example#

Postal address

Use cases#

One question, several boxes

When the boxes together make one answer, set group. The legend then ends with “(optional)” if the group isn’t required, and the Fields inside never say “(optional)” on their own: one box of a name is never optional by itself.

Name of your contact person (optional)

Props in this example:

Describe the group and report its error

The description is read before answering, so it goes above the controls. The help text goes under them, and the error last. All three belong to the group and are linked to it, so a screen reader can read them when focus enters the group. Support differs between screen readers (see Known issues). After a failed send, move focus to the first control that is wrong.

What is your name?

Write it as it is in your passport.

Both names are needed to find your case.

Props in this example:

Information: Did you know?

A Fieldset’s invalid marks only its own parts. Mark a Field inside with the Field’s own invalid, and give it its own error message.

The question is the page heading

When a page asks one thing, the legend can be the page’s heading. Put the heading inside the legend and add the class kv-fieldset-legend--heading. Use h1 if it is the page’s main heading: this example is in a page that already has one, so it uses h2.

How can we contact you?

Wire your own fieldset

For markup of your own, useFieldset gives the id, aria-describedby and state attributes to spread on a native fieldset and its legend. Name the descriptions from the first render, so server-rendered markup is complete.

Postal address

We send letters about your case to this address.

Props in this example:

Warning: Don't forget

Every fieldset needs a legend as its first child, or the group has no name (1.3.1 Info and Relationships, 4.1.2 Name, Role, Value). See What you need to do.

Accessibility#

What it does for you#

  • APG pattern: none. A fieldset is native HTML: <fieldset> with a <legend> is the group's name, and the browser maps it to the group role.
  • Deviations: none from APG. A Prose in a Fieldset is the group's description (there is no Fieldset.Description), and the group's help text is Fieldset.HelpText.
  • Native elements used: <fieldset> (Fieldset.Root), <legend> (Fieldset.Legend), <p> (Fieldset.ErrorMessage, which behaves like Field.ErrorMessage), a <div class="kv-prose"> for the description (Fieldset.Prose), a <p class="kv-field-help-text"> for the help text (Fieldset.HelpText), and a <span> for the optional marker.

A Fieldset groups related questions or the options of one question. Its legend is the group's name, and its description (Fieldset.Prose), help text (Fieldset.HelpText) and error are the group's. Checkbox groups, radio groups and the date input are built on it.

PartElement / roleARIA / stateNotes
Fieldset.Root<fieldset> → grouparia-describedby = every description id in DOM order, then the error id, only for parts rendered. Native disabled. data-invalid, data-required, data-disabledClass kv-fieldset. Props: invalid, required, disabled, group, messages. The root is always a <fieldset>. No aria-invalid: ARIA doesn't support it on group, so the error reaches users through the description. disabled disables every control inside (native)
Fieldset.Legend<legend>, the group's namedata-invalid, data-required, data-disabledClass kv-fieldset-legend. Takes marker: appends <span class="kv-field-optional">(optional)</span> (in en; each language has its own text) when optional. Default optional in a group fieldset that isn't required, and none in a plain Fieldset
Fieldset.Prose<div class="kv-prose" id>, or the element as givesIn the fieldset's aria-describedby, in DOM order, before the error. data-invalid, data-disabledA Fieldset.Prose inside a Fieldset, and not inside a Field in it, registers itself and describes the group. Several are allowed, each with its own id. There is no Fieldset.Description
Fieldset.HelpText<p class="kv-field-help-text" id>, or the element as gives. No roleIn the fieldset's aria-describedby, in DOM order (with the Proses), before the error. data-invalid, data-disabledThe Field.HelpText part under the group's name (see field.a11y.md): a short instruction or example under the controls, for example the format of a date. A Fieldset.HelpText inside a Fieldset, and not inside a Field in it, registers itself and describes the group. Outside a Fieldset or Field it warns (help-text-outside-field) and renders a plain paragraph with no id. Never focusable, never a live region
Fieldset.ErrorMessage<p id>, only while the fieldset is invalidIn the fieldset's aria-describedby, after every description. data-invalidClass kv-field-error-message. The same prefix and icon as Field's
useFieldsetthe same attributes, for your own elementsfieldsetProps, legendProps, descriptionProps, getDescriptionProps(name), errorMessagePropsOptions: invalid, required, disabled, hasDescription, descriptions (names, in render order), hasErrorMessage (default: invalid), group, marker, messages. Also returns optionalMarker and errorPrefix

Rules, tested in fieldset.test.tsx:

  • The legend is the first child of the fieldset in the DOM (the browser needs this for the name).
  • A Fieldset's invalid marks its own parts only. It doesn't pass down to the Fields inside, so a group error doesn't mark every control.
  • A Field inside a group fieldset defaults to marker="none": an option or a date box is never "(optional)". The group's legend carries the marker instead, when the group isn't required.
  • A plain Fieldset only groups questions (for example an address), so its legend has no marker, and its Fields mark themselves.
  • Nested fieldsets: the nearest Fieldset's text parts win, and a plain Fieldset inside a group fieldset stops the group defaults.
  • A Prose, a HelpText and the Fieldset.ErrorMessage attach to the nearest Field or Fieldset, as in field.a11y.md: a Prose or a HelpText in a Field in the Fieldset describes that Field's control, not the group.
  • A Fieldset.Prose in a Fieldset is the group's description. It is the Prose's text content: a heading, list or link inside it is read as plain text, so keep a description short. A Prose that isn't a description goes outside the Fieldset. A help text is plain text only.
  • Several descriptions and help texts are listed in DOM order, then the error, each with its own id. With the default order that is the description, the help text, then the error. A second Fieldset.ErrorMessage gives a dev warning. useFieldset({ descriptions }) and getDescriptionProps(name) give the same markup on the server.

It announces nothing.

What you need to do#

  • Give every fieldset a Fieldset.Legend that asks the question or names the group. Put the Fieldset.Legend first inside the Fieldset.
  • A legend that is the page's heading: <Fieldset.Legend className="kv-fieldset-legend--heading"><h1>…</h1></Fieldset.Legend>.
  • Use a Fieldset for options and for one question asked in several controls. Don't wrap every Field in one: a fieldset announces itself, and nested ones get noisy.
  • Set invalid and render a Fieldset.ErrorMessage together, with plain text that says what's wrong and how to fix it (3.3.1 Error Identification, level A; 3.3.3 Error Suggestion, level AA).
  • Set group on a Fieldset that holds options or the parts of one answer, so its Fields drop the optional marker and its legend takes it.
  • Mark which individual Fields inside are wrong with the Fields' own invalid: the Fieldset's doesn't cascade.

Warning: Don't forget

Set invalid and render a Fieldset.ErrorMessage together, with text that says what is wrong and how to fix it. A group has no invalid state of its own, so without the text a screen reader user isn’t told anything is wrong (3.3.1 Error Identification, 3.3.3 Error Suggestion).

Visual and modes#

  • Focus indicator: on the controls inside. The fieldset has none.
  • Target size: no interactive area of its own.
  • Colour: the legend, description, help text and marker use the normal text colour, and a help text keeps it when the group is invalid or disabled. The error uses the danger colour with a prefix and an icon, so colour is never the only signal (1.4.1 Use of Color, level A).
  • forced-colors behaviour: nothing depends on colour. The error uses the system's text colour.
  • reduced-motion behaviour: none.
  • RTL: logical properties only.

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • On error: Fieldset never moves focus. After a failed submit, you move focus to the first invalid control, or to an ErrorSummary (see its contract).
  • Never obscured by: Fieldset renders no overlay.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): native <fieldset>/<legend>, the group's description and error in its accessible description (fieldset.test.tsx › wiring: name and description per state).
  • 1.4.1 Use of Color (A): the error has a prefix and an icon.
  • 2.5.3 Label in Name (A): the visible legend text, with the marker, is the group's name.
  • 3.3.1 Error Identification (A), 3.3.2 Labels or Instructions (A), 3.3.3 Error Suggestion (AA): as for Field.
  • 4.1.2 Name, Role, Value (A): native group, with a name and a description.

Keyboard#

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

Fieldset handles no keys and moves no focus. A group's keys come from its controls (see the TextInput, CheckboxGroup and RadioGroup pages). Enter, Space, Escape, the arrow keys, Home and End are not handled by Fieldset.

KeyWhereWhat happens
Tab / Shift+TabFieldset with controlsMoves through the controls in DOM order. The fieldset, legend, description, help text and error aren't Tab stops
Test: fieldset.test.tsx › Tab moves through the controls in DOM order and never stops on the group, its legend, its help text or its error
No keyfieldset[disabled]Disabled controls inside are skipped (native)
Test: fieldset.test.tsx › disabled disables every control inside, natively, fieldset.test.tsx › Tab skips the controls of a disabled fieldset (native)

API reference#

import { Fieldset, useFieldset } from '@kvirn-ui/react'

Fieldset.Root

Renders <fieldset> with the role group. It takes every attribute of a <fieldset> and passes ref to it. Your own aria-describedby ids come after its. The default theme is described on Theming.

PropTypeDefaultDescription
invalidbooleanfalseSets data-invalid on the fieldset’s own parts and shows its Fieldset.ErrorMessage. It does not pass down to the Fields inside.
requiredbooleanfalseSets data-required and removes the optional text from the legend.
disabledbooleanfalseNative disabled on the <fieldset>, which disables every control inside it.
groupbooleanfalseOne question answered with several controls. The legend takes the optional text and the Fields inside drop theirs.
messagesPartial<KvirnMessages['field']>–Overrides field.optional and field.errorPrefix for this fieldset.
Class or attributeValuesMeaning
kv-fieldsetalwaysThe part class. The default theme styles it.
data-invalidpresent or absentThe fieldset is invalid.
data-requiredpresent or absentThe fieldset is required.
data-disabledpresent or absentThe fieldset is disabled.
aria-describedbyidsEvery description and help text in DOM order, then the error, then your own ids. Only rendered parts are listed.

Fieldset.Legend

Renders <legend>, the group’s name. It takes every attribute of a <legend> and passes ref to it. Put it first in the fieldset.

PropTypeDefaultDescription
marker'optional' | 'none''optional' in a group, 'none' otherwiseWhether the legend ends with the optional text. It shows only when the fieldset is not required.
Class or attributeValuesMeaning
kv-fieldset-legendalwaysThe part class.
kv-fieldset-legend--headingclass you addThe legend is the page’s heading: put the heading inside it. Theme option.
kv-field-optionalon the span inside the legendThe optional text. It is part of the group’s name.
data-invalid, data-required, data-disabledpresent or absentThe state of the fieldset.

Fieldset.Prose

Renders <div class="kv-prose">, the group’s description. Directly in a Fieldset, and not inside a Field in it, it registers itself and is listed in the fieldset’s aria-describedby.

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

Fieldset.HelpText

Renders <p>, the group’s help text. It is the same part as Field.HelpText under the group’s name, and goes under the controls.

Class or attributeValuesMeaning
kv-field-help-textalwaysThe part class. The default theme sets it in smaller text.
data-invalidpresent or absentThe fieldset is invalid.
data-disabledpresent or absentThe fieldset is disabled.

Fieldset.ErrorMessage

Renders <p>, rendered only while the fieldset is invalid, with the same icon and prefix as the Field’s. It is not a live region.

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 fieldset is disabled.

useFieldset

Options

PropTypeDefaultDescription
idstringgeneratedThe fieldset’s id. The description and error ids are derived from it.
invalidbooleanfalseMarks the fieldset as invalid.
requiredbooleanfalsedata-required, and no optional text in the legend.
disabledbooleanfalseNative disabled on the <fieldset>.
groupbooleanfalseOne question answered with several controls.
hasDescriptionbooleanfalseYou render one description with descriptionProps, so aria-describedby lists it.
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 fieldset.
marker'optional' | 'none''optional' in a group, 'none' otherwiseWhether the legend’s optional text shows.
messagesPartial<KvirnMessages['field']>–Per-instance message overrides.

Result

PropTypeDefaultDescription
fieldsetPropsFieldsetRootPartProps–Spread on the <fieldset>: class, id, aria-describedby, disabled and state.
legendPropsFieldsetLegendPartProps–Spread on the <legend>, the first child of the fieldset.
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.
descriptionIdstring–The base id the description ids are derived from.
errorMessageIdstring–The error message’s id.
isInvalidboolean–Whether the fieldset is invalid.
isRequiredboolean–Whether the fieldset is required.
isDisabledboolean–Whether the fieldset is disabled.
isGroupboolean–Whether group is on.
marker'optional' | 'none'–The resolved legend marker.
optionalMarkerstring | undefined–The legend’s optional text, or undefined when it 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 Fieldset, pass its messages prop.

A Fieldset uses the same two texts as a Field.

field.optional

The text that ends the legend of a group 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 Fieldset.ErrorMessage.

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

Known issues#

  • aria-describedby on a <fieldset> may not be announced consistently. GOV.UK reports that NVDA, JAWS and VoiceOver announce it when the user enters the group and that TalkBack does not do so consistently. That is a report from others, not a result here: test all four with your users.
  • 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.

Information: Did you know?

Test with the screen readers your users have (see Testing), and read these items as reports, not results. Add what applies to your service to your accessibility statement, with the workaround.

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/apassfieldset.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassfieldset.test.tsx (axe and role, name and state): the legend is the group’s name, and the description is its description, in en
Keyboard contract rows (Vitest browser)passn/apass2 rows, each names a test in fieldset.test.tsx
Focus order and managementpassn/apassfieldset.test.tsx: keyboard › Tab moves through the controls in DOM order and never stops on the group, its legend, its help text or its 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 in from outside: the legend and "group" are read, then the description or the error (support differs).
  • A group that is not required reads "(optional)" at the end of its legend.
  • There is no Tab stop on the legend, description, help text or error.
  • Submit with a box empty: after focus lands on the first wrong box, the group's error can be heard.
  • A disabled fieldset: Tab skips every control.