# Fieldset

A native fieldset and legend that give a group of related form fields one name. Use it for an address, a set of checkboxes or a block of fields that can be disabled together.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-fieldset`
- Source: [libs/ui/src/lib/molecules/fieldset/fieldset.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/fieldset/fieldset.ts)

```typescript title="Import"
import { Fieldset } from '@vigdx/ui/fieldset';
```

## Basic

`legend` names the group; project the fields.

```typescript title="fieldset-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Fieldset } from '@vigdx/ui/fieldset';
import { InputField } from '@vigdx/ui/input-field';

@Component({
  selector: 'app-fieldset-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Fieldset, InputField],
  template: `
    <vig-fieldset legend="Shipping address" class="w-full max-w-md">
      <vig-input label="Street" autocomplete="shipping street-address" />
      <vig-input label="City" autocomplete="shipping address-level2" />
      <vig-input label="Postal code" autocomplete="shipping postal-code" />
    </vig-fieldset>
  `,
})
export class FieldsetBasicDemo {}
```

## Checkbox group, disabled and collapsible

Checkboxes have no group component of their own: wrap them in a fieldset. `disabled` disables the native fieldset and with it every control inside. `collapsible` puts a disclosure button in the legend; `[(collapsed)]` holds the state.

```typescript title="fieldset-checkboxes.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Checkbox } from '@vigdx/ui/checkbox';
import { Fieldset } from '@vigdx/ui/fieldset';
import { ToggleSwitch } from '@vigdx/ui/toggle-switch';

@Component({
  selector: 'app-fieldset-checkboxes',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Fieldset, Checkbox, ToggleSwitch],
  template: `
    <div class="flex w-full max-w-md flex-col gap-4">
      <vig-toggle-switch label="Pause all notifications" [(value)]="paused" />
      <vig-fieldset legend="Notify me by" [disabled]="paused()" collapsible>
        <vig-checkbox label="Email" [value]="true" />
        <vig-checkbox label="Text message" />
        <vig-checkbox label="Push notification" />
      </vig-fieldset>
    </div>
  `,
})
export class FieldsetCheckboxesDemo {
  readonly paused = signal(false);
}
```

## Radio buttons and checkboxes

`vig-radio-group` already renders a fieldset with its `label` as the legend, plus the group's required and invalid states: do not wrap a single radio group in a `vig-fieldset` just to name it. Use `vig-fieldset` for checkbox lists and for groups that mix several fields.

`disabled` is the native fieldset attribute: the browser disables the native controls inside and leaves them out of form submission, and the kit's controls follow it too (their own widgets such as rating and knob sliders, and their disabled look), also through nested fieldsets. The forms model is not changed: to disable fields in Signal Forms or Reactive Forms, disable them there.

The group's name is the `legend` input rather than the kit's usual `heading`: it renders the native `<legend>`, which names the group but is not a heading.

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="fieldset"][data-part="group"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="fieldset"]`

| Part | Element | data-state |
| --- | --- | --- |
| `group` | The native `<fieldset>`. | `open, closed` |
| `legend` | The `<legend>`. |   |
| `trigger` | The disclosure button in the legend of a `collapsible` fieldset. | `open, closed` |
| `indicator` | The chevron of the trigger. |   |
| `content` | The collapsible part (`hidden="until-found"` while collapsed); `data-state` only when collapsible. | `open, closed` |
| `body` | The column of projected fields. |   |

## Accessibility

- Native semantics: the `<legend>` names the group, and screen readers announce it when focus enters the group.
- The collapse button sits inside the legend, so the legend keeps naming the group; it stays usable while the fieldset is disabled. Collapsed fields stay in the DOM as `hidden="until-found"`: they keep their values and are submitted, and find-in-page reveals them.

## API

### Fieldset

Selector: `vig-fieldset` · Kind: component

A native `<fieldset>` with its `<legend>`: groups related fields under one name that screen readers announce on entering the group (an address, a set of checkboxes). `disabled` disables the native fieldset, which disables every native control inside it, the kit's fields included.

**Example :**

```html
<vig-fieldset legend="Shipping address">
  <vig-input label="Street" … />
  <vig-input label="City" … />
</vig-fieldset>
```

`collapsible` puts a disclosure button (`aria-expanded`, `aria-controls`) in the legend, which keeps naming the group; the fields of a collapsed fieldset stay in the DOM as `hidden="until-found"` (still submitted, and find-in-page expands it). The legend's button stays usable while the fieldset is disabled, as the HTML spec has it.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `collapsed` (model) | `boolean` | `false` | Whether the fields are collapsed; two-way bindable. Ignored unless `collapsible`. |
| `collapsible` | `boolean` | `false` | Puts a button in the legend that collapses and expands the fields. |
| `disabled` | `boolean` | `false` | Disables the native fieldset and with it every native control inside. |
| `legend` (required) | `string` | — | The group's name, rendered in the `<legend>`. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `collapsedChange` | `boolean` | Whether the fields are collapsed; two-way bindable. Ignored unless `collapsible`. |

#### Methods

| Signature | Description |
| --- | --- |
| `toggle(): void` | Collapses expanded fields or expands collapsed ones. |
