Headless componentSource
Fieldset
Groups several fields under one legend, and disables all of them at once.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"
const fields = [
{ name: "company", label: "Company", placeholder: "Effectful Technologies" },
{ name: "taxId", label: "Tax ID", placeholder: "IT 0123456789" }
] as const
const FieldsetBasicMessage = UiSchema.TaggedEnum({
UpdatedField: { name: Schema.Literals(["company", "taxId"]), value: Schema.String }
})
export const FieldsetBasic = Html.component({
name: "FieldsetBasic",
model: Schema.Struct({ company: Schema.String, taxId: Schema.String }),
message: FieldsetBasicMessage
}).make({
init: () => ({ company: "", taxId: "" }),
update: {
UpdatedField: ($, { name, value }) => $.evolve({ [name]: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
Fieldset(h, {
view: (fieldset) =>
FieldsetRoot(h, fieldset).children(
FieldsetLegend(h, fieldset).children("Billing details"),
fields.map(({ label, name, placeholder }) =>
Field(h, {
key: name,
view: (field) =>
FieldRoot(h, field).children(
FieldLabel(h, field).children(label),
Input(h, {
element: field.control(h.input),
props: { value: model[name] },
onUpdate: ({ value }) => FieldsetBasicMessage.UpdatedField({ name, value })
}).attrs({ name, placeholder })
)
})
)
)
})
)
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.Copy the component into your project
The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
src/ui/fieldset.ts import type { Component } from "@effect/ui" import type { Html } from "@effect/ui/html" import type { Fieldset } from "@effect/ui-kit" import { cn } from "#lib/cn" /** What the kit's `Fieldset` hands its `view` closure. */ export type FieldsetParts = Component.Headless.Parts<typeof Fieldset> interface ClassOption { readonly class?: string | undefined } const variants = { default: "border-0 p-0", card: "rounded-lg border border-border bg-card p-5" } as const export type FieldsetVariant = keyof typeof variants /** The `<fieldset>`: a column of fields under one legend. `card` draws a border around them. */ export const FieldsetRoot = <Msg>( h: Html.H<Msg>, fieldset: FieldsetParts, options: ClassOption & { readonly variant?: FieldsetVariant | undefined } = {} ) => fieldset.root( h.fieldset.class(cn("m-0 flex min-w-0 flex-col gap-4", variants[options.variant ?? "default"], options.class)) ) /** The fieldset's name. Always draw it: the root points at it. */ export const FieldsetLegend = <Msg>(h: Html.H<Msg>, fieldset: FieldsetParts, options: ClassOption = {}) => fieldset.legend(h.div.class(cn("text-sm font-semibold data-[disabled]:opacity-50", options.class))) /** A sentence under the legend. Point the root's `aria-describedby` at its id. */ export const FieldsetDescription = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.p.class(cn("m-0 mt-1 text-[13px] text-muted-foreground", options.class))Import it
import { FieldsetRoot, FieldsetLegend, FieldsetDescription } from "#ui/fieldset"
Usage
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"
const fields = [
{ name: "company", label: "Company", placeholder: "Effectful Technologies" },
{ name: "taxId", label: "Tax ID", placeholder: "IT 0123456789" }
] as const
const FieldsetBasicMessage = UiSchema.TaggedEnum({
UpdatedField: { name: Schema.Literals(["company", "taxId"]), value: Schema.String }
})
export const FieldsetBasic = Html.component({
name: "FieldsetBasic",
model: Schema.Struct({ company: Schema.String, taxId: Schema.String }),
message: FieldsetBasicMessage
}).make({
init: () => ({ company: "", taxId: "" }),
update: {
UpdatedField: ($, { name, value }) => $.evolve({ [name]: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
Fieldset(h, {
view: (fieldset) =>
FieldsetRoot(h, fieldset).children(
FieldsetLegend(h, fieldset).children("Billing details"),
fields.map(({ label, name, placeholder }) =>
Field(h, {
key: name,
view: (field) =>
FieldRoot(h, field).children(
FieldLabel(h, field).children(label),
Input(h, {
element: field.control(h.input),
props: { value: model[name] },
onUpdate: ({ value }) => FieldsetBasicMessage.UpdatedField({ name, value })
}).attrs({ name, placeholder })
)
})
)
)
})
)
})
Examples
Disabled
One prop disables every field inside. Each Field reads the fieldset, so nothing is passed to it.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"
const fields = [
{ name: "name", label: "Name" },
{ name: "email", label: "Email" }
] as const
const FieldsetDisabledMessage = UiSchema.TaggedEnum({
UpdatedField: { name: Schema.Literals(["name", "email"]), value: Schema.String },
PressedEdit: {},
PressedSave: {}
})
export const FieldsetDisabled = Html.component({
name: "FieldsetDisabled",
model: Schema.Struct({ name: Schema.String, email: Schema.String, editing: Schema.Boolean }),
message: FieldsetDisabledMessage
}).make({
init: () => ({ name: "Ada Lovelace", email: "ada@example.com", editing: false }),
update: {
UpdatedField: ($, { name, value }) => $.evolve({ [name]: value }),
PressedEdit: ($) => $.evolve({ editing: true }),
PressedSave: ($) => $.evolve({ editing: false })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
// Each Field inside reads the fieldset, so nothing is passed to it.
Fieldset(h, {
props: { disabled: !model.editing },
view: (fieldset) =>
FieldsetRoot(h, fieldset).children(
FieldsetLegend(h, fieldset).children("Profile"),
fields.map(({ label, name }) =>
Field(h, {
key: name,
view: (field) =>
FieldRoot(h, field).children(
FieldLabel(h, field).children(label),
Input(h, {
element: field.control(h.input),
props: { value: model[name] },
onUpdate: ({ value }) => FieldsetDisabledMessage.UpdatedField({ name, value })
}).attr("name", name)
)
})
)
)
}),
h.div.children(
model.editing
? Button(h, { size: "sm", onPress: FieldsetDisabledMessage.PressedSave() })
.children("Save")
: Button(h, { variant: "outline", size: "sm", onPress: FieldsetDisabledMessage.PressedEdit() })
.children("Edit profile")
)
)
})
Card with a description
The card variant, a description under the legend and the fields in a grid.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetDescription, FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"
const FieldName = Schema.Literals(["first", "last", "city", "postcode"])
const fields: ReadonlyArray<{ readonly name: typeof FieldName.Type; readonly label: string }> = [
{ name: "first", label: "First name" },
{ name: "last", label: "Last name" },
{ name: "city", label: "City" },
{ name: "postcode", label: "Postcode" }
]
const FieldsetCardMessage = UiSchema.TaggedEnum({
UpdatedField: { name: FieldName, value: Schema.String }
})
export const FieldsetCard = Html.component({
name: "FieldsetCard",
model: Schema.Struct({
first: Schema.String,
last: Schema.String,
city: Schema.String,
postcode: Schema.String
}),
message: FieldsetCardMessage
}).make({
init: () => ({ first: "Ada", last: "Lovelace", city: "London", postcode: "" }),
update: {
UpdatedField: ($, { name, value }) => $.evolve({ [name]: value })
},
view: (h, model) =>
Fieldset(h, {
view: (fieldset) =>
FieldsetRoot(h, fieldset, { variant: "card", class: "w-full max-w-110" })
.attr("aria-describedby", "fieldset-card-description")
.children(
h.div.children(
FieldsetLegend(h, fieldset).children("Invoice recipient"),
FieldsetDescription(h).attr("id", "fieldset-card-description").children(
"Printed at the top of every invoice we send you."
)
),
h.div.class("grid grid-cols-2 gap-4").children(
fields.map(({ label, name }) =>
Field(h, {
key: name,
props: { filled: model[name] !== "" },
view: (field) =>
FieldRoot(h, field).children(
FieldLabel(h, field).children(label),
Input(h, {
element: field.control(h.input),
props: { value: model[name] },
onUpdate: ({ value }) => FieldsetCardMessage.UpdatedField({ name, value })
}).attr("name", name)
)
})
)
)
)
})
})
Nested
A fieldset inside another is disabled while the outer one is.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field, Fieldset } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldLabel, FieldRoot } from "#ui/field"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import { Input } from "#ui/input"
const FieldName = Schema.Literals(["street", "city", "contact", "phone"])
const FieldsetNestedMessage = UiSchema.TaggedEnum({
UpdatedField: { name: FieldName, value: Schema.String },
ToggledShipping: { checked: Schema.Boolean },
ToggledContact: { checked: Schema.Boolean }
})
export const FieldsetNested = Html.component({
name: "FieldsetNested",
model: Schema.Struct({
street: Schema.String,
city: Schema.String,
contact: Schema.String,
phone: Schema.String,
shipping: Schema.Boolean,
separateContact: Schema.Boolean
}),
message: FieldsetNestedMessage
}).make({
init: () => ({
street: "",
city: "",
contact: "",
phone: "",
shipping: true,
separateContact: false
}),
update: {
UpdatedField: ($, { name, value }) => $.evolve({ [name]: value }),
ToggledShipping: ($, { checked }) => $.evolve({ shipping: checked }),
ToggledContact: ($, { checked }) => $.evolve({ separateContact: checked })
},
view: (h, model) => {
const text = (name: typeof FieldName.Type, label: string) =>
Field(h, {
key: name,
view: (field) =>
FieldRoot(h, field).children(
FieldLabel(h, field).children(label),
Input(h, {
element: field.control(h.input),
props: { value: model[name] },
onUpdate: ({ value }) => FieldsetNestedMessage.UpdatedField({ name, value })
}).attr("name", name)
)
})
return h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.label.class("flex items-center gap-2 text-[13px]").children(
h.input.class("size-3.5 accent-brand").attr("type", "checkbox").checked(model.shipping)
.on("change", ({ checked }) => FieldsetNestedMessage.ToggledShipping({ checked })),
"Ship this order"
),
Fieldset(h, {
key: "shipping",
props: { disabled: !model.shipping },
view: (outerFieldset) =>
FieldsetRoot(h, outerFieldset, { variant: "card" }).children(
FieldsetLegend(h, outerFieldset).children("Shipping address"),
text("street", "Street"),
text("city", "City"),
h.label.class("flex items-center gap-2 text-[13px]").children(
h.input.class("size-3.5 accent-brand").attr("type", "checkbox").checked(model.separateContact)
.on("change", ({ checked }) => FieldsetNestedMessage.ToggledContact({ checked })),
"Someone else receives it"
),
// The inner fieldset is disabled while the outer one is, whatever its own prop says.
Fieldset(h, {
key: "contact",
props: { disabled: !model.separateContact },
view: (innerFieldset) =>
FieldsetRoot(h, innerFieldset).children(
FieldsetLegend(h, innerFieldset).children("Delivery contact"),
text("contact", "Name"),
text("phone", "Phone")
)
})
)
})
)
}
})
Controls without a field
A slider and a number field take disabled from fieldset.disabled.
Playback
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Fieldset, NumberField, Slider } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldsetLegend, FieldsetRoot } from "#ui/fieldset"
import {
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
NumberFieldLabel,
NumberFieldRoot
} from "#ui/number-field"
import {
SliderControl,
SliderHeader,
SliderIndicator,
SliderLabel,
SliderRoot,
SliderThumb,
SliderTrack,
SliderValue
} from "#ui/slider"
const FieldsetControlsMessage = UiSchema.TaggedEnum({
ToggledOverride: { checked: Schema.Boolean },
MovedSlider: { value: Schema.Number },
UpdatedNumberField: { value: Schema.NullOr(Schema.Number) }
})
export const FieldsetControls = Html.component({
name: "FieldsetControls",
model: Schema.Struct({
override: Schema.Boolean,
volume: Schema.Number,
speed: Schema.NullOr(Schema.Number)
}),
message: FieldsetControlsMessage
}).make({
init: () => ({ override: false, volume: 60, speed: 1 }),
update: {
ToggledOverride: ($, { checked }) => $.evolve({ override: checked }),
MovedSlider: ($, { value }) => $.evolve({ volume: value }),
UpdatedNumberField: ($, { value }) => $.evolve({ speed: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.label.class("flex items-center gap-2 text-[13px]").children(
h.input.class("size-3.5 accent-brand").attr("type", "checkbox").checked(model.override)
.on("change", ({ checked }) => FieldsetControlsMessage.ToggledOverride({ checked })),
"Override the workspace defaults"
),
Fieldset(h, {
props: { disabled: !model.override },
view: (fieldset) =>
FieldsetRoot(h, fieldset, { variant: "card" }).children(
FieldsetLegend(h, fieldset).children("Playback"),
// A control with no Field around it is disabled by the browser alone,
// so its own parts take `disabled: fieldset.disabled`.
Slider(h, {
props: { value: model.volume, disabled: fieldset.disabled },
onEmit: {
Moved: ({ value }) =>
typeof value === "number" ? FieldsetControlsMessage.MovedSlider({ value }) : undefined,
Committed: () => undefined
},
view: (slider) =>
SliderRoot(h, slider).children(
SliderHeader(h, slider)
.children(
SliderLabel(h).attr("for", "fieldset-volume").children("Volume"),
SliderValue(h, slider).children(`${model.volume}%`)
),
SliderControl(h, slider).children(
SliderTrack(h, slider).children(
SliderIndicator(h, slider),
SliderThumb(h, slider, {
input: h.input.attrs({ id: "fieldset-volume", name: "volume" })
})
)
)
)
}),
NumberField(h, {
props: {
value: model.speed,
min: 0.25,
max: 3,
step: 0.25,
name: "speed",
locale: "en-US",
disabled: fieldset.disabled
},
onEmit: {
Updated: ({ value }) => FieldsetControlsMessage.UpdatedNumberField({ value }),
Committed: () => undefined
},
view: (numberField) =>
NumberFieldRoot(h, numberField).children(
NumberFieldLabel(h, numberField).attr("for", "fieldset-speed").children("Speed"),
NumberFieldGroup(h, numberField).children(
NumberFieldDecrement(h, numberField),
NumberFieldInput(h, numberField, { element: h.input.attr("id", "fieldset-speed") }),
NumberFieldIncrement(h, numberField)
)
)
})
)
})
)
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
root | The <fieldset>. Its aria-labelledby points at the legend. |
legend | Any element, usually a <div>. Always draw it. |
disabled | A boolean, for a control inside that is drawn without a Field. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
disabled | boolean | Sets the native disabled on the <fieldset>, which disables every control inside, and disables each Field inside. |
Data attributes
| Attribute | Description |
|---|---|
data-disabled | On the root and the legend, while the fieldset or one around it is disabled. |
Related components
The components this page's file and examples import.
- Button: A native button that can stay focusable while disabled.
- Field: Groups a control with its label, description and error, and reports whether it is invalid, touched or focused on each of them.
- Input: A native text input whose value lives in your Model.
- NumberField: A controlled input for a number, with stepper buttons, locale formatting and an area to drag across.
- Slider: A controlled slider with one thumb or several, dragged or moved with the keyboard.