Keel v0.5.1 English GitHub

GroupBox

在线示例:这是真实的 Keel 程序编译成 WebAssembly,可以直接操作。首次加载约 10 MB。全屏打开
示例代码Go · 可复制

这是上方组件库展示案例的完整源码。examples/components/group_box.go

在脚手架项目中复用组件构造和 Render 写法;示例的 registerSection 与共享辅助函数由组件库提供。

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("group_box", "display", func() core.Widget {
		hint := el.ViewFunc(func(*el.Context) el.Element {
			return el.Text(demoText("Settings apply to this device only; the footer sits outside the content box.", "设置只用于当前设备,底部说明位于内容框外。"))
		})
		boxes := []*kit.GroupBoxView{
			kit.GroupBox(demoText("Default · Account information", "默认 · 账户信息")).Description(demoText("Surface background without an outline", "Surface 背景,默认无描边")).Child(kit.DescriptionList().Item(demoText("Name", "姓名"), demoText("Alex / Ada", "张三 / Ada")), kit.Tag(demoText("Verified", "已验证")).Tone(kit.ToneSuccess)).Footer(hint),
			kit.GroupBox(demoText("Filled · Notifications", "填充 · 通知")).Variant(kit.GroupBoxFill).Child(kit.Switch(demoText("Email reminders", "邮件提醒"), true)).Footer(hint),
			kit.GroupBox(demoText("Outline · Custom style", "描边 · 自定义样式")).Variant(kit.GroupBoxOutline).Child(kit.Input(demoText("Name", "名称"))).TitleStyle(func(e *el.TextEl) { e.TextColor(theme.PrimaryText).TextSize(theme.TextLg) }).ContentStyle(func(e *el.DivEl) { e.P(theme.Space2xl).Rounded(theme.RadiusLg).Border(2, theme.Primary) }).Footer(kit.Button(demoText("Save", "保存"), nil).Variant(kit.ButtonSecondary)),
			kit.GroupBox(demoText("No decoration", "无装饰")).Variant(kit.GroupBoxNormal).Child(kit.Checkbox(demoText("Receive updates", "接收更新"), true)),
		}
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			root := el.Div().P(theme.SpaceXl).Gap(theme.Space2xl).W(el.Full)
			for _, box := range boxes {
				root.Child(box.Render(cx))
			}
			return root
		}))
	})
}

kit.GroupBox("通知设置").Description("选择接收方式").Child(views...) 在内容框外显示标题和说明,内容区带圆角与 Surface 背景,默认无描边。

构造函数只接受标题。Child 接受 el.View 并追加到内容区,SetChildren 批量替换内容。每帧调用子视图的 Render,保留子视图交互。SetTitle 修改标题。Agent 角色 group,标题为名字,说明与内容单独列出。组件本身无键盘操作。

Variant 接受 GroupBoxSurface(默认,Surface 背景,描边随主题显式开启)、GroupBoxNormal(无背景/边框)、GroupBoxFill(Subtle 背景)和 GroupBoxOutline(透明底色加边框)。各外观均保留内容内边距。

Footer(el.View) 在内容框外添加底部说明或操作,和标题左侧对齐、间距 8dp;默认继承小号 Muted 文字,传 nil 移除。标题、描述和 footer 不受正文样式影响。

TitleStyle(func(*el.TextEl)) 修改标题字号、颜色、字重、间距等;ContentStyle(func(*el.DivEl)) 在外观默认值之后修改正文背景、边框、圆角、内边距与布局。回调每帧执行,可读取当前主题;只修改传入元素,不保留元素引用。传 nil 恢复默认。正文容器保持稳定身份,删除标题或更新 footer 不会重建内部输入状态。

box := kit.GroupBox("通知设置").Variant(kit.GroupBoxFill).
    Child(kit.Switch("邮件通知", true)).
    Footer(el.ViewFunc(func(*el.Context) el.Element {
        return el.Text("设置仅用于当前设备")
    })).
    ContentStyle(func(e *el.DivEl) { e.P(24).Rounded(theme.RadiusLg) })

验证:go run ./examples/components -section group_box -theme dark,省略 theme 查看浅色。