ButtonGroup
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/button_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("button_group", "general", buttonGroupGallery) }
func buttonGroupGallery() core.Widget {
// The selected view follows clicks.
views := kit.ButtonGroup().Name(demoText("View", "视图"))
for i, label := range []string{demoText("List", "列表"), demoText("Board", "看板"), demoText("Calendar", "日历")} {
views.Add(kit.Button(label, func() {
for j, b := range views.Buttons() {
b.SetSelected(j == i)
}
}).Variant(kit.ButtonSecondary).Selected(i == 0))
}
pages := kit.ButtonGroup(kit.Button(demoText("Previous page", "上一页"), nil).Outline(true), kit.Button("1", nil).Outline(true).Selected(true),
kit.Button("2", nil).Outline(true), kit.Button(demoText("Next page", "下一页"), nil).Outline(true)).Name(demoText("Pagination", "分页"))
save := kit.ButtonGroup(kit.Button(demoText("Save", "保存"), nil), kit.Button("", nil).Name(demoText("More save options", "更多保存选项")).Icon(kit.IconChevronDown)).Name(demoText("Save", "保存"))
order := kit.ButtonGroup(kit.Button(demoText("Pin", "置顶"), nil).Variant(kit.ButtonSecondary), kit.Button(demoText("Move up", "上移"), nil).Variant(kit.ButtonSecondary),
kit.Button(demoText("Move down", "下移"), nil).Variant(kit.ButtonSecondary)).Vertical(true).Name(demoText("Sort", "排序"))
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(16).Items(el.Start).Child(
el.Text(demoText("ButtonGroup", "按钮组")).TextSize(24).Bold(),
el.Text(demoText("Only outer corners are rounded, with a narrow gap between buttons.", "只圆外侧的角,按钮之间留细缝。")).TextColor(theme.Muted),
views.Render(cx), pages.Render(cx), save.Render(cx), order.Render(cx))
}))
}
kit.ButtonGroup(
kit.Button("Previous page", prev).Outline(true),
kit.Button("Next page", next).Outline(true),
).Name("Pagination")
- Several buttons are connected into one control: only the outer corners are rounded, and a 1dp background gap separates every neighbour without drawing dividers or strokes.
- Each button retains its own click, icon, loaded, and selected states;
Buttons()removes the button for later modification. Vertical(true)is arranged vertically, with the upper and lower ends of the circle;SetDisabled(true)disables all buttons in the group.- The Agent role is
groupand its name comes fromName.
Verification: go run ./examples/components -section button_group.