Checkbox
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/checkbox.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"
)
func init() {
registerSection("checkbox", "inputs", func() core.Widget {
all, a, b := kit.Checkbox(demoText("Select all", "全选"), false), kit.Checkbox(demoText("Order SO-1001", "订单 SO-1001"), true), kit.Checkbox(demoText("Order SO-1002", "订单 SO-1002"), false)
sync := func() {
n := 0
for _, c := range []*kit.CheckboxView{a, b} {
if c.Value() {
n++
}
}
all.SetValue(n == 2)
all.SetMixed(n == 1)
}
a.OnChange(func(bool) { sync() })
b.OnChange(func(bool) { sync() })
all.OnChange(func(on bool) { a.SetValue(on); b.SetValue(on) })
sync()
off := kit.Checkbox(demoText("Unavailable", "不可用"), true)
off.SetDisabled(true)
small := kit.Checkbox(demoText("Small", "小尺寸"), true).Size(14).TextSize(12)
large := kit.Checkbox(demoText("Large · Indeterminate", "大尺寸 · 半选"), false).Size(28).TextSize(20)
large.SetMixed(true)
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(10).Items(el.Start).Child(all.Render(cx), el.Div().Pl(24).Gap(8).Child(a.Render(cx), b.Render(cx)), off.Render(cx), small.Render(cx), large.Render(cx))
}))
})
}
A labeled checkbox will toggle when clicked, Space, or Enter.
agree := kit.Checkbox("Accept the terms", false).OnChange(func(on bool) { … })
all.SetMixed(true) // "Select All" displays half selection when partially selected
Value()/SetValue(bool)reads or sets whether it is checked or not.SetValuedoes not trigger a callback and clears the half-selected state.SetMixed(true)displays half-selected and becomes checked when clicked.- It cannot be operated after
SetDisabled(true), and the text can be modified afterSetLabel. - Checkbox is used for options that require the user to click "Submit" to take effect; Switch is used for switches that take effect immediately.
Agent: Role checkbox, checked indicates whether to check or not; when half-selected, value becomes mixed.
Verify: go run ./examples/components -section checkbox, add -theme dark to check the dark theme.
Size(dp) sets the box side length, positive values are limited to 12–64dp, 0 returns to the default 18dp; checkmarks and half-selected horizontal lines scale simultaneously. TextSize(sp) sets the label font size individually, positive values are limited to 8–128sp, 0 restores the parent element font size. Negative and non-finite values are ignored; modifying the appearance does not change the value, half-selected state, or existing focus.
agree.Size(24).TextSize(18).TabIndex(2)
TabStop(false) Just skips tab traversal and still has mouse and program focus. TabIndex(n) traverses in ascending order, keeping the same value in tree order, and skipping negative values. Reuse el's single root rule, loop independently within the overlay, and do not sort across independent Embed or native Gio controls. The focus outline is still drawn along the entire line of labels and boxes.