RangePicker 日期范围选择
用一个输入框触发两个相邻面板,选择并保存日期、周期或日期时间范围。
何时使用
- 需要查询报表、日志或日程的开始和结束日期时。
- 需要把两个端点放在紧凑输入框中,并支持键盘输入或预设范围时。
- 需要直接把日期面板嵌入页面时,用 DatePickerPanel 日期面板。
- 只选择一个日期时,用 DatePicker 日期选择框。
- 只选择时刻、不包含日期时,用 TimePicker 时间选择框。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"time"
"github.com/tangoui/tango/ui"
)
var period [2]time.Time
func page(c *ui.Context) {
picker := ui.RangePicker(c, &period).Placeholder("开始日期", "结束日期")
if picker.Changed() && !period[0].IsZero() && !period[1].IsZero() {
ui.Text(c, period[0].Format("2006-01-02")+" 至 "+period[1].Format("2006-01-02"))
}
}范围绑定到 *[2]time.Time,零值表示没有对应端点。第一次点击选择开始端,第二次点击选择结束端,结果会自动按时间先后排序;也可以在两个输入段中直接键入日期。
常见用法
周、月、季度和年范围
Picker 同时改变两个端点的粒度。周期范围保存为周期第一天的 UTC 零点,ShowTime 对这些粒度不起作用。
import (
"time"
"github.com/tangoui/tango/ui"
)
var (
weeks [2]time.Time
months [2]time.Time
quarters [2]time.Time
years [2]time.Time
)
func page(c *ui.Context) {
ui.RangePicker(c, &weeks).Picker(ui.PickerWeek)
ui.RangePicker(c, &months).Picker(ui.PickerMonth)
ui.RangePicker(c, &quarters).Picker(ui.PickerQuarter)
ui.RangePicker(c, &years).Picker(ui.PickerYear)
}日期时间范围
ShowTime 在两个日期面板下方增加时间列;草稿会在点“确定”或按 Enter 后提交。使用不含秒的 Go 时间布局可以隐藏秒列。
import (
"time"
"github.com/tangoui/tango/ui"
)
var meeting [2]time.Time
func page(c *ui.Context) {
ui.RangePicker(c, &meeting).ShowTime().
Format("2006-01-02 15:04").Placeholder("开始时间", "结束时间")
}预设范围
Presets 在面板中显示快捷项,点击后直接写入 DatePreset.Range。日期值建议明确使用 UTC,避免本地时区导致边界日期变化。
import (
"time"
"github.com/tangoui/tango/ui"
)
var reportPeriod [2]time.Time
func page(c *ui.Context) {
today := time.Now().UTC().Truncate(24 * time.Hour)
ui.RangePicker(c, &reportPeriod).Presets(
ui.DatePreset{Label: "最近 7 天", Range: [2]time.Time{today.AddDate(0, 0, -6), today}},
ui.DatePreset{Label: "最近 30 天", Range: [2]time.Time{today.AddDate(0, 0, -29), today}},
)
}禁用日期与时间
DisabledDate 禁止日期,DisabledTime 按开始端或结束端分别返回不可选的小时、分钟和秒。禁用的端点不会被键盘输入绕过。
import (
"time"
"github.com/tangoui/tango/ui"
)
var booking [2]time.Time
func weekend(day time.Time) bool {
return day.Weekday() == time.Saturday || day.Weekday() == time.Sunday
}
func page(c *ui.Context) {
ui.RangePicker(c, &booking).DisabledDate(weekend).ShowTime().
DisabledTime(func(day time.Time, end bool) ui.DisabledTimes {
return ui.DisabledTimes{
Hours: func(hour int) bool { return hour < 9 || hour >= 18 },
}
})
}回调的 end 为 false 表示开始端,为 true 表示结束端;day 是对应端的日期。
外观、宽度与无障碍
Variant 支持 outlined、filled、borderless 和 underlined;Status、Size、Width 和 A11yLabel 可用于表单反馈、布局和辅助技术。
import "github.com/tangoui/tango/ui"
var styledPeriod [2]time.Time
func page(c *ui.Context) {
ui.RangePicker(c, &styledPeriod).Variant("filled").Width(320).
Status("warning").Size(ui.Large).A11yLabel("报告时间范围")
}主题与样式
- RangePicker 的日历、范围带、预览范围和时间列共用
theme.Components.DatePicker(DatePickerToken);触发器边框沿用输入框令牌。 - 日期范围相关令牌包括
CellRangeBg、CellRangePreviewBg、CellRangeHoverBorder、CellActiveBg、PanelBg、TimeCellActiveBg和FooterHeight。 ConfigProvider.Defaults("RangePicker", ui.Props{...})支持format、separator、allowClear、showTime、width、size、variant、picker和placeholder(类型为[2]string)。显式链式方法优先。
注意事项
- 提交时机:普通日期范围第二次选择后提交;
ShowTime的日期和时间先是草稿,点“确定”或按 Enter 才提交。Changed()只报告已提交的范围变化。 - 状态:使用稳定的
*[2]time.Time变量保存值;打开状态、当前端点、月份和键盘光标由组件按调用位置保存。循环中使用ui.Key区分多个范围框。 - 时区:默认日期布局是 UTC 日期语义;
ShowTime解析时会参考已有值的 location,业务层应统一时间存储和展示时区。 - 无障碍:RangePicker 暴露可展开的组合框语义,打开后可用方向键、PageUp / PageDown、Enter 和 Escape;独立使用时用
A11yLabel。 - 测试与截图:
Open()强制打开面板,IsOpen()查询状态,适合测试和 gallery,不要在正常交互中永久调用Open()。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.RangePicker
func RangePicker(c *Context, val *[2]time.Time) *RangePickerWRangePicker adds a date range input bound to *val ([start, end], zero = empty). The first click picks one end, the second click the other; the result is ordered. Picker switches to week, month, quarter or year ranges. Each end can also be typed: printable keys edit the active end in place; Enter (or leaving the field) applies valid text, invalid text is dropped.
返回 *RangePickerW,可链式调用的方法见 RangePickerW 方法。
RangePickerW 方法
ui.RangePickerW:RangePickerW selects a date range in two adjacent month panels.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; otherwise the two placeholders). | A11yLabel(s string) *RangePickerW |
AllowClear | AllowClear(on bool) *RangePickerW | |
Changed | Changed reports a committed range this frame. | Changed() bool |
Disabled | Disabled disables the picker. | Disabled(on bool) *RangePickerW |
DisabledDate | DisabledDate marks days that cannot be picked. | DisabledDate(fn func(time.Time) bool) *RangePickerW |
DisabledTime | DisabledTime disables hours, minutes and seconds per day in the ShowTime columns (Ant disabledTime with type "start" / "end"): fn receives the drafted day of an end (UTC midnight) and end=false for the start, true for the end. | DisabledTime(fn func(day time.Time, end bool) DisabledTimes) *RangePickerW |
Format | Format(f string) *RangePickerW | |
IsOpen | IsOpen reports whether the panels are shown. | IsOpen() bool |
Layout | Layout(o ...layout.Option) *RangePickerW | |
MaxDate | MaxDate limits the pickable dates (Ant maxDate), like MinDate. | MaxDate(t time.Time) *RangePickerW |
MinDate | MinDate limits the pickable dates (Ant minDate): periods wholly before t cannot be picked or typed, and the panel arrows stop at t's page. | MinDate(t time.Time) *RangePickerW |
Open | Open forces the panels open (screenshots, tests). | Open() *RangePickerW |
Order | Order toggles sorting the two ends (Ant order, on by default). | Order(on bool) *RangePickerW |
Picker | Picker sets the granularity of both ends: PickerDate (default), PickerWeek, PickerMonth, PickerQuarter or PickerYear (Ant picker, Element type="monthrange" / "yearrange"). | Picker(p PickerType) *RangePickerW |
Placeholder | Placeholder(start, end string) *RangePickerW | |
Placement | Placement sets where the panels open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); they flip when there is no room. | Placement(p Placement) *RangePickerW |
Presets | Presets(p ...DatePreset) *RangePickerW | |
Separator | Separator sets the text between the two ends (Ant separator; default the Locale's RangeSeparator). | Separator(s string) *RangePickerW |
ShowTime | ShowTime adds start / end time columns under the month panels and an OK button (Ant RangePicker showTime, Element type="datetimerange"): days and times are drafts until OK (or Enter on the picked end day) commits. | ShowTime() *RangePickerW |
Size | Size(s ControlSize) *RangePickerW | |
Status | Status(s string) *RangePickerW | |
Variant | Variant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined". | Variant(v string) *RangePickerW |
Width | Width(px float32) *RangePickerW |