Skip to content

Radio Button

A radio button is one option of a group in which exactly one option is selected. Create the group with AutoRadioStateGroup, which keeps one *core.State[bool] per option and clears all others when one is selected, and bind each radio button to its state. In forms you usually want the Radio Button Field, which adds a label.

Radio Button

group := AutoRadioStateGroup(wnd, "colors", 3).InitIndex(0)

HStack(
	Each2(group.All(), func(idx int, checked *core.State[bool]) core.View {
		return RadioButton(checked.Get()).InputChecked(checked)
	})...,
).Gap(L16)

Constructors

ConstructorDescription
func RadioButton(checked bool) TRadioButtonCreates a radio button with the given value.
func AutoRadioStateGroup(wnd core.Window, id string, states int) RadioStateGroupCreates a window-scoped group of states boolean states of which at most one is true.

Methods

MethodDescription
Disabled(disabled bool) TRadioButtonDisabled disables the radio button when set to true, preventing user interaction.
ID(id string) TRadioButtonAssigns a unique identifier to the radio button.
InputChecked(input *core.State[bool]) TRadioButtonInputChecked binds the radio button to the given state, enabling two-way data binding so that the selected state is synchronized with external logic.
Name(name string) TRadioButtonName defines the name of the checkbox.
Value(checked bool) TRadioButtonValue sets the initial selected state of the radio button.
Visible(v bool) TRadioButtonVisible controls the visibility of the radio button.

RadioStateGroup

MethodDescription
All() iter.Seq2[int, *core.State[bool]]All iterates over all states in the group, yielding (index, state) pairs.
InitIndex(idx int) RadioStateGroupInitIndex allows to initialize the radio group with a specific index.
Notify()Notify triggers observers of all states in the group.
Observe(f func(newIdx int))Observe registers a callback that is invoked with the currently selected index whenever any state in the group changes.
SelectedIndex() intSelectedIndex returns -1 or the selected index.
SetSelectedIndex(idx int)SetSelectedIndex marks the state at idx as selected (true) and clears all others.

Related