Skip to content

Tag 标签 ​

用紧凑的彩色文本标记分类、状态或筛选条件。

何时使用 ​

  • 展示状态、类别和可关闭的筛选条件。
  • 需要用户选择一个或多个选项时使用 Select 选择器。
  • 需要数字角标时使用 Badge 徽章。

在线体验 ​

Tag 标签 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

func page(c *ui.Context) {
    ui.Tag(c, "处理中").Color("processing")
    ui.Tag(c, "已完成").Color("success")
}

常见用法 ​

状态颜色 ​

go
func statuses(c *ui.Context) {
    ui.Tag(c, "成功").Color("success")
    ui.Tag(c, "警告").Color("warning")
    ui.Tag(c, "失败").Color("error")
    ui.Tag(c, "自定义").Color("#722ed1")
}

可关闭标签 ​

go
var closed bool
func closable(c *ui.Context) {
    t := ui.Tag(c, "筛选条件").Closable()
    if t.Closed() { closed = true }
    _ = closed
}

图标和无边框 ​

go
func decorated(c *ui.Context) {
    ui.Tag(c, "发布").Borderless().Effect("light")
}

主题与样式 ​

Color 支持状态名和十六进制颜色;Size、Effect、Borderless 分别控制尺寸、填充效果和边框。默认键为 size、effect、bordered。

注意事项 ​

Closed 是当前帧的交互结果,业务状态应在外部变量中保存。标签文字不要承担唯一的颜色语义,并为可关闭操作保留清晰的键盘焦点。

示例代码 ​

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

Tag 标签

gallery 演示 Tag 的 Render 函数,源码 page_display.go#L43。

加载中…

Tag 变体、禁用与 Badge 外观

gallery 演示 TagAlign 的 Render 函数,源码 page_b2g.go#L24。

加载中…

Tag 尺寸与效果、图标头像

gallery 演示 TagMore 的具名 Render 函数,源码 page_p2c_display.go#L77。

加载中…

API ​

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

入口函数 ​

ui.Tag ​

go
func Tag(c *Context, label string) *TagW

Tag adds a tag. color may be "success", "processing", "error", "warning", "default", an Ant preset ("blue", "magenta" ...; with the suffix "-inverse", e.g. "blue-inverse", the preset solid unless a Variant is set, like antd), a shadcn Badge variant ("primary", "secondary", "destructive", "outline", "ghost", "link") or a hex color.

ConfigProvider.Defaults("Tag", ...) keys: "size" (ControlSize), "effect", "variant", "color" (string), "bordered" (bool; deprecated, false = variant "filled"), "disabled" (bool).

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

ui.Disabled ​

go
func Disabled(c *Context, on bool, fn func())

Disabled runs fn with every widget inside disabled.

TagW 方法 ​

ui.TagW:TagW is a small label.

方法说明签名
BorderlessBorderless draws the tag without its border.Borderless() *TagW
ClosableClosable shows a close icon.Closable() *TagW
CloseIconCloseIcon replaces the close icon (antd closeIcon); it also makes the tag closable.CloseIcon(ic *icon.Icon) *TagW
ClosedClosed reports a click on the close icon (or a keyboard close); always false while disabled.Closed() bool
ColorColor(col string) *TagW
DisabledDisabled greys the tag out (antd 6 disabled); a disabled closable tag does not close.Disabled(on bool) *TagW
EffectEffect sets the Element theme of the tag: "light" (tinted with a border; = Variant("outlined")), "dark" (solid color, white text; = Variant("solid")) or "plain" (container background, colored border and text).Effect(e string) *TagW
IconIcon(ic *icon.Icon) *TagW
RoundRound draws a pill-shaped tag (Element round).Round() *TagW
SizeSize sets small (20px), middle (default) or large (Element 32px) tags.Size(sz ControlSize) *TagW
VariantVariant sets the antd 6 variant: "filled" (tinted, no border; the default outside Element), "solid" (saturated color, light text) or "outlined" (tinted with a border; Element's default light effect).Variant(v string) *TagW

相关链接 ​

以 MIT 许可发布