Checkbox 多选框
在一组可选项中进行多项选择,或单独用于两种状态之间的切换(例如“同意协议”)。包含单个多选框 Checkbox、多选框组 CheckboxGroup 和带“全选”的 CheckAll。
何时使用
- 在一组选项中选择多个值,例如兴趣、标签、权限。
- 单独使用表示勾选或不勾选,通常需要用户再点击“提交”才生效。
- 需要切换后立即生效的开关(例如“开启通知”),用 Switch 开关。
- 只能选一个值时用 Radio 单选框;选项很多时用多选模式的 Select 选择器。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var agree bool
func page(c *ui.Context) {
ui.Checkbox(c, "我已阅读并同意用户协议", &agree)
ui.Button(c, "注册").Primary().Disabled(!agree)
}Checkbox 绑定一个 *bool,点击标签或方框都会切换;Changed() 报告本帧是否发生切换。
常见用法
多选框组
CheckboxGroup 把选中项的 Value 绑定到 *[]string。选项用 []ui.Option 描述(Label 显示、Value 存储、Disabled 单项禁用),ui.Opts 可以从字符串快速生成值与标签相同的选项。
import (
"strings"
"github.com/tangoui/tango/ui"
)
var langs = []string{"go"}
func page(c *ui.Context) {
opts := []ui.Option{
{Label: "Go", Value: "go"},
{Label: "Rust", Value: "rust"},
{Label: "TypeScript", Value: "ts"},
{Label: "Swift(暂不支持)", Value: "swift", Disabled: true},
}
if ui.CheckboxGroup(c, &langs, opts).A11yLabel("语言").Changed() {
ui.Message(c, "info", "已选:"+strings.Join(langs, ", "))
}
}全选
CheckAll 在多选框组上方加一个“全选”多选框:部分选中时显示半选状态,点击时全选或全不选;被禁用的选项保持原状态。
import (
"github.com/tangoui/tango/ui"
)
var fruits = []string{"苹果"}
func page(c *ui.Context) {
ui.CheckAll(c, &fruits, []ui.Option{
{Label: "苹果", Value: "苹果"},
{Label: "香蕉", Value: "香蕉"},
{Label: "橙子", Value: "橙子"},
{Label: "葡萄(禁用)", Value: "葡萄", Disabled: true},
})
}需要自己摆放“全选”时,用单个 Checkbox 加 Indeterminate 自行实现:
import (
"github.com/tangoui/tango/ui"
)
var (
all = ui.Opts("北京", "上海", "广州")
checked []string
)
func page(c *ui.Context) {
full := len(checked) == len(all)
if ui.Checkbox(c, "全部城市", &full).Indeterminate(len(checked) > 0 && !full).Changed() {
checked = checked[:0]
if full {
for _, o := range all {
checked = append(checked, o.Value)
}
}
}
ui.CheckboxGroup(c, &checked, all).A11yLabel("城市")
}选择数量限制
Min(n) 保证至少选中 n 项(只剩 n 项时无法再取消),Max(n) 最多选 n 项(达到上限后其余选项变为禁用)。
import (
"github.com/tangoui/tango/ui"
)
var picks = []string{"Go"}
func page(c *ui.Context) {
ui.CheckboxGroup(c, &picks, ui.Opts("Go", "Rust", "Zig", "C")).Min(1).Max(2).A11yLabel("语言(1–2 项)")
}边框与按钮样式
Border() 给多选框加边框(Element Plus el-checkbox border),Buttons() 把选项组渲染成相连的切换按钮(el-checkbox-button)。Size 对两种样式都生效。
import (
"github.com/tangoui/tango/ui"
)
var (
bordered bool
cities = []string{"上海"}
)
func page(c *ui.Context) {
ui.Checkbox(c, "带边框", &bordered).Border()
ui.CheckboxGroup(c, &cities, ui.Opts("北京", "上海", "广州", "深圳")).Buttons().A11yLabel("城市")
ui.CheckboxGroup(c, &cities, ui.Opts("北京", "上海", "广州")).Border().Size(ui.Small).A11yLabel("城市(小号)")
}主题与样式
- Ant Design 与 shadcn 下默认是方框加文字;
Border()、Buttons()对应 Element Plus 的变体,在任何风格下都可使用,配色跟随当前主题。 - 组件令牌:
theme.Components.Checkbox(CheckboxToken)。 ConfigProvider.Defaults("Checkbox", ui.Props{"size": ...})统一设置Checkbox与CheckboxGroup的尺寸。
注意事项
- 状态:勾选状态就是你绑定的
bool/[]string,组件不另存副本;在代码中修改它们,下一帧界面就会更新。 - 表单校验:放进 Form 时,单个多选框用
ui.RulesOf(item, &agree, ui.MustBeTrue("...")),多选框组用ui.RulesOf配合ui.RequiredItems[string]、MinItems、MaxItems规则;在出错的表单项里方框自动显示错误色,也可以用Status("error")手动设置。 - 无障碍:Tab 聚焦,空格切换。没有可见标签的多选框(例如表格行选择)用
A11yLabel设置名称;多选框组用A11yLabel命名整组(在表单项里默认使用表单项标签)。 - 大量选项:一屏放不下的选项请改用多选 Select 或 Transfer 穿梭框。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Checkbox
func Checkbox(c *Context, label string, val *bool) *CheckboxWCheckbox adds a checkbox bound to *val.
返回 *CheckboxW,可链式调用的方法见 CheckboxW 方法。
ui.CheckboxGroup
func CheckboxGroup(c *Context, selected *[]string, opts []Option) *CheckboxGroupWCheckboxGroup renders one checkbox per option bound to the selected values. Changed on the result reports a selection change; the builder also takes Disabled, Min/Max, Border, Buttons and Size.
返回 *CheckboxGroupW,可链式调用的方法见 CheckboxGroupW 方法。
CheckboxW 方法
ui.CheckboxW:CheckboxW is a checkbox with a label.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of a checkbox without a visible label. | A11yLabel(s string) *CheckboxW |
Border | Border draws a frame around the box and the label (Element el-checkbox border); a checked one gets a brand-colored frame. | Border() *CheckboxW |
Button | Button renders the checkbox as a toggle button without a box (Element el-checkbox-button); checked buttons take the brand color. | Button() *CheckboxW |
Changed | Changed reports a toggle this frame. | Changed() bool |
Disabled | Disabled(on bool) *CheckboxW | |
Indeterminate | Indeterminate(on bool) *CheckboxW | |
Size | Size sets the checkbox size: the label font, the box (Element shrinks it when small) and the height of the Border / Button styles. | Size(s ControlSize) *CheckboxW |
Status | Status sets the validation status ("error" marks the box invalid; inside a failing FormItem it is set automatically). | Status(s string) *CheckboxW |
CheckboxGroupW 方法
ui.CheckboxGroupW:CheckboxGroupW is the checkbox group CheckboxGroup returns.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel names the group for screen readers. | A11yLabel(s string) *CheckboxGroupW |
Border | Border draws every checkbox with a frame (see CheckboxW.Border). | Border() *CheckboxGroupW |
Buttons | Buttons renders the options as a row of joined toggle buttons (Element el-checkbox-button). | Buttons() *CheckboxGroupW |
Changed | Changed reports a selection change this frame. | Changed() bool |
Disabled | Disabled disables every checkbox of the group. | Disabled(on bool) *CheckboxGroupW |
Max | Max allows at most n selected values: once n are checked the others are disabled (0 = no maximum). | Max(n int) *CheckboxGroupW |
Min | Min keeps at least n values selected: while only n are checked they cannot be unchecked (0 = no minimum). | Min(n int) *CheckboxGroupW |
Size | Size sets the size of the checkboxes. | Size(s ControlSize) *CheckboxGroupW |
Option 参数类型
ui.Option:Option is an item of a Select / RadioGroup / CheckboxGroup. Group puts the option under a titled group in Select / MultiSelect / VirtualSelect dropdowns (Ant OptGroup, Element el-option-group); options of one group are listed together in order of the group's first appearance.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Group | string |
CheckAll 的签名见 Go API 参考:func CheckAll(c *Context, selected *[]string, opts []Option) *CheckAllW,返回值的 Changed() 报告选择变化。