Fieldset component is used to group together several related form controls. This helps to make these groups accessible for assistive technologies.

Open in new tab
import { Fieldset, Input, Select } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery address">
    <Input label="Street address" name="street" autoComplete="street-address" />
    <Select label="City" name="city" placeholder="Choose a city">
      <option value="helsinki">Helsinki</option>
      <option value="espoo">Espoo</option>
      <option value="vantaa">Vantaa</option>
    </Select>
    <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" />
  </Fieldset>
);
import { Fieldset, Input, Textarea } from "@kesko/components";

export default () => (
  <Fieldset label="Contact person" description="Who the driver calls if nobody answers the door.">
    <Input label="Name" name="contact-name" autoComplete="name" />
    <Input label="Phone" name="contact-phone" type="tel" autoComplete="tel" />
    <Textarea label="Delivery instructions" name="instructions" placeholder="Door code, floor, where to leave the order" />
  </Fieldset>
);
import { Checkbox, Fieldset } from "@kesko/components";

export default () => (
  <Fieldset label="Order updates">
    <Checkbox label="Email" name="updates" value="email" defaultChecked />
    <Checkbox label="Text message" name="updates" value="text" />
    <Checkbox label="Push notification" name="updates" value="push" />
  </Fieldset>
);
import { Fieldset, Input } from "@kesko/components";

const compact = {
  "--k-fieldset-gap": "var(--k-space-2xs)",
  "--k-fieldset-border": "var(--k-color-border-primary)",
  "--k-fieldset-radius": "var(--k-radius-md)",
  "--k-fieldset-legend-size": "var(--k-font-size-md)",
  "--k-fieldset-legend-text": "var(--k-color-text-primary)",
};

export default () => (
  <Fieldset label="Card holder" variant="outline" style={compact}>
    <Input label="First name" name="first-name" autoComplete="given-name" />
    <Input label="Last name" name="last-name" autoComplete="family-name" />
  </Fieldset>
);
import { Fieldset, Input, Select } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery address" disabled>
    <Input label="Street address" name="street" autoComplete="street-address" />
    <Select label="City" name="city" placeholder="Choose a city">
      <option value="helsinki">Helsinki</option>
      <option value="espoo">Espoo</option>
      <option value="vantaa">Vantaa</option>
    </Select>
    <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" />
  </Fieldset>
);
import { Fieldset, Input } from "@kesko/components";

export default () => (
  <Fieldset label="Name" hideLabel>
    <Input label="First name" name="first-name" autoComplete="given-name" />
    <Input label="Last name" name="last-name" autoComplete="family-name" />
  </Fieldset>
);
import { Fieldset, Input, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery address">
    <Input label="Street address" name="street" autoComplete="street-address" />
    <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" />
    <Fieldset label="If nobody is home">
      <Radio label="Leave the order at the door" name="absent" value="door" defaultChecked />
      <Radio label="Take the order back to the store" name="absent" value="store" />
    </Fieldset>
  </Fieldset>
);
import { Fieldset, Input, Select } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery address" variant="outline">
    <Input label="Street address" name="street" autoComplete="street-address" />
    <Select label="City" name="city" placeholder="Choose a city">
      <option value="helsinki">Helsinki</option>
      <option value="espoo">Espoo</option>
      <option value="vantaa">Vantaa</option>
    </Select>
    <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" />
  </Fieldset>
);
import { Fieldset, Radio } from "@kesko/components";

export default () => (
  <Fieldset label="Payment method">
    <Radio label="Card" name="payment" value="card" defaultChecked />
    <Radio label="Invoice" name="payment" value="invoice" />
    <Radio label="Pay at pickup" name="payment" value="pickup" />
  </Fieldset>
);
import { Fieldset, Input, Stack } from "@kesko/components";

export default () => (
  <Stack gap="xl" alignment="stretch">
    <Fieldset label="Card holder" required>
      <Input label="First name" name="first-name" autoComplete="given-name" required hideRequired />
      <Input label="Last name" name="last-name" autoComplete="family-name" required hideRequired />
    </Fieldset>
    <Fieldset label="Billing address" required hideRequired>
      <Input label="Street address" name="street" autoComplete="street-address" required hideRequired />
      <Input label="Postal code" name="postal" inputMode="numeric" autoComplete="postal-code" required hideRequired />
    </Fieldset>
  </Stack>
);
import { Fieldset, Input, Select, Stack } from "@kesko/components";

export default () => (
  <Fieldset label="Amount">
    <Stack direction="row">
      <Input label="Quantity" name="quantity" inputMode="numeric" defaultValue="2" style={{ "--k-input-inline-size": "8rem" }} />
      <Select label="Unit" name="unit" defaultValue="kg" style={{ "--k-select-inline-size": "8rem" }}>
        <option value="kg">kg</option>
        <option value="g">g</option>
        <option value="pcs">pcs</option>
      </Select>
    </Stack>
    <Input label="Note to the picker" name="note" placeholder="Ripe ones, please" />
  </Fieldset>
);
import { Fieldset, Input } from "@kesko/components";

export default () => (
  <Fieldset label="Delivery address" error="We don't deliver to this address yet.">
    <Input label="Street address" name="street" defaultValue="Satamakatu 3" autoComplete="street-address" />
    <Input label="Postal code" name="postal" defaultValue="48100" inputMode="numeric" autoComplete="postal-code" />
  </Fieldset>
);

Properties #

NameDescriptionTypeDefault
label

Label for the group, rendered as the <legend>. Always provide one, even when it is hidden with hideLabel.

ReactNode—
hideLabel

Hides the legend visually while keeping it available to assistive technology.

booleanfalse
variant

The style of the group. outline draws a border around the fields with the legend sitting on it.

"plain" | "outline""plain"
description

Assistive text for the whole group, shown under the legend.

ReactNode—
error

Optional error message for the whole group, shown after the fields. Use it for an error no single field owns, such as a choice missing from a group of radio buttons. When a required group of radio buttons has nothing selected on submit, a built-in message shows here on its own; error replaces it.

ReactNode—
required

Draws the required marker on the legend. The controls inside decide what is actually required.

booleanfalse
hideRequired

Hides the required marker on the legend. Use it when the form says once that every field is required.

booleanfalse
disabled

Disables every control inside the group and drops their values from the submitted data.

boolean—
name

The name of the group.

string—
form

The id of the form the group belongs to. Only needed when the group sits outside that form.

string—

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-fieldset-gap

Overrides the space between the legend, the fields and the messages.

var(--k-space-md)
--k-fieldset-border

Overrides the border color of the outline variant.

var(--k-color-text-base)
--k-fieldset-radius

Overrides the border radius of the outline variant.

var(--k-radius-lg)
--k-fieldset-legend-size

Overrides the font size of the legend.

var(--k-font-size-lg)
--k-fieldset-legend-text

Overrides the text color of the legend.

var(--k-color-text-base)

Accessibility #

Fieldset is built on a native <fieldset> with a <legend>, so assistive technology reads the label as the name of the group and announces it with the controls inside.

  • Always pass a label. The label is what names the group. If there is no room for it, pass hideLabel rather than leaving it out.
  • Keep the label short. Some screen readers read it before every control's own label, so a long one is repeated many times. Put detail in description instead.
  • Group only what belongs together. A fieldset earns its place when the fields share one meaning, such as the lines of an address or the options of one choice. A group that already has a name, such as a form under its own heading or inside a dialog, does not need a second one from a Fieldset.
  • Fieldset is not a form. Put everything people fill in and send together inside a Form. Fieldset names the parts inside it that share one meaning.
  • Put a group's error on the group. An error no single field owns, such as nothing chosen in a group of radio buttons, goes in error so it is read with the group. An error about one field stays on that field. The message shows after everything in the group, so when radio buttons share a group with other fields, give the radio buttons a Fieldset of their own. A required Radio group that is left empty shows the browser's own message here without any extra code.
  • disabled switches off every control inside. Reach for it only when the whole group does not apply right now, and say why nearby. Disabled controls leave the tab order and cannot explain themselves.
  • Check contrast if you override colors. The label needs 4.5:1 against the surface, and --k-fieldset-border needs 3:1 against it in the outline variant. Verify with the WebAIM contrast checker.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Fieldset component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Fieldset:

import { Fieldset } from "@kesko/components";

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #