CheckCard 多选卡片
以卡片形式呈现可选项,支持单卡切换、单选组和多选组。
何时使用
- 选项需要标题、描述、头像或图标,普通 Checkbox 的信息密度不够时。
- 选择产品方案、页面模板、工作区或功能模块等视觉化选项时。
- 只有简短文本选项时,用 Radio 单选 或 Checkbox 多选 更紧凑。
- 选项很多、需要搜索或虚拟滚动时,不应把所有项都渲染成卡片。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
var selected bool
func page(c *ui.Context) {
card := ui.CheckCard(c, "启用智能摘要", &selected).
Description("在保存后自动生成摘要").Avatar("智").Show()
if card.Changed() {
ui.Message(c, "info", "选项已切换")
}
}卡片通过 *bool 绑定选中状态;Show() 负责构建卡片并处理点击、Enter 和空格操作。
常见用法
单选组
CheckCardGroup.Value 绑定一个字符串;再次点击已选卡片会清空值。每个选项由 CheckCardOption 描述。
import "github.com/tangoui/tango/ui"
var template string
var templates = []ui.CheckCardOption{
{Value: "dashboard", Title: "仪表盘", Description: "监控和指标", Avatar: "仪"},
{Value: "form", Title: "表单页", Description: "配置和录入", Avatar: "表"},
{Value: "disabled", Title: "暂不可用", Disabled: true},
}
func page(c *ui.Context) {
ui.CheckCardGroup(c, templates).Value(&template).Show()
}多选组
Values 绑定 []string,提交顺序按选项定义顺序保持;单个选项仍可通过 Disabled 禁用。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
var features []string
var featureOptions = []ui.CheckCardOption{
{Value: "table", Title: "数据表格", Description: "查询与筛选", Icon: icon.Table},
{Value: "chart", Title: "图表", Description: "趋势与分布", Icon: icon.Dashboard},
}
func page(c *ui.Context) {
ui.CheckCardGroup(c, featureOptions).Values(&features).Show()
}尺寸、宽度与状态
Small、默认尺寸和 Large 分别提供 212、320、440 像素的默认宽度;Width 可以覆盖默认值,Loading 显示骨架。
import "github.com/tangoui/tango/ui"
var compact, normal, loading bool
func page(c *ui.Context) {
ui.Row(c, func() {
ui.CheckCard(c, "小卡片", &compact).Small().Show()
ui.CheckCard(c, "标准卡片", &normal).Width(360).Show()
ui.CheckCard(c, "加载中", &loading).Loading(true).Show()
})
}描述、头像与右上角内容
Avatar 或 AvatarIcon 提供识别信息,Description 放置辅助说明,Extra 在右上角构建操作或状态;不需要边框时调用 Borderless()。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/icon"
)
var plan bool
func page(c *ui.Context) {
ui.CheckCard(c, "团队版", &plan).
AvatarIcon(icon.Team).Description("适合多人协作").
Extra(func() { ui.Icon(c, icon.More).Size(16) }).
Borderless().Show()
}主题与样式
CheckCard使用 Pro 组件卡片令牌;Ant Design、Element Plus、shadcn 会分别呈现选中边框/角标或右上角选择指示器。Size、Small、Large控制卡片尺寸;Borderless只去掉普通边框,选中状态仍保留主题强调。ConfigProvider.Defaults("CheckCard", ...)支持size和bordered;组的默认尺寸也读取这个配置。
注意事项
- 把绑定的
bool、字符串或字符串切片放在稳定状态中;卡片每帧重建不会丢失它们。 CheckCardGroup应只使用Value或Values其中一种;多选组的Values会按选项顺序重新生成切片。- 大量选项会带来布局和可访问性负担;应考虑 Select、Radio 或分页列表。
- 卡片具有 checkbox/radio 语义并支持 Enter、空格;有描述时会作为辅助说明传给无障碍树。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.CheckCard
func CheckCard(c *Context, title string, checked *bool) *CheckCardWCheckCard starts a checkable card bound to checked; chain options, then Show.
返回 *CheckCardW,可链式调用的方法见 CheckCardW 方法。
ui.CheckCardGroup
func CheckCardGroup(c *Context, opts []CheckCardOption) *CheckCardGroupWCheckCardGroup starts a group. Value / Values bind application state; without a binding the group owns its state, initialized by DefaultValue or DefaultValues. Values also selects multiple-choice mode.
返回 *CheckCardGroupW,可链式调用的方法见 CheckCardGroupW 方法。
CheckCardW 方法
ui.CheckCardW:CheckCardW builds a checkable card.
| 方法 | 说明 | 签名 |
|---|---|---|
Actions | Actions builds the bottom action bar. | Actions(fns ...func()) *CheckCardW |
Avatar | Avatar shows a text avatar (first character) left of the title. | Avatar(s string) *CheckCardW |
AvatarIcon | AvatarIcon shows an icon avatar left of the title. | AvatarIcon(ic *icon.Icon) *CheckCardW |
Borderless | Borderless removes the border (the checked state still colors it). | Borderless() *CheckCardW |
Changed | Changed reports a toggle by the user this frame (after Show). | Changed() bool |
Clicked | Clicked reports a click this frame (after Show). | Clicked() bool |
Content | Content builds body content below the card's metadata. | Content(fn func()) *CheckCardW |
Cover | Cover replaces the content with fn (an image or any block). | Cover(fn func()) *CheckCardW |
DefaultChecked | DefaultChecked sets the initial checked state when checked is nil. | DefaultChecked(on bool) *CheckCardW |
Description | Description sets the secondary text under the title. | Description(s string) *CheckCardW |
Disabled | Disabled disables the card. | Disabled(on bool) *CheckCardW |
Extra | Extra builds content at the top right. | Extra(fn func()) *CheckCardW |
ID | ID returns the card's widget ID (after Show). | ID() ID |
IsChecked | IsChecked reports the current selection after Show. | IsChecked() bool |
Large | Large is Size(Large). | Large() *CheckCardW |
Loading | Loading shows a skeleton instead of the content. | Loading(on bool) *CheckCardW |
Show | Show builds the card and toggles it on click / Enter / Space. | Show() *CheckCardW |
Size | Size sets the size: Small (212px), Middle (320px) or Large (440px). | Size(s ControlSize) *CheckCardW |
Small | Small is Size(Small). | Small() *CheckCardW |
SubTitle | SubTitle sets text beside the title. | SubTitle(s string) *CheckCardW |
Value | Value sets this card's key when built in CheckCardGroup.Body. | Value(s string) *CheckCardW |
Width | Width overrides the width of the size. | Width(px float32) *CheckCardW |
CheckCardGroupW 方法
ui.CheckCardGroupW:CheckCardGroupW builds a single- or multiple-choice card group.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body builds custom CheckCards linked to this group by Value. | Body(fn func()) *CheckCardGroupW |
Borderless | Borderless removes option card borders. | Borderless() *CheckCardGroupW |
Changed | Changed reports a user selection change this frame. | Changed() bool |
DefaultValue | DefaultValue sets the initial single selection, ignored when bound. | DefaultValue(v string) *CheckCardGroupW |
DefaultValues | DefaultValues sets initial multiple selections, ignored when bound. | DefaultValues(v ...string) *CheckCardGroupW |
Disabled | Disabled disables every card. | Disabled(on bool) *CheckCardGroupW |
Layout | Layout sets wrapping-row layout options. | Layout(opts ...layout.Option) *CheckCardGroupW |
Loading | Loading shows noninteractive, unchecked loading placeholders. | Loading(on bool) *CheckCardGroupW |
Multiple | Multiple enables multiple choices with internally owned state. | Multiple() *CheckCardGroupW |
Selected | Selected reports the single selection after Show / Body. | Selected() string |
SelectedValues | SelectedValues reports a copy of the multiple selections after Show / Body. | SelectedValues() []string |
Show | Show renders the configured options. | Show() *CheckCardGroupW |
Size | Size sets the group size. | Size(s ControlSize) *CheckCardGroupW |
Value | Value binds the single choice. | Value(v *string) *CheckCardGroupW |
Values | Values binds multiple choices, maintained in option order. | Values(v *[]string) *CheckCardGroupW |
Width | Width overrides every option card's width. | Width(px float32) *CheckCardGroupW |
CheckCardOption 参数类型
ui.CheckCardOption:CheckCardOption is one card of a CheckCardGroup.
| 字段 | 说明 | 类型 |
|---|---|---|
Value | string | |
Title | string | |
Description | string | |
Avatar | text avatar (first character) | string |
Icon | icon avatar | *icon.Icon |
Disabled | bool | |
Size | zero without SizeSet: inherit group size | ControlSize |
SizeSet | allows explicit Middle to override the group size | bool |
Cover | func() | |
Children | []CheckCardOption |