RadioGroup unstable
A controlled group of radio buttons. Pass the selected value as value,
the group's options as items, and update value when the group emits
Picked. Your view supplies the markup; the component owns roving focus,
arrow-key navigation, ARIA and data attributes, indicator transitions and
the form field.
Details
RadioGroup is a headless component: it keeps the highlighted radio and
the indicator transitions, and moves focus with a Command.
Anatomy:
| Part | Element |
|---|---|
root |
<div role="radiogroup"> |
radio |
<button role="radio"> for each item |
indicator |
<span> inside a radio, shown while it's picked |
root takes the consumer's builder and returns it; radio and
indicator take an options object with element and value (and
keepMounted on indicator). indicator returns undefined while its
radio is unchecked and not leaving, unless keepMounted is true, so
chain children on it with ?.:
radioGroup.indicator({ element: h.span, value })?.children(...).
When name is set, a value is picked, and neither the group nor the
checked item is disabled, root appends a hidden <input> after any
children already on the div and before children chained afterward.
Name the group with aria-label, aria-labelledby, or a Field's
field.group and field.legend. Name each radio with a wrapping
<label>, a sibling <label for>, aria-label, or text inside the
button. Inside a Field, apply field.group on the root, field.member
on each radio, and pass disabled: field.disabled and
invalid: field.invalid: radio removes the aria-invalid that
field.member sets so validity lives on the radiogroup.
Data attributes:
Each present with an empty value or absent:
root:data-disabled,data-readonly,data-requiredradioandindicator:data-checked/data-unchecked,data-disabled,data-readonly,data-requiredindicator:data-starting-stylefor the first frame after its radio becomes checked, anddata-ending-stylewhile it stays mounted for exit animations after its radio becomes unchecked
Keyboard:
One radio in the group sits in the tab order (tabindex="0"); the rest
have tabindex="-1". Down and Right move focus to the next enabled radio
and pick it; Up and Left move to the previous one, wrapping at the ends.
In a right-to-left layout, Left and Right swap.
In a read-only group or on a read-only item, arrow keys move focus and
the tab stop without emitting Picked. Space picks the focused radio
through the native button click; Enter is cancelled. Arrow keys with
Ctrl, Alt or Meta held are left alone.
Example A labelled radio group whose value lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { RadioGroup } from "effect/ui/kit"import { Effect, Schema } from "effect"
const storage = [ { value: "ssd", label: "SSD" }, { value: "hdd", label: "HDD" }]
const PickerMessage = UiSchema.TaggedEnum({ PickedStorage: { value: Schema.String } })
const Picker = Html.component({ name: "Picker", model: Schema.Struct({ storage: Schema.NullOr(Schema.String) }), message: PickerMessage}).make({ init: () => ({ storage: "ssd" }), update: { PickedStorage: ($, { value }) => $.evolve({ storage: value }) }, view: (h, model) => RadioGroup(h, { props: { value: model.storage, items: storage, name: "storage" }, onEmit: { Picked: ({ value }) => PickerMessage.PickedStorage({ value }) }, view: (radioGroup) => radioGroup.root(h.div.class("group").attr("aria-label", "Storage")).children( storage.map(({ label, value }) => h.label.children( radioGroup.radio({ element: h.button.class("radio"), value }).children( radioGroup.indicator({ element: h.span.class("dot"), value }) ), label ) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Picker, {}))if (!/<div class="group"[^>]* role="radiogroup"/.test(html)) throw new Error("the root isn't a styled radiogroup")if (!/<input type="hidden" name="storage" value="ssd"/.test(html)) throw new Error("the hidden input is missing")Components
makeRadioGroup
unstableCreates a radio group whose selected value, items, value-taking parts and
Picked event use the supplied string-valued schema. Picked returns an
item value, not an unchecked DOM string.
Details
The schema must encode to a string. Create the component once at module
level: each call creates a new component. The existing RadioGroup export
uses Schema.String.
Signature
declare function makeRadioGroup<V extends string>(options: { readonly value: Codec<V, string>;}): Headless<{ readonly disabled?: boolean; readonly form?: string; readonly invalid?: boolean; readonly items: readonly Array<{ readonly disabled?: boolean; readonly readOnly?: boolean; value: V; }>; readonly name?: string; readonly readOnly?: boolean; readonly required?: boolean; readonly value: V | null;}, { readonly _tag: "Picked"; value: V;}, { disabled: boolean; indicator: <B extends PartTarget<"span">>(__namedParameters: { readonly element: B; readonly keepMounted?: boolean; readonly value: V; }) => ReturnType<B["attr"]> | undefined & Pick<Part<"span", [value: V], never>, "~R">; invalid: boolean; radio: <B extends KeyedButton>(__namedParameters: { readonly element: B; readonly value: V; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"button"> & { readonly key: (key: string) => PartTarget<"button">; }>(element: B, value: V, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<"button", [value: V], never>, "~R">, "~R">; readOnly: boolean; required: boolean; root: Part<"div", [], never>; value: V | null;}, Exclude<PartRequirements<V>, FrameworkService>, never, never, never, never, never, { readonly current: string | null; readonly highlighted: string | null; readonly leaving: readonly Array<string>; readonly mounted: readonly Array<string>; readonly rtl: boolean; readonly settled: string | null;}, { readonly _tag: "PressedEnter";} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "ClickedRadio"; readonly value: string;} | { readonly _tag: "FocusedRadio"; readonly value: string;} | { readonly _tag: "PressedArrow"; readonly direction: -1 | 1; readonly value: string;} | { readonly _tag: "SyncedValue"; readonly value: string | null;} | { readonly _tag: "MountedIndicator"; readonly value: string;} | { readonly _tag: "UnmountedIndicator"; readonly value: string;} | { readonly _tag: "StartedIndicator"; readonly value: string;} | { readonly _tag: "EndedIndicator"; readonly value: string;}, QueryEntries<{}, {}>>Example A radio group typed by a literal union
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { makeRadioGroup } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Scope = Schema.Literals(["All", "League", "Cup"])const ScopeRadioGroup = makeRadioGroup({ value: Scope })const items = Scope.literals.map((value) => ({ value }))
const Message = UiSchema.TaggedEnum({ PickedScope: { scope: Scope } })
const Filter = Html.component({ name: "Filter", model: Schema.Struct({ scope: Scope }), message: Message}).make({ init: () => ({ scope: "All" as const }), update: { PickedScope: ($, { scope }) => $.evolve({ scope }) }, view: (h, model) => ScopeRadioGroup(h, { props: { value: model.scope, items }, // `value` is "All" | "League" | "Cup" onEmit: { Picked: ({ value }) => Message.PickedScope({ scope: value }) }, view: (radioGroup) => radioGroup.root(h.div.attr("aria-label", "Scope")).children( items.map(({ value }) => radioGroup.radio({ element: h.button, value }).children(value) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Filter, {}))if (!html.includes('aria-checked="true"')) throw new Error("no radio is checked")RadioGroup
unstableA headless radio group. Call it with the consumer's h, the props,
onEmit, and a view closure that draws radioGroup.root(h.div) with
radioGroup.radio({ element: h.button, value }) and optional
radioGroup.indicator({ element: h.span, value }) children.
Details
The parts object also exposes value, disabled, readOnly,
required and invalid for markup that depends on the group's state.
Each part's attributes win over values the consumer set on the builder
before applying the part, including attributes a state leaves absent: on
root, role, aria-required, aria-disabled, aria-readonly,
aria-invalid and the data attributes; on radio, type, role,
aria-checked, aria-disabled, aria-invalid, disabled, tabindex
and the data attributes; on indicator, aria-hidden="true", the radio's
data attributes, data-starting-style and data-ending-style.
The hidden indicator's content stays out of the radio's accessible name.
radio and indicator throw when given a value that is not in
items. The group follows the nearest DirectionProvider above it or,
without one, the root's CSS direction.
History restore saves nothing of the group's transient focus or indicator
transition state: after Back, forward or a reload it starts from init.
Signature
declare const RadioGroup: Headless<{ readonly disabled?: boolean; readonly form?: string; readonly invalid?: boolean; readonly items: readonly Array<{ readonly disabled?: boolean; readonly readOnly?: boolean; value: string; }>; readonly name?: string; readonly readOnly?: boolean; readonly required?: boolean; readonly value: string | null;}, { readonly _tag: "Picked"; value: string;}, { disabled: boolean; indicator: <B extends PartTarget<"span">>(__namedParameters: { readonly element: B; readonly keepMounted?: boolean; readonly value: string; }) => ReturnType<B["attr"]> | undefined & Pick<Part<"span", [value: string], never>, "~R">; invalid: boolean; radio: <B extends KeyedButton>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"button"> & { readonly key: (key: string) => PartTarget<"button">; }>(element: B, value: string, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<"button", [value: string], never>, "~R">, "~R">; readOnly: boolean; required: boolean; root: Part<"div", [], never>; value: string | null;}, never, never, never, never, never, never, { readonly current: string | null; readonly highlighted: string | null; readonly leaving: readonly Array<string>; readonly mounted: readonly Array<string>; readonly rtl: boolean; readonly settled: string | null;}, { readonly _tag: "PressedEnter";} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "ClickedRadio"; readonly value: string;} | { readonly _tag: "FocusedRadio"; readonly value: string;} | { readonly _tag: "PressedArrow"; readonly direction: -1 | 1; readonly value: string;} | { readonly _tag: "SyncedValue"; readonly value: string | null;} | { readonly _tag: "MountedIndicator"; readonly value: string;} | { readonly _tag: "UnmountedIndicator"; readonly value: string;} | { readonly _tag: "StartedIndicator"; readonly value: string;} | { readonly _tag: "EndedIndicator"; readonly value: string;}, QueryEntries<{}, {}>>Models
What a RadioGroup emits.
Signature
type RadioGroupEvents<V extends string = string> = ReturnType<typeof eventsSchema>["Type"]One option in a RadioGroup.
Signature
type RadioGroupItem<V extends string = string> = ReturnType<typeof itemSchema>["Type"]The props of a RadioGroup.
Signature
type RadioGroupProps<V extends string = string> = ReturnType<typeof propsSchema>["Type"]Schemas
RadioGroupEvents
unstablePicked asks for the radio the user chose. Store value and pass it
back as the value prop to update the group.
Signature
declare const RadioGroupEvents: TaggedEnum<{ Picked: TaggedStruct<"Picked", { value: String; }>;}>RadioGroupItem
unstableOne option in a RadioGroup.
Details
value: unique string identifying the radio in the group.disabled: disables this radio, skips it during arrow-key navigation and prevents it from submitting a form value.readOnly: keeps this radio focusable while preventing clicks and arrow keys from picking it.
Signature
declare const RadioGroupItem: Struct<{ readonly disabled: optional<Boolean>; readonly readOnly: optional<Boolean>; value: String;}>RadioGroupProps
unstableProps for controlling a RadioGroup and the value it submits.
Details
value: the currently picked radio'svalue, ornullwhen none is picked. Required: the consumer owns it.items: the radios in navigation order, each with itsvalueand optionaldisabledandreadOnlyflags.disabled: disables every radio in the group and prevents form submission.readOnly: keeps radios focusable and navigable with arrow keys while preventing any pick from emittingPicked.required: setsaria-requiredon the root anddata-requiredon every part.invalid: setsaria-invalidon the root.nameandform: the hidden form input's attributes when a non-disabled radio is picked.
Signature
declare const RadioGroupProps: Struct<{ readonly disabled: optional<Boolean>; readonly form: optional<String>; readonly invalid: optional<Boolean>; readonly items: $Array<Struct<{ readonly disabled: optional<Boolean>; readonly readOnly: optional<Boolean>; value: String; }>>; readonly name: optional<String>; readonly readOnly: optional<Boolean>; readonly required: optional<Boolean>; readonly value: NullOr<String>;}>