Fieldset
A native <fieldset> that groups related controls under one <legend>, such as a set of checkboxes. A screen reader reads the legend as the group's name when focus enters it.
On this page
import { Checkbox, Description, ErrorMessage, Fieldset } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function FieldsetCheckboxes() {
const [topics, setTopics] = useState<string[]>([]);
const hintId = useId();
const errorId = useId();
const invalid = topics.length === 0;
const toggle = (topic: string) => (checked: boolean) =>
setTopics((prev) => (checked ? [...prev, topic] : prev.filter((t) => t !== topic)));
return (
<Fieldset.Root invalid={invalid} aria-describedby={invalid ? `${hintId} ${errorId}` : hintId}>
<Fieldset.Legend>Email topics</Fieldset.Legend>
<Description.Root id={hintId}>We send at most one email a week.</Description.Root>
<Checkbox.Root checked={topics.includes('product')} onCheckedChange={toggle('product')}>
Product news
</Checkbox.Root>
<Checkbox.Root checked={topics.includes('security')} onCheckedChange={toggle('security')}>
Security notices
</Checkbox.Root>
<Checkbox.Root checked={topics.includes('events')} onCheckedChange={toggle('events')}>
Events
</Checkbox.Root>
{invalid ? <ErrorMessage.Root id={errorId}>Pick at least one topic.</ErrorMessage.Root> : null}
</Fieldset.Root>
);
}
invalid marks every Todae control inside invalid: a Checkbox, Switch, RadioGroup or Field gets aria-invalid and its invalid style. A fieldset has no aria-invalid of its own, so say why with a Description or ErrorMessage and point the root's aria-describedby at it.
disabled is the native attribute: the browser disables every form control inside, and Todae's controls show their disabled style. A fieldset inside a disabled one is disabled too. Inside an invalid fieldset, a control or nested fieldset is invalid unless it sets invalid={false}, for a field the person got right. Controls inside the first Fieldset.Legend stay enabled, as the browser keeps them.
import { Checkbox, Fieldset, Stack, Switch } from '@tounsoo/todae';
import { useState } from 'react';
export default function FieldsetDisabled() {
const [paused, setPaused] = useState(true);
return (
<Stack.Root>
<Switch.Root checked={paused} onCheckedChange={setPaused}>
Pause notifications
</Switch.Root>
<Fieldset.Root disabled={paused}>
<Fieldset.Legend>Channels</Fieldset.Legend>
<Stack.Root direction="row">
<Checkbox.Root defaultChecked>Email</Checkbox.Root>
<Checkbox.Root>Push</Checkbox.Root>
<Checkbox.Root>SMS</Checkbox.Root>
</Stack.Root>
</Fieldset.Root>
</Stack.Root>
);
}
The parts stack with fieldset.gap. For controls side by side, put them in a Stack.Root direction="row".
Inside a Form.Root, a Fieldset.Root is a validation scope that shows its controls' problems in an ErrorMessage.Root with no id. Its customValidity prop sets a message with the browser's setCustomValidity on its own first control that takes part in validation, or, when all its controls sit in nested groups, on the first one at any depth, for a rule the browser can't check. It warns in development when there is no such control.
Accessible composition
Todae handles: Fieldset.Root is a native <fieldset>, so it has the group role and its first Fieldset.Legend is the group's name. disabled is the native attribute: every form control inside is disabled and leaves the tab order, except those in that first legend. invalid sets aria-invalid on each Todae control inside: Checkbox, Switch, RadioGroup.Root and its radios, and the control of a Field.Root. Controls in that first legend are left out of invalid too. Nested fieldsets inherit both.
You supply: A Fieldset.Legend as the first child, with text that names the question. The fieldset has no aria-invalid of its own, so point aria-describedby on the root at an ErrorMessage or Description that says why. invalid={false} on a control the person got right.
Watch for: A Legend wrapped in another element, or after another legend, does not name the group, and the controls in it follow the fieldset's disabled. disabled is not read-only: disabled controls cannot be focused, so keyboard users cannot reach their values. A styled div in place of the fieldset drops the group and its name.
When to use
When: Several controls answer one question: a checkbox group, a set of switches, or the fields of an address. Name the group with Fieldset.Legend.
When not: One field → Label with Field.Root. A single choice among options → RadioGroup, which is its own group. Sections of a long form with their own headings → headings.
Props
| Name | Type | Description |
|---|---|---|
disabled | boolean | Native disabled: every form control inside is disabled. Todae controls inside also show their disabled style. Default: false |
invalid | boolean | Marks the Todae controls inside invalid (aria-invalid and the invalid style); a control with invalid={false} opts out. A fieldset has no aria-invalid of its own, so point aria-describedby at an ErrorMessage. Default: false |
customValidity | string | A message for a rule the browser cannot check, set with setCustomValidity on the fieldset's own first control that takes part in validation; when all its controls sit in nested groups, on the first one at any depth, where its error shows. An empty string or undefined means the rule holds. Warns in development when there is no such control. |
aria-describedby | string | Ids of a Description or ErrorMessage for the whole group. |
children | React.ReactNode | A Fieldset.Legend first, then the controls. |
| Name | Type | Description |
|---|---|---|
children | React.ReactNode | Group name. |