RadioGroup
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/radio_group.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
"github.com/dyike/keel/ui/theme"
)
func init() {
registerSection("radio_group", "inputs", func() core.Widget {
pay := kit.RadioGroup(demoText("Payment method", "付款方式"), demoText("Bank transfer", "转账"), demoText("Check", "支票"), demoText("Cash", "现金"))
pay.SetValue(demoText("Bank transfer", "转账"))
pay.SetOptionDisabled(demoText("Check", "支票"), true)
plans := kit.RadioGroup(demoText("Plan", "套餐"), demoText("Basic", "基础"), demoText("Pro", "专业")).Size(28).TextSize(20)
for _, plan := range []struct{ name, description string }{{demoText("Basic", "基础"), demoText("For personal use", "适合个人使用")}, {demoText("Pro", "专业"), demoText("Includes team collaboration", "支持团队协作")}} {
plans.Content(plan.name, el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().Gap(theme.SpaceXs).Child(el.Text(plan.name).Bold(),
el.Text(plan.description).TextSize(theme.TextSm).TextColor(theme.Muted))
}))
}
size := kit.RadioGroup(demoText("Size", "尺寸"), "S", "M", "L").Horizontal().Size(14).TextSize(12).ItemSize("M", 18, 14).ItemSize("L", 28, 20)
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(16).Items(el.Start).Child(pay.Render(cx), size.Render(cx),
el.Div().Role("radiogroup").Name(demoText("Plan", "套餐")).Gap(12).Child(
el.Div().P(12).Child(plans.Item(demoText("Basic", "基础")).Render(cx)),
el.Div().P(12).Child(plans.Item(demoText("Pro", "专业")).Render(cx))))
}))
})
}
Choose one of several options.
pay := kit.RadioGroup("Payment method", "Bank transfer", "Check", "Cash").OnChange(func(s string) { … })
size := kit.RadioGroup("Size", "S", "M", "L").Horizontal()
- The keyboard behavior is consistent with the native radio group: Tab falls on the currently selected item when entering the group (it falls on the first available item when there is no selection or the selected item is disabled), the arrow keys move and select, and the head and tail cycle. By default, the entire group occupies only one Tab stop; an explicit ItemTab can override this.
SetOptionDisabled(value, bool)disables a single item and retains the existing selection; the arrow keys skip disabled items and Home / End go to the first and last available items. When fully disabled, tab stops are not occupied.Item(value)Returns an independently renderable option, suitable for use in card layouts; all items in the same group share selection, keyboard order, and disabled state. Each value is rendered only once per frame; the outer layer declares theradiogrouprole and name itself. Items that are not rendered or have disabled ancestors do not become arrow key targets.Size(dp)sets the diameter of the dots in the group, the default is 18dp, and the positive value is limited to 12–64dp; theTextSize(sp)setting option inherits the font size, and the positive value is limited to 8–128sp. Passing 0 to both returns to default, negative/non-finite values are ignored; independentItemuses the same configuration.Content(value, view)replaces the visible label of the corresponding option, and can combine title, description, icon and other display content. The original option string is still the value and accessible name; the content should not contain buttons or input boxes. Pass nil to restore text, unknown options are ignored; rearrange the retained content, and remove options to clean up the content. Content is rendered every frame, with explicit font sizes taking precedence over the group's inherited font sizes.Options()returns a copy; passed options are also copied, and null and duplicate values are removed. Reordering preserves option identity.Value()returns the selected item, and is empty when not selected;SetValuedoes not trigger a callback;SetOptionsreplaces the option, and clears it when the original selection is not in the new option;SetDisabled.
Agent: The role of the group is radiogroup, each option is radio, and checked indicates whether it is selected.
Verify: go run ./examples/components -section radio_group, add -theme dark to check the dark theme.
Rich tag example:
plans := kit.RadioGroup("Plan", "Basic", "Pro").Size(28).TextSize(20)
plans.ItemSize("Basic", 18, 14)
plans.Content("Pro", el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().Child(el.Text("Pro plan").Bold(),
el.Text("Includes team collaboration").TextSize(theme.TextSm).TextColor(theme.Muted))
}))
The default is to use a single tab stop within the group. TabStop(false) or TabIndex(-1) can skip the entire group, still allowing mouse and arrow key selection; non-negative TabIndex is arranged in ascending order, the same value is in tree order, and the range is limited to el root. ItemTab(value, stop, index) can overwrite the single-item configuration, which explicitly participates in docking judgment and is no longer restricted by the default single docking point; ClearItemTab(value) restores the group configuration. Item-by-item coverage, rearrangement and retention, deletion and cleaning, unknown options are ignored. Disabled items can never be docked; Tab movement itself does not change the selected value.
ItemSize(value, dp, sp) can overwrite dots and font sizes item by item. When each parameter is 0, the group configuration is inherited; ItemSize(value, 0, 0) clears the override. Positive values inherit the upper and lower bounds of the group, unknown options or any negative/non-finite values ignore the entire call. Rearranging retains coverage, and deleting options cleans up. Independent Items also work.
See Radio for a single, freely positioned radio button.