TimeField
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/time_field.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"time"
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
)
func init() {
registerSection("time_field", "inputs", func() core.Widget {
start := kit.TimeField(demoText("Start time · Small", "开始时间 · 小")).Segmented().Size(kit.TimeFieldSizeSmall)
seconds := kit.TimeField(demoText("Seconds precision · Segmented shortcuts", "精确到秒 · 分段快捷键")).Seconds().Hour12(false).SegmentKeys(true)
seconds.SetValue(23*time.Hour + 59*time.Minute + 58*time.Second)
ampm := kit.TimeField(demoText("12-hour clock · Large", "12 小时制 · 大")).Hour12(true).SegmentKeys(true).Size(kit.TimeFieldSizeLarge)
ampm.SetValue(13*time.Hour + 30*time.Minute)
start.SetValue(9*time.Hour + 30*time.Minute)
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(8).Items(el.Start).Child(start.Render(cx), seconds.Render(cx), ampm.Render(cx), el.Text(demoText("Segmented shortcuts: arrows change segments, two digits advance automatically, a/p selects AM/PM, and Delete resets. Plain mode commits on Enter or blur.", "分段快捷键模式:左右切段,两位数字自动跳段;a/p 切换时段,删除重置。普通模式仍按 Enter 或移焦提交。")))
}))
})
}
Enter the time of day. Supports whole string input and independent editing of hours, minutes and seconds.
start := kit.TimeField("Start time").Segmented()
start.SetValue(9*time.Hour + 30*time.Minute)
precise := kit.TimeField("End time").Seconds().Hour12(false)
precise.SetValue(17*time.Hour + 45*time.Minute + 30*time.Second)
Segmented()turns on segmented input;Seconds()enables both seconds segments and second precision. Tab / Shift+Tab Switch between sections, press Enter after input or move out of the section to submit; illegal numbers revert to submitted values.- In segment mode, ↑ ↓ adjusts the current segment by one unit, and PageUp / PageDown adjusts by ten units. Hours, minutes and seconds carry linkage, looping past midnight; one key press only triggers
OnChangeonce. - The segmented format follows
locale.Current().Clock12by default: Chinese is 24-hour format, English is 12-hour format.Hour12(bool)can be overridden explicitly. The 12-hour clock shows localized AM/PM buttons; switching periods retain minutes and seconds, showing 12 for both midnight and noon. Switching formats at runtime discards uncommitted text and retains the value. - When the above configuration is not called, the entire string of
HH:MMis retained. Input: accepts9:30,0930,930; ↑ ↓ adjusts minutes, PageUp / PageDown adjusts hours. Value()returnstime.Durationstarting at midnight; defaults to minutes andSeconds()to seconds.SetValuetakes the remainder of 24 hours and does not trigger a callback.Size(kit.TimeFieldSizeXSmall / TimeFieldSizeSmall / TimeFieldSizeMedium / TimeFieldSizeLarge)synchronously adjusts the frame, font size, clock icon, segment width and padding, applicable to full string and segmented modes. By default, Medium uses the theme control height; the minimum heights of the other three levels are 24, 28, and 40dp. Labels and error text retain the form's standard font size; switching sizes does not submit a draft or change values. Double-rate size validation for whole strings and segments passed.SetDisabled,SetError. Discard drafts when component or ancestor is disabled, show committed values when restored.
Agent: Each segment is a separate textbox, whose name consists of the localized "hour/minute/second" and field label; FocusID() points to the small segment. AM/PM are buttons with localized "slot" names.
Verify: go run ./examples/components -section time_field, add -theme dark to check the dark theme.
SegmentKeys(true) enables fast segmented editing and automatically turns on segmented mode; pass false to restore the original draft editing. Compared to default mode:
- When the focus is obtained, the entire paragraph is selected, and the left and right arrows submit the current draft and move it to the adjacent paragraph; moving the first paragraph to the left and the last paragraph to the right will not leave the control, and Tab can still leave.
- Enter or paste a valid two-digit number to submit immediately and focus the next segment; the focus period button is after the last digit segment of the 12-hour clock. Invalid two-digit numbers will not be automatically submitted or skipped.
- ↑ ↓. PageUp / PageDown only circulates within the current segment range and does not carry to other segments. The 12-hour period is 1–12, the 24-hour period is 0–23, and the minutes and seconds are 0–59.
- Backspace / Delete resets the current segment to 0; resets the 12-hour segment to 12 and retains the current AM/PM.
- The a/p of the 12-hour clock is set to am/pm; the time period button supports left and right segmentation, up and down switching, and deletion to return to am.
Continuous input, paste skips, independent loops, delete reset, period shortcuts, disabled and double size verified; Enter/focus shift commit and carry behavior still retained in default mode.