Keel v0.5.1 中文 GitHub

GroupBox

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/group_box.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("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("Notification settings").Description("Choose how to receive notifications").Child(views...) displays the title and description outside the content box, with rounded corners and a Surface background, without a default outline.

The constructor only accepts a title. Child accepts el.View and appends it to the content area, and SetChildren replaces the content in batches. Call the subview's Render every frame, preserving subview interaction. SetTitle modifies the title. Agent role group, titled as name, description and content listed separately. The component itself has no keyboard operation.

Variant accepts GroupBoxSurface (default, Surface background with an optional theme outline), GroupBoxNormal (no background/borders), GroupBoxFill (Subtle background), and GroupBoxOutline (transparent background and borders). Each appearance preserves content padding.

Footer(el.View) adds bottom description or operation outside the content box, aligns with the left side of the title, and has a spacing of 8dp; it inherits the small Muted text by default, and passes nil to remove it. Title, description, and footer are not affected by body styles.

TitleStyle(func(*el.TextEl)) modifies the title font size, color, font weight, spacing, etc.; ContentStyle(func(*el.DivEl)) modifies the text background, border, rounded corners, padding and layout after the appearance default value. The callback is executed every frame and can read the current theme; it only modifies the incoming elements and does not retain element references. Pass nil to restore default. The body container maintains a stable identity, and deleting the title or updating the footer does not rebuild the internal input state.

box := kit.GroupBox("Notification settings").Variant(kit.GroupBoxFill).
    Child(kit.Switch("Email notifications", true)).
    Footer(el.ViewFunc(func(*el.Context) el.Element {
        return el.Text("Settings apply to this device only")
    })).
    ContentStyle(func(e *el.DivEl) { e.P(24).Rounded(theme.RadiusLg) })

Verification: go run ./examples/components -section group_box -theme dark, omit theme to see the light theme.