Tabs 标签页
在同一工作区切换内容面板,例如概览、事件、设置。用稳定的 Key 标识标签,用 Label 显示标题。
在线体验
点击不同标签查看面板变化;聚焦后可以用方向键切换。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var active = "overview"
func page(c *ui.Context) {
ui.Tabs(c, &active, []ui.TabItem{
{Key: "overview", Label: "Overview"},
{Key: "events", Label: "Events"},
{Key: "settings", Label: "Settings"},
}).Show()
switch active {
case "events":
ui.Text(c, "Calls, events and streams")
default:
ui.Text(c, "Build your interface with TanGo")
}
}常用参数与方法
| 参数 / 方法 | 说明 |
|---|---|
active *string | 当前标签的 Key |
items []ui.TabItem | 标签项列表 |
Card() | 卡片式标签 |
Placement(ui.Placement) | 标签栏的位置 |
Closable() | 允许关闭标签 |
Show() | 完成标签栏构建 |
Changed() bool | 读取当前帧是否切换标签 |
使用说明
本文示例根据 active 渲染内容。也可以使用 Panel 回调组织面板;可关闭标签的应用需要维护自己的标签列表。
更多场景
官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。
实现与更多示例
全部示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
完整 API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Tabs
func Tabs(c *Context, active *string, items []TabItem) *TabsWTabs adds a tab bar bound to the active key; render the panel yourself based on *active (or with Panel). Call Show after options.
ConfigProvider.Defaults("Tabs", ...) keys: "type" ("line" | "card" | "editable-card" | "border-card"), "size" (ControlSize), "centered" (bool), "tabPlacement" (Placement or Ant v6 "top" | "bottom" | "start" | "end"; the deprecated keys "placement" and "tabPosition" take "top" | "bottom" | "left" | "right" too), "stretch" (bool), "tabBarGutter" (px), "variant" ("default" | "line", the shadcn TabsList variant).
active may be nil (the Tabs keep the key themselves). When *active is not a key of items, the tab at the last active index (clamped; the first tab at first) becomes active, like Ant's Tabs when the active key goes away; Changed does not report that fallback.
Under shadcn/ui every combination is drawn as the segmented TabsList (muted track, background pill with shadow-sm): vertical placements stack the triggers, Closable adds a small x to each trigger, OnAdd a ghost + button after the list, Card switches to an outline track and BorderCard puts the list and panel in a bordered card.
返回 *TabsW,可链式调用的方法见 TabsW 方法。
TabsW 方法
ui.TabsW:TabsW is a tab bar: line or card style, editable cards (close and add buttons), top / bottom / left / right placement, centered tabs and a scrolling tab list with a "more" dropdown for the tabs that do not fit.
| 方法 | 说明 | 签名 |
|---|---|---|
ActiveTabFontWeight | ActiveTabFontWeight sets the active tab label's font weight (100–900). | ActiveTabFontWeight(weight int) *TabsW |
AddIcon | AddIcon replaces the + of the add button (Ant addIcon). | AddIcon(ic *icon.Icon) *TabsW |
Added | Added reports a click on the add button this frame. | Added() bool |
BeforeLeave | BeforeLeave is asked before the active tab changes (Element before-leave): returning false keeps the current tab. | BeforeLeave(fn func(newKey, oldKey string) bool) *TabsW |
BorderCard | BorderCard puts the bar and the panel built by Panel in a bordered card whose header strip holds the tabs (Element type="border-card"). | BorderCard() *TabsW |
Card | Card() *TabsW | |
Centered | Centered centers the tabs in the bar. | Centered() *TabsW |
Changed | Changed() bool | |
Closable | Closable makes an editable card bar (Ant type="editable-card", Element closable): every tab without NoClose gets a close button; read Closed. | Closable() *TabsW |
Closed | Closed returns the key of the tab closed this frame ("" when none). | Closed() string |
Draggable | Draggable lets the user reorder the tabs by dragging them (the moved tab becomes active); read the new order with Reordered. | Draggable() *TabsW |
Extra | Extra(fn func()) *TabsW | |
ExtraLeft | ExtraLeft builds content at the start of the bar, before the tabs (Ant tabBarExtraContent.left); Extra is the right one. | ExtraLeft(fn func()) *TabsW |
Height | Height fixes the height of a Left / Right Tabs (bar and panel); tabs that do not fit scroll and are listed in the more menu. | Height(px float32) *TabsW |
Indicator | Indicator sets the ink bar of line tabs (Ant v6 indicator): size is its length in px (0: the tab's extent; Element: the label's), align places it along the tab: "start", "center" (default) or "end". | Indicator(size float32, align string) *TabsW |
IndicatorThickness | IndicatorThickness sets the cross-axis thickness of a line-tab indicator in logical pixels. | IndicatorThickness(px float32) *TabsW |
MoreIcon | MoreIcon replaces the icon of the more button (Ant more.icon). | MoreIcon(ic *icon.Icon) *TabsW |
NoMargin | NoMargin drops the gap between the bar and the content (for a bar that sits at the bottom edge of a header, as in PageContainer). | NoMargin() *TabsW |
OnAdd | OnAdd shows the add button of an editable card bar; fn runs when it is clicked (Added reports it as well). | OnAdd(fn func()) *TabsW |
OnTearOff | OnTearOff makes the tabs draggable and lets a tab be dragged out of the window: released outside, fn runs with its key and the release point, and TornOff reports them. | OnTearOff(fn func(key string, pos geom.Point)) *TabsW |
Panel | Panel builds the content of the active tab in a tab panel (exposed to screen readers as the tabpanel labelled by the active tab). | Panel(fn func()) *Box |
Placement | Placement puts the bar at the Top (default), Bottom, Left or Right of the panel built by Panel. | Placement(p Placement) *TabsW |
RemoveIcon | RemoveIcon replaces the x of the close buttons (Ant removeIcon). | RemoveIcon(ic *icon.Icon) *TabsW |
Reordered | Reordered returns the items in their new order after a drag or keyboard move this frame (nil when none). | Reordered() []TabItem |
Show | Show builds the tab bar. | Show() *TabsW |
Size | Size(s ControlSize) *TabsW | |
Stretch | Stretch makes the tabs of a horizontal bar share its width (Element stretch); the bar no longer scrolls. | Stretch() *TabsW |
TabBarGutter | TabBarGutter sets the space between two tabs (Ant tabBarGutter; the default comes from the theme: Ant 32 for line tabs, 2 for cards). | TabBarGutter(px float32) *TabsW |
TabBarStyle | TabBarStyle applies layout options to the bar (padding, margin ...; Ant tabBarStyle). | TabBarStyle(opts ...layout.Option) *TabsW |
TabClicked | TabClicked returns the key of the tab clicked this frame, also when it is already active ("" when none; Ant onTabClick, Element tab-click). | TabClicked() string |
TabItemHeight | TabItemHeight sets the height of horizontal tabs in logical pixels. | TabItemHeight(px float32) *TabsW |
TabPlacement | TabPlacement puts the bar by Ant v6 name: "top" (default), "bottom", "start" or "end" (logical sides: mirrored in RTL); "left" and "right" are accepted as their deprecated aliases. | TabPlacement(p string) *TabsW |
TornOff | TornOff reports the tab torn off this frame and its release point (see OnTearOff). | TornOff() (key string, pos geom.Point, ok bool) |
Variant | Variant picks the list look in every theme: "default" or "line" (line tabs: a transparent list with an underline under the active tab). | Variant(v string) *TabsW |
TabItem 参数类型
ui.TabItem:TabItem is a tab.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Label | string | |
Icon | *icon.Icon | |
Disabled | bool | |
NoClose | no close button on this tab of a Closable bar | bool |