Tag 标签
用紧凑的彩色文本标记分类、状态或筛选条件。
何时使用
- 展示状态、类别和可关闭的筛选条件。
- 需要用户选择一个或多个选项时使用 Select 选择器。
- 需要数字角标时使用 Badge 徽章。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Tag(c, "处理中").Color("processing")
ui.Tag(c, "已完成").Color("success")
}常见用法
状态颜色
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")
}可关闭标签
var closed bool
func closable(c *ui.Context) {
t := ui.Tag(c, "筛选条件").Closable()
if t.Closed() { closed = true }
_ = closed
}图标和无边框
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Tag
func Tag(c *Context, label string) *TagWTag 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
func Disabled(c *Context, on bool, fn func())Disabled runs fn with every widget inside disabled.
TagW 方法
ui.TagW:TagW is a small label.
| 方法 | 说明 | 签名 |
|---|---|---|
Borderless | Borderless draws the tag without its border. | Borderless() *TagW |
Closable | Closable shows a close icon. | Closable() *TagW |
CloseIcon | CloseIcon replaces the close icon (antd closeIcon); it also makes the tag closable. | CloseIcon(ic *icon.Icon) *TagW |
Closed | Closed reports a click on the close icon (or a keyboard close); always false while disabled. | Closed() bool |
Color | Color(col string) *TagW | |
Disabled | Disabled greys the tag out (antd 6 disabled); a disabled closable tag does not close. | Disabled(on bool) *TagW |
Effect | Effect 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 |
Icon | Icon(ic *icon.Icon) *TagW | |
Round | Round draws a pill-shaped tag (Element round). | Round() *TagW |
Size | Size sets small (20px), middle (default) or large (Element 32px) tags. | Size(sz ControlSize) *TagW |
Variant | Variant 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 |