Documentation
¶
Overview ¶
Package datepicker is a keyboard-navigable calendar widget operating directly on time.Time (stdlib time only, no external date library).
Example ¶
Arrow keys move the cursor by a day or a week; Enter selects the date.
package main
import (
"fmt"
"time"
"github.com/ows4444/tui"
"github.com/ows4444/tui/datepicker"
)
func main() {
m := datepicker.New(time.Date(2026, time.March, 10, 0, 0, 0, 0, time.UTC))
m, _ = m.Update(tui.Key{Type: tui.KeyRight})
m, _ = m.Update(tui.Key{Type: tui.KeyDown})
fmt.Println(m.Cursor().Format("2006-01-02"))
_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(datepicker.SelectedMsg).Date.Format("Mon 2 Jan"))
}
Output: 2026-03-18 Wed 18 Mar
Index ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Cursor() time.Time
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyMap ¶
type KeyMap struct {
PrevDay keymap.Binding // move the cursor back one day
NextDay keymap.Binding // move the cursor forward one day
PrevWeek keymap.Binding // move the cursor back one week
NextWeek keymap.Binding // move the cursor forward one week
MonthStart keymap.Binding // move the cursor to the first of the month
MonthEnd keymap.Binding // move the cursor to the last of the month
Select keymap.Binding // confirm the highlighted date
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the keys a Model used before KeyMap existed.
type Model ¶
type Model struct {
Value time.Time
MinDate time.Time
MaxDate time.Time
Theme theme.Theme
// KeyMap holds the keys for each action. New fills it with
// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
// behaves as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left click on a
// day moves the cursor to it (a click on the highlighted day confirms it,
// like Enter) and the wheel moves the cursor by a month. Days outside
// [MinDate, MaxDate] ignore clicks. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the calendar (its
// first row is the weekday header); mouse events outside it are ignored.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a single-month calendar grid. Value is the last confirmed date (set by New and left untouched by navigation — only Enter updates it, via the caller applying SelectedMsg). MinDate and MaxDate, when non-zero, bound both cursor movement and which days View renders as enabled.
func New ¶
New builds a Model with the cursor (and Value) on value, or today if value is the zero time.Time.
func (Model) Bindings ¶
Bindings returns the actions the picker currently honours, with descriptions, for help text.
func (Model) LayoutNode ¶
LayoutNode adapts the calendar to a layout.Node. Measure reports its natural size (the weekday row plus one row per week of the cursor's month). Render fits the allotted Size: the weekday row stays, and when there are more weeks than rows it shows a window of weeks containing the cursor's week; columns are cut to the width. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the calendar as plain text for accessible output (see tui.Linearizer): the date the cursor is on (", unavailable" if it is outside MinDate and MaxDate), the confirmed value if there is one, and the allowed range if one is set. The month grid is not spoken.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentDatePicker, ...) and then by WithTokens.
func (Model) Update ¶
Update moves the cursor by day (Left/Right), by week (Up/Down), to the start/end of its month (Home/End), and confirms it on Enter, returning a Cmd that delivers SelectedMsg. Movement is clamped to [MinDate, MaxDate] whenever those are set (non-zero). With Mouse on it also handles tui.MouseEvent (see Model.Mouse). Any other Msg is a no-op.
type SelectedMsg ¶
SelectedMsg is delivered (via the Cmd Update returns) when the highlighted date is confirmed with Enter.