Skip to content

Compact 紧凑布局 ​

把相邻输入框、选择器、按钮和附加标签拼接为一个整体,只保留外侧圆角。

何时使用 ​

  • 输入框与按钮、协议选择与地址输入、金额与单位等组合控件。
  • 一组按钮需要首尾相连,形成按钮组。
  • 只是给控件之间留间距,使用 Space 间距。
  • 表单中的标签、校验和布局交给 Form 表单,Compact 只负责控件拼接。

在线体验 ​

Compact 紧凑组合(Space.Compact / Input.Group) · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

var keyword string

func page(c *ui.Context) {
    ui.Compact(c).Body(func() {
        ui.Input(c, &keyword).Placeholder("搜索").Layout(layout.W(220))
        ui.Button(c, "搜索").Primary()
    })
}

Compact 让相邻控件边框重叠 1px,连接处的圆角被消除,形成一个视觉整体。

常见用法 ​

前置和后置标签 ​

Addon 可以放在输入框两侧,表达协议、单位或固定后缀。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

var site string

func page(c *ui.Context) {
    ui.Compact(c).Body(func() {
        ui.Addon(c, "https://")
        ui.Input(c, &site).Layout(layout.W(220)).A11yLabel("网址")
        ui.Addon(c, ".com")
    })
}

选择器与按钮组合 ​

不同控件可以混排;每个控件仍保留自身的状态、焦点和禁用逻辑。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

var (
    protocol = "https://"
    host     string
)

func page(c *ui.Context) {
    ui.Compact(c).Body(func() {
        ui.Select(c, &protocol, ui.Opts("https://", "http://")).Width(100).A11yLabel("协议")
        ui.Input(c, &host).Placeholder("站点").Layout(layout.W(200))
        ui.Button(c, "访问").Primary()
    })
}

竖向按钮组与撑满 ​

Vertical() 或 Orientation("vertical") 上下拼接,Orientation("horizontal") 切回水平拼接。Block() 让组合宽度撑满容器。

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    ui.Compact(c).Vertical().Block().Body(func() {
        ui.Button(c, "上移")
        ui.Button(c, "下移")
        ui.Button(c, "删除").Danger()
    })
}

主题与样式 ​

Compact 处理拼接边缘和外侧圆角,各控件沿用当前主题的样式。可用 Size(ui.Small) 设置组内控件的默认尺寸,单个控件显式设置的尺寸优先。ConfigProvider.Defaults("Compact", ui.Props{"block": true}) 可设置默认撑满。

注意事项 ​

  • Compact 只作用于直接子控件;包装多层容器可能影响边缘识别,建议保持子控件平铺。
  • 子控件的值仍由各自绑定的变量保存,Compact 不保存状态。
  • 每个输入和图标按钮都需要可读的标签,例如 A11yLabel,否则组合后的语义不清楚。
  • 错误态、禁用态应设置在具体控件上;Compact 不会把一个控件的状态传给其他控件。
  • RTL 下拼接顺序与外侧圆角自动镜像。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Compact 紧凑组合(Space.Compact / Input.Group)

gallery 演示 Compact 的 Render 函数,源码 page_entry_input.go#L94。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Compact ​

go
func Compact(c *Context) *CompactW

Compact starts a compact group; chain options, then Body.

ui.Compact(c).Body(func() { ui.Input(c, &v); ui.Button(c, "提交").Primary() })

ConfigProvider.Defaults("Compact", ...) keys: "block" (bool).

返回 *CompactW,可链式调用的方法见 CompactW 方法。

ui.Addon ​

go
func Addon(c *Context, label string) *AddonW

Addon adds an addon box with a label.

返回 *AddonW,可链式调用的方法见 AddonW 方法。

CompactW 方法 ​

ui.CompactW:CompactW joins its direct child controls (Input, Select, Button, InputNumber, DatePicker, Addon, ...) into one group: neighbours overlap by one pixel and only the outer corners stay rounded (Ant Space.Compact / Input.Group, Element input prepend/append and button groups).

The children are drawn by their own painters; Compact squares the joined corners by repainting the joined edge columns with a widened rectangle, so it works with any control without changing it.

方法说明签名
BlockBlock stretches the group to the container width.Block() *CompactW
BodyBody adds the controls.Body(fn func()) *Box
LayoutLayout applies layout options to the group.Layout(o ...layout.Option) *CompactW
OrientationOrientation sets the direction: "horizontal" (default) or "vertical" (antd 6 orientation; Vertical is the shorthand).Orientation(o string) *CompactW
SizeSize sets the size of the controls in the group that do not set their own (antd Space.Compact size): it scopes the component size like ConfigProvider.Size.Size(s ControlSize) *CompactW
VerticalVertical stacks the controls (joined top/bottom edges).Vertical() *CompactW

AddonW 方法 ​

ui.AddonW:AddonW is a static (or clickable) label attached before/after an input inside Compact (Ant addonBefore/addonAfter, Element prepend/append).

方法说明签名
A11yLabelA11yLabel sets the accessible name (needed for an icon-only addon button).A11yLabel(s string) *AddonW
ClickedClicked makes the addon a button (Element append button) and reports a click.Clicked() bool
DisabledDisabled(on bool) *AddonW
IconIcon shows an icon before (or instead of) the label.Icon(ic *icon.Icon) *AddonW
LayoutLayout(o ...layout.Option) *AddonW
SizeSize(s ControlSize) *AddonW

相关链接 ​

以 MIT 许可发布