Fieldset unstable
Groups several fields under one legend, such as a company field and a tax id field under "Billing details", and disables all of them at once.
Fieldset is a headless component: the legend needs an id for the
fieldset to point at, and the fields inside read whether the fieldset is
disabled. It keeps no state of its own, and emits nothing.
Use a fieldset for several fields, each with its own label, validity and
error. One value with one error, such as a range slider, a checkbox group
or a radio group, is a single Field drawn with field.group and
field.legend.
Anatomy
| Part | Element |
|---|---|
root |
<fieldset> |
legend |
any element, usually a <div> |
Each part takes the consumer's builder and returns it, so the consumer
goes on chaining and adds children with .children(...):
fieldset.root(h.fieldset).children(fieldset.legend(h.div).children("Billing details"), ...).
No part adds markup.
The root's aria-labelledby always points at the legend's id, so draw
the legend. A fieldset with no legend needs no component:
h.fieldset.disabled(...) is enough.
Data attributes
On both parts, present with an empty value or absent:
data-disabled
Keyboard
Fieldset adds no key handler. Each control keeps its own keyboard.
Usage
A disabled fieldset writes the native disabled on the <fieldset>, and
the browser then disables every control inside it, including the hidden
input a checkbox or a switch keeps its form value in. So nothing inside
takes a press or focus, and the form submits none of it. The browser
leaves out one place: the controls inside a <legend> that is the
fieldset's first child.
A Field inside reads the nearest fieldset, at any depth, and is
disabled while the fieldset is: data-disabled on its parts, its error
hidden and its focus dropped. Nothing is passed to it. A control hears of
the fieldset from its field, so a control's other parts still take
disabled: field.disabled, as in any field. A control drawn with no
field around it is disabled by the browser alone, and its own parts
don't show it: pass disabled: fieldset.disabled to it.
A nested fieldset reads the fieldset around it, and is disabled while it is, along with its legend and its fields.
Example Two fields under one legend
import { Html, HtmlServer } from "effect/ui/html"import { Field, Fieldset } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Billing = Html.component({ name: "Billing", model: Schema.Struct({ locked: Schema.Boolean }), message: Schema.Never}).make({ init: () => ({ locked: true }), update: ($) => $.model, view: (h, model) => Fieldset(h, { props: { disabled: model.locked }, view: (fieldset) => fieldset.root(h.fieldset.class("billing")).children( fieldset.legend(h.div).children("Billing details"), ["Company", "Tax ID"].map((name) => Field(h, { key: name, view: (field) => field.root(h.div).children(field.label(h.label).children(name), field.control(h.input)) }) ) ) })})
// The fieldset is disabled and named by its legend, and each field's input is disabled.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Billing, {}))const [, root, legendId] = /<fieldset class="billing"([^>]*)><div id="([^"]+)"/.exec(html) ?? []if (!root?.includes(" disabled") || !root.includes(`aria-labelledby="${legendId}"`)) { throw new Error("the fieldset isn't disabled and named by its legend")}if (html.match(/<input [^>]* disabled/g)?.length !== 2) throw new Error("the fields' inputs aren't disabled")Components
A headless fieldset. Call it with the consumer's h, the props, and a
view closure that draws the root with fieldset.root(h.fieldset) and,
inside it, the legend with fieldset.legend(h.div) and the fields.
Details
The parts also return disabled as a boolean, for markup that depends
on it.
The fieldset's own attributes win over the ones the consumer set on the builder before the part, including the ones a state leaves absent:
root:disabled,aria-disabled, which it never sets, anddata-disabled.aria-labelledbyis a token list: the legend's id is added to the consumer's own.legend:idanddata-disabled
A part writes them when it is applied, so an attribute the consumer chains after the part is the consumer's to answer for.
The fieldset shares { disabled } with the components in its view
closure, which is how a Field inside reads it.
Signature
declare const Fieldset: Headless<{ readonly disabled?: boolean;}, never, { disabled: boolean; legend: Part<string, [], never>; root: Part<"fieldset", [], never>;}, never, never, Shared<"Fieldset">, { readonly disabled: boolean;}, never, never, {}, never, QueryEntries<{}, {}>>Models
The props of a Fieldset.
Signature
type FieldsetProps = typeof FieldsetProps.TypeSchemas
FieldsetProps
unstableProps for the state of a Fieldset.
Details
disabled: disables the<fieldset>with the nativedisabledattribute, which disables every control inside it, setsdata-disabledon both parts, and disables eachFieldinside.
Signature
declare const FieldsetProps: Struct<{ readonly disabled: optional<Boolean>;}>