TimePicker 时间选择框
输入或选择一天中的某个时间。下拉面板由时 / 分 / 秒滚动列组成,支持步长、12 小时制和禁用时间;还提供时间范围 TimeRangePicker 和可单独嵌入的 TimePanel。
何时使用
- 需要用户输入一个时刻(提醒时间、营业开始时间)时。
- 需要同时选择日期和时间时,用 DatePicker 中的
DateTimePicker;选择一段日期用RangePicker。 - 只能从固定间隔的几个时间点中选择(例如每 30 分钟一个档位)时,用下拉列表风格的 TimeSelect 更直接。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"time"
"github.com/tangoui/tango/ui"
)
var remindAt time.Time
func page(c *ui.Context) {
ui.TimePicker(c, &remindAt).Placeholder("请选择时间")
if !remindAt.IsZero() {
ui.Text(c, "提醒时间:"+remindAt.Format("15:04:05"))
}
}值绑定到 *time.Time,零值表示未选择;新选出的值的日期部分为当天。既可以在面板里滚动选择,也可以直接在输入框中键入时间。Changed() 报告本帧是否提交了新值。
常见用法
格式与步长
Format 使用 Go 时间布局决定显示哪些列:"15:04:05"(默认)、"15:04"(隐藏秒列)。HourStep、MinuteStep、SecondStep 或一次性的 Steps(时, 分, 秒) 设置每列的步长。
import (
"time"
"github.com/tangoui/tango/ui"
)
var open, slot time.Time
func page(c *ui.Context) {
ui.TimePicker(c, &open).Format("15:04").MinuteStep(15)
ui.TimePicker(c, &slot).Steps(1, 5, 10)
}12 小时制
Use12Hours() 显示 12 小时制:小时列为 12、01 … 11,并增加上午 / 下午列;默认格式变为 "03:04:05 PM",上午 / 下午的文字取自当前语言包。
import (
"time"
"github.com/tangoui/tango/ui"
)
var alarm time.Time
func page(c *ui.Context) {
ui.TimePicker(c, &alarm).Use12Hours()
ui.TimePicker(c, &alarm).Use12Hours().Format("03:04 PM")
}禁用时间
DisabledHours、DisabledMinutes、DisabledSeconds 标记不可选择也不可键入的时间(小时为 0–23)。HideDisabledOptions(true) 直接从列中隐藏被禁用的项,而不是置灰。
import (
"time"
"github.com/tangoui/tango/ui"
)
var appointment time.Time
func page(c *ui.Context) {
ui.TimePicker(c, &appointment).Format("15:04").
DisabledHours(func(h int) bool { return h < 9 || h >= 18 }).
DisabledMinutes(func(h, m int) bool { return h == 12 }). // 午休
HideDisabledOptions(true)
}时间范围 TimeRangePicker
TimeRangePicker 绑定 *[2]time.Time,开始和结束各有一组时间列;步长、12 小时制和禁用时间的用法与 TimePicker 相同,Placeholder(开始, 结束) 设置两端占位符。
import (
"time"
"github.com/tangoui/tango/ui"
)
var businessHours [2]time.Time
func page(c *ui.Context) {
ui.TimeRangePicker(c, &businessHours).Format("15:04").MinuteStep(15).
Placeholder("开门时间", "打烊时间").
DisabledHours(func(h int) bool { return h < 6 })
}内嵌时间面板 TimePanel
TimePanel 是 TimePicker 的下拉面板本身,可以直接放在页面或自定义弹层里。面板可聚焦:↑ / ↓ 改变当前列的值,← / → 切换列。
import (
"time"
"github.com/tangoui/tango/ui"
)
var at time.Time
func page(c *ui.Context) {
ui.TimePanel(c, &at).Format("15:04").Steps(1, 15, 1).Height(200)
ui.Text(c, at.Format("15:04"))
}尺寸、状态与外观
Size(ui.Small | ui.Large)、Status("error" | "warning")、Variant("filled" | "borderless" | "underlined")、Width、AllowClear(false)、Disabled(true) 调整外观与交互,用法与其他输入类组件一致。
import (
"time"
"github.com/tangoui/tango/ui"
)
var t time.Time
func page(c *ui.Context) {
ui.TimePicker(c, &t).Size(ui.Small)
ui.TimePicker(c, &t).Status("error").Placeholder("错误状态")
ui.TimePicker(c, &t).Variant("filled").AllowClear(false).Width(160)
}主题与样式
- 输入框外观沿用输入类组件的令牌;下拉面板的列宽、单元格高度和选中色与日期面板共用
theme.Components.DatePicker(DatePickerToken)中的TimeColumnWidth、TimeColumnHeight、TimeCellHeight、TimeCellHoverBg、TimeCellActiveBg等,三套风格切换无需改代码。 ConfigProvider.Defaults("TimePicker", ui.Props{...})支持format、placeholder、use12Hours、hourStep、minuteStep、secondStep、allowClear、width、size、variant、hideDisabledOptions;其中格式、12 小时制、步长和hideDisabledOptions对TimePanel同样生效。TimeRangePicker使用"TimeRangePicker"键。
注意事项
- 日期部分:TimePicker 只关心时分秒,新值的日期为当天。保存或比较时只取
Hour()、Minute()、Second(),不要依赖日期部分。 - 状态:值就是你绑定的
time.Time;下拉是否展开、正在键入的文本由组件按调用位置保存,循环渲染时用ui.Key区分。 - 表单校验:放进 Form 时,用
RulesFunc(func() string { ... })把时间格式化成字符串再配合ui.Required,或用ui.RulesOf配合ui.CheckValue直接检查time.Time。 - 无障碍:在表单项里自动以表单项标签命名,否则依次使用
A11yLabel、占位符;TimePanel默认名称取自语言包的“选择时间”。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.TimePicker
func TimePicker(c *Context, val *time.Time) *TimePickerWTimePicker adds a time input bound to *val (zero = empty). The date part of a new value is today's date.
ConfigProvider.Defaults("TimePicker", ...) keys: "format", "placeholder", "use12Hours", "hourStep", "minuteStep", "secondStep", "allowClear", "width", "size", "variant", "hideDisabledOptions", "placement", "showNow", "needConfirm".
返回 *TimePickerW,可链式调用的方法见 TimePickerW 方法。
ui.TimePanel
func TimePanel(c *Context, val *time.Time) *TimePanelWTimePanel adds hour/minute(/second) scroll columns bound to *val. It is focusable: Up/Down change the active column, Left/Right switch columns.
ConfigProvider.Defaults("TimePicker", ...) keys "format", "use12Hours", "hourStep", "minuteStep", "secondStep" and "hideDisabledOptions" apply.
返回 *TimePanelW,可链式调用的方法见 TimePanelW 方法。
TimePickerW 方法
ui.TimePickerW:TimePickerW is an input with a dropdown of time columns.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort). | A11yLabel(s string) *TimePickerW |
AllowClear | AllowClear toggles the clear button (on by default). | AllowClear(on bool) *TimePickerW |
Changed | Changed reports a committed value this frame. | Changed() bool |
Disabled | Disabled disables the picker. | Disabled(on bool) *TimePickerW |
DisabledHours | DisabledHours marks hours (0-23) that cannot be picked or typed. | DisabledHours(fn func(h int) bool) *TimePickerW |
DisabledMinutes | DisabledMinutes marks minutes of hour h that cannot be picked or typed. | DisabledMinutes(fn func(h, m int) bool) *TimePickerW |
DisabledSeconds | DisabledSeconds marks seconds of h:m that cannot be picked or typed. | DisabledSeconds(fn func(h, m, s int) bool) *TimePickerW |
Format | Format sets the Go time layout, e.g. "15:04:05" (default), "15:04" or with Use12Hours "03:04 PM" (AM / PM are shown with the Locale words). | Format(f string) *TimePickerW |
HideDisabledOptions | HideDisabledOptions leaves disabled hours, minutes and seconds out of the columns instead of greying them (Ant hideDisabledOptions). | HideDisabledOptions(on bool) *TimePickerW |
HourStep | HourStep(n int) *TimePickerW | |
IsOpen | IsOpen reports whether the dropdown is shown. | IsOpen() bool |
Layout | Layout applies layout options to the input frame. | Layout(o ...layout.Option) *TimePickerW |
MinuteStep | MinuteStep(n int) *TimePickerW | |
NeedConfirm | NeedConfirm keeps column picks as a draft until OK or Enter (Ant needConfirm; on by default for time pickers). | NeedConfirm(on bool) *TimePickerW |
Open | Open forces the dropdown open (screenshots, tests). | Open() *TimePickerW |
Placeholder | Placeholder(s string) *TimePickerW | |
Placement | Placement sets where the columns open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); they flip when there is no room. | Placement(p Placement) *TimePickerW |
SecondStep | SecondStep(n int) *TimePickerW | |
ShowNow | ShowNow toggles the Now link in the footer (Ant showNow, on by default). | ShowNow(on bool) *TimePickerW |
Size | Size(s ControlSize) *TimePickerW | |
Status | Status(s string) *TimePickerW | |
Steps | Steps sets the hour, minute and second steps at once. | Steps(h, m, s int) *TimePickerW |
Use12Hours | Use12Hours shows a 12-hour clock: hours 12, 01 … 11 and an AM / PM column; the default format becomes "03:04:05 PM". | Use12Hours() *TimePickerW |
Variant | Variant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined". | Variant(v string) *TimePickerW |
Width | Width(px float32) *TimePickerW |
TimePanelW 方法
ui.TimePanelW:TimePanelW is a standalone set of hour/minute/second columns.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (default: the Locale's SelectTime). | A11yLabel(s string) *TimePanelW |
Changed | Changed reports a new value this frame. | Changed() bool |
DisabledHours | DisabledHours marks hours (0-23) that cannot be picked. | DisabledHours(fn func(h int) bool) *TimePanelW |
DisabledMinutes | DisabledMinutes marks minutes of hour h that cannot be picked. | DisabledMinutes(fn func(h, m int) bool) *TimePanelW |
DisabledSeconds | DisabledSeconds marks seconds of h:m that cannot be picked. | DisabledSeconds(fn func(h, m, s int) bool) *TimePanelW |
Format | Format sets the Go layout that decides the columns ("15:04" or "15:04:05"). | Format(f string) *TimePanelW |
Height | Height sets the column height. | Height(px float32) *TimePanelW |
HideDisabledOptions | HideDisabledOptions leaves disabled options out of the columns. | HideDisabledOptions(on bool) *TimePanelW |
Steps | Steps sets the hour, minute and second steps. | Steps(h, m, s int) *TimePanelW |
Use12Hours | Use12Hours shows a 12-hour clock with an AM / PM column. | Use12Hours() *TimePanelW |
TimeRangePicker 的方法见 Go API 参考,与 TimePicker 同名方法含义一致。