# Fieldset (/docs/fieldset)





`Fieldset` groups the controls of one part of a form (an address, a set of notification channels, the billing details) under a legend. It renders a real `<fieldset>` on top of Ark UI's `Fieldset`, so the browser's own rules apply: the legend names the group for assistive tech, and `disabled` turns off every control inside at once.

It sits one level above [`Field`](/docs/field). A `Field` wires one control's label, description and error; a `Fieldset` names a group of them and carries the state they share. It never touches a field's own label or error, only its `disabled` and `invalid`.

```tsx
import { Fieldset } from '@42-central/ui-react/fieldset';
import { TextInput } from '@42-central/ui-react/text-input';

<Fieldset legend="Personal information" description="Shown on your public profile.">
  <TextInput label="Full name" />
  <TextInput label="Email" type="email" />
</Fieldset>
```

## Playground [#playground]

<story.Playground />

## Variants [#variants]

`variant` picks the frame. `default` is the kit's form hairline alone, so the group takes the background it sits on. `filled` adds a wash behind the controls, for a group that has to stand out on a busy page. `unstyled` drops the frame and the padding: the legend becomes a heading over the controls, for a form that is already inside a card.

In the framed variants the legend sits on the border, where the browser places the first `<legend>` of a `<fieldset>`.

<Preview align="block" code="&#x22;<Fieldset variant=\&#x22;default\&#x22; legend=\&#x22;default\&#x22; description=\&#x22;Shown on your profile.\&#x22;>…</Fieldset>\n<Fieldset variant=\&#x22;filled\&#x22; legend=\&#x22;filled\&#x22; description=\&#x22;Shown on your profile.\&#x22;>…</Fieldset>\n<Fieldset variant=\&#x22;unstyled\&#x22; legend=\&#x22;unstyled\&#x22; description=\&#x22;Shown on your profile.\&#x22;>…</Fieldset>&#x22;">
  <FieldsetVariants />
</Preview>

## Disabled [#disabled]

`disabled` reaches every control inside, with no prop on any of them. Toggle it below: each form control the kit ships is in this one group.

<Preview align="block" code="&#x22;<Fieldset legend=\&#x22;Every control\&#x22; disabled={disabled} invalid={invalid}>\n  <TextInput label=\&#x22;TextInput\&#x22; />\n  <Select label=\&#x22;Select\&#x22; data={projects} />\n  <TreeSelect label=\&#x22;TreeSelect\&#x22; data={tree} />\n  <Checkbox label=\&#x22;Checkbox\&#x22; />\n  {/* …and the rest of the form family */}\n  <Button type=\&#x22;submit\&#x22;>Save</Button>\n</Fieldset>&#x22;">
  <FieldsetEveryControl />
</Preview>

It gets there two ways. The `<fieldset>` disables every native `<input>`, `<button>`, `<select>` and `<textarea>` inside it, so nothing can be typed into, clicked or tabbed to. The kit's controls also read the group's state, and take the disabled look: Ark UI's `Field` inherits it, and the controls that render without a `Field` read it themselves.

<Callout type="info" title="Give each control a label">
  A control follows the group whether it has a `label` or not: it dims in a disabled group and turns
  red in an invalid one. It still needs an accessible name, so give it a `label`, wrap it in a
  [`Field`](/docs/field), or name it with `aria-label`.
</Callout>

A disabled control is left out of a form's submission. That is the browser's rule for a disabled `<fieldset>` too, so a group you disable while saving sends nothing. Use `readOnly` on the controls instead when their values must still be submitted.

## Error [#error]

`error` is a message for the whole group, under the controls: "pick at least one", "this address couldn't be verified". Like `Field`'s `error`, its presence sets `invalid`, and the group's `invalid` reaches every control inside, so they all turn red with it. A `Field` that sets its own `invalid` keeps it.

<Preview align="block" code="&#x22;<Fieldset\n  legend=\&#x22;Notifications\&#x22;\n  description=\&#x22;Where should we reach you?\&#x22;\n  error={picked.length === 0 ? \&#x22;Pick at least one channel.\&#x22; : undefined}\n>\n  <Checkbox label=\&#x22;Email\&#x22; … />\n  <Checkbox label=\&#x22;Slack\&#x22; … />\n  <Checkbox label=\&#x22;SMS\&#x22; … />\n</Fieldset>&#x22;">
  <FieldsetGroupError />
</Preview>

An error about one control still goes on that control's own `error`. Both can show at once.

## Sizes [#sizes]

`size` uses [`Field`](/docs/field)'s scale, `xs` to `xl`. It scales the legend, the description and error text, the padding and the gap between the controls. Set the controls' `size` to match.

## Anatomy [#anatomy]

For a layout the props don't cover, compose the parts: `Fieldset.Root` is the `<fieldset>`, `.Legend` its name, `.Description` the helper text, `.Error` the group's error. `.Error` renders only while the root is `invalid`. The controls go anywhere among them.

Each part takes a `testId`, rendered as `data-testid` on its own element. The ready-made `Fieldset` puts its `testId` on the `<fieldset>` and derives `` `${testId}-legend` ``, `` `${testId}-description` `` and `` `${testId}-error` `` for its parts. See the [Testing](/docs/testing) guide.

<Preview align="block" code="&#x22;<Fieldset.Root testId=\&#x22;address\&#x22; invalid>\n  <Fieldset.Legend testId=\&#x22;address-legend\&#x22;>Shipping address</Fieldset.Legend>\n  <Fieldset.Description testId=\&#x22;address-description\&#x22;>\n    We ship your swag here.\n  </Fieldset.Description>\n  <TextInput label=\&#x22;Street\&#x22; />\n  <TextInput label=\&#x22;City\&#x22; />\n  <Fieldset.Error testId=\&#x22;address-error\&#x22;>We couldn't verify this address.</Fieldset.Error>\n</Fieldset.Root>&#x22;">
  <FieldsetAnatomy />
</Preview>

`.Legend`, `.Description` and `.Error` take `asChild`. `Fieldset` and `Fieldset.Root` don't: the cascade of `disabled` belongs to a real `<fieldset>`, which another element can't stand in for.

## API [#api]

### Fieldset [#fieldset]

<TypeTable
  type="{
  legend: {
    type: 'ReactNode',
    description: 'The name of the group, on the border (`unstyled`: a heading above the controls).',
  },
  description: {
    type: 'ReactNode',
    description: 'Helper text for the whole group, under the legend.',
  },
  error: {
    type: 'ReactNode',
    description: 'An error for the whole group, under the controls. Its presence sets `invalid`.',
  },
  disabled: {
    type: 'boolean',
    default: 'false',
    description: 'Disables every control inside.',
  },
  invalid: {
    type: 'boolean',
    description: 'Marks the group invalid: shows the error and turns every control inside red. Defaults to true when `error` is set.',
  },
  variant: {
    type: '&#x22;default&#x22; | &#x22;filled&#x22; | &#x22;unstyled&#x22;',
    default: '&#x22;default&#x22;',
    description: 'The frame: the kit\'s hairline, the hairline over a wash, or none.',
  },
  size: {
    type: '&#x22;xs&#x22; | &#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;',
    default: '&#x22;md&#x22;',
    description: 'Scales the legend, the description and error text, the padding and the gap between the controls.',
  },
  classNames: {
    type: '{ legend?, description?, error? }',
    description: 'A class per part. `className` styles the `<fieldset>`.',
  },
  testId: {
    type: 'string',
    description: 'data-testid on the `<fieldset>`; derives `${testId}-legend`, `${testId}-description` and `${testId}-error` on the parts.',
  },
}"
/>

Every other prop (including `id`) forwards to the `<fieldset>`.

### Fieldset.Root [#fieldsetroot]

<TypeTable
  type="{
  disabled: { type: 'boolean', default: 'false', description: 'Disables every control inside.' },
  invalid: { type: 'boolean', default: 'false', description: 'Shows `Fieldset.Error` and turns every control inside red.' },
  variant: { type: '&#x22;default&#x22; | &#x22;filled&#x22; | &#x22;unstyled&#x22;', default: '&#x22;default&#x22;', description: 'The frame. The parts inside follow it.' },
  size: { type: '&#x22;xs&#x22; | &#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;', default: '&#x22;md&#x22;', description: 'The scale. The parts inside follow it.' },
  testId: { type: 'string', description: 'data-testid on the `<fieldset>`.' },
}"
/>

### Fieldset.Legend, Fieldset.Description, Fieldset.Error [#fieldsetlegend-fieldsetdescription-fieldseterror]

<TypeTable
  type="{
  asChild: { type: 'boolean', default: 'false', description: 'Render as the single child element instead.' },
  testId: { type: 'string', description: 'data-testid on the part.' },
}"
/>

## Accessibility [#accessibility]

* The root is a native `<fieldset>`, which carries the `group` role, and its legend names it. Ark also sets `aria-labelledby` to the legend.
* The description and, while invalid, the error describe the group through `aria-describedby`. The error is a polite live region, so it is announced when it appears.
* `disabled` takes every control out of the tab order and out of the accessibility tree's interactive controls, natively. The kit's controls also mark themselves `data-disabled` / `aria-disabled` where they draw their own elements (a slider thumb, a rating star, a tree picker's trigger).
* RTL: the frame and the legend use logical padding, so the group mirrors under `dir="rtl"`.
