Timeline 时间轴
按时间或流程顺序展示一组事件节点。
何时使用
- 操作记录、审批流、发布历史和物流轨迹。
- 需要事件之间的轴线关系时使用;简单步骤导航用 Steps 步骤条。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Timeline(c, []ui.TimelineItem{
{Content: "创建项目", Time: "09:00", Color: "success"},
{Content: "提交审核", Time: "10:30", Color: "processing"},
{Content: "等待发布", Time: "进行中", Hollow: true},
}).Show()
}常见用法
交替布局
func alternate(c *ui.Context, items []ui.TimelineItem) {
ui.Timeline(c, items).Mode("alternate").Reverse().Show()
}横向时间轴
func horizontal(c *ui.Context) {
ui.Timeline(c, []ui.TimelineItem{{Content: "开始"}, {Content: "完成"}}).Horizontal().Show()
}自定义节点和待处理项
Body 或 Dot 可构建自定义内容,Pending 在末尾追加旋转中的节点。
func pending(c *ui.Context) {
ui.Timeline(c, []ui.TimelineItem{{Content: "已完成"}}).Pending("下一步处理中").Show()
}主题与样式
Mode 支持 left、right、alternate、start、end;默认键为 mode 和 reverse。颜色状态和空心节点由 Timeline 令牌统一适配三套主题。
注意事项
事件数组顺序就是渲染顺序;实时数据到达时替换外部数组。时间文本要与内容形成可读的语义,不要只用颜色区分状态。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Timeline
func Timeline(c *Context, items []TimelineItem) *TimelineWTimeline starts a timeline; finish with Show.
ConfigProvider.Defaults("Timeline", ...) keys: "mode" ("start" | "end" | "alternate" | "alternate-reverse"; "left" / "right" are the deprecated names of start / end), "reverse" (bool), "orientation" ("vertical" | "horizontal"), "variant" ("outlined" | "filled").
返回 *TimelineW,可链式调用的方法见 TimelineW 方法。
TimelineW 方法
ui.TimelineW:TimelineW shows events along a vertical axis.
| 方法 | 说明 | 签名 |
|---|---|---|
Horizontal | Horizontal lays the events out along a horizontal axis (Ant v6 orientation="horizontal"): every event takes an equal share of the width with its dot on the axis. | Horizontal() *TimelineW |
Layout | Layout applies layout options to the timeline (call after Show). | Layout(o ...layout.Option) *TimelineW |
Mode | Mode places the content: "start" (axis at the inline start, content after it; default), "end" (axis at the inline end), "alternate" (content alternates around a centered axis, the first item after it) or "alternate-reverse" (Element: the first item before it). | Mode(m string) *TimelineW |
Orientation | Orientation lays the events out "vertical" (default) or "horizontal" (Ant v6 orientation; same as Horizontal). | Orientation(o string) *TimelineW |
Pending | Pending appends a "still in progress" item with a spinning dot. | Pending(label string) *TimelineW |
Reverse | Reverse lists the items in reverse order. | Reverse() *TimelineW |
Show | Show builds the timeline. | Show() *TimelineW |
TitleSpan | TitleSpan sets the share of the width, in 24ths, taken by the titles of a vertical timeline whose items have a Title (Ant titleSpan; default 12, the axis in the middle). | TitleSpan(span float32) *TimelineW |
Variant | Variant sets the dots: "outlined" (a ring, Ant's default) or "filled" (a solid dot, the Element and shadcn default). | Variant(v string) *TimelineW |
TimelineItem 参数类型
ui.TimelineItem:TimelineItem is one event of a Timeline.
| 字段 | 说明 | 类型 |
|---|---|---|
Content | main text (Ant v6 content) | string |
Body | custom content (replaces Content) | func() |
Title | Title is the text on the opposite side of the axis (Ant v6 title); any item with a Title centers the axis of a vertical timeline. | string |
Label | Deprecated: use Title. | string |
Time | Element: timestamp under (or above) the content | string |
TimeTop | put Time above the content | bool |
Color | Color is a status ("primary", "success", "warning", "error", "info", "gray") or a hex color; empty uses the theme default. | string |
DotIcon | icon instead of the circle | *icon.Icon |
Dot | fully custom dot node | func() |
Hollow | Element: hollow dot | bool |
Large | Element: larger dot | bool |
Placement | Placement puts this item's content at the "start" or "end" side, overriding the Mode (Ant v6 items[].placement). | string |
Loading | Loading shows a spinning dot (Ant v6 items[].loading); the axis entering it (leaving it under Reverse) is dotted where the theme's TimelineToken.BusyTailDashed says so. | bool |
Center | Center centers the dot vertically on the item (Element center). | bool |