form

package
v0.46.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 16 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package form is a form drawn as one card: each field a label over its value, a note under it saying what it is for, Submit and Cancel buttons, and a line of the keys that work. The focused field carries a bar down its left edge, and typed values sit in a well a tone lighter than the card.

o := t.Form()
o.Title = "Sign in"
o.Fields = []form.Field{
    form.Text(form.TextOptions{Key: "user", Label: "Username", Required: true,
        Note: "The name you sign in with."}),
    form.Password(form.PasswordOptions{Key: "pass", Label: "Password", Required: true}),
    form.Select(form.SelectOptions{Key: "role", Label: "Role",
        Options: []string{"admin", "editor", "viewer"}, RequirePick: true}),
}
s.form = form.New(o)

The form sizes and places itself: it is at most MaxWidth columns wide, exactly as tall as its fields, and centred in whatever rect it is given. A screen that is only the form hands it the whole body with layout.Sized(&s.form); a modal puts the same node in a ZStack over the screen and sets Options.Modal. Height reports the rows it needs, for a host that wants to know.

Steps make it a wizard: each step is a page of fields with its own title, Back and Next move between them, esc goes back a step, and a step can skip itself based on what was answered before it. Values from every step arrive together in one SubmittedMsg.

The form validates: Required, RequirePick, MultiSelect's Min and any Validate run when the user moves past a step or submits. A refused step flags the offending fields, moves focus to the first, and posts InvalidMsg; a flagged field then re-checks on every keystroke so a fix is acknowledged at once. A screen can never receive values the form refused.

Enter moves to the next field and, on the last, submits (or moves to the next step). Tab and shift+tab move between fields and buttons. esc cancels, or goes back a step. The form always captures keys: mirror IsCapturingKeys in the screen's own, and leave on CancelledMsg.

Banner, when Options.Banner has Text, is drawn above the card on an intro screen — big block letters with a moving gradient (pkg/banner). It is dropped when the rect is too narrow or short for it, and never drawn on a modal.

Index

Constants

View Source
const DefaultMaxWidth = 60

DefaultMaxWidth is how wide the card is when Options.MaxWidth is zero.

View Source
const DefaultVisible = 5

DefaultVisible is how many options a Select or MultiSelect shows before it scrolls.

Variables

View Source
var ErrRequired = errors.New("required")

ErrRequired is what a Required field reports while it is empty, and what a Select with RequirePick or a MultiSelect short of Min reports until enough is chosen.

Functions

This section is empty.

Types

type CancelledMsg

type CancelledMsg struct{}

CancelledMsg is posted on esc (on the first step) or the Cancel button.

type ConfirmOptions

type ConfirmOptions struct {
	Key, Label string
	Note       string
	Initial    bool
	// Yes and No label the two sides. Default "yes" and "no".
	Yes, No string
	// Validate reports why the answer is unacceptable, or nil. A yes/no
	// always has a value, so there is no Required — say "must accept" here.
	Validate func(any) error
}

ConfirmOptions configures a Confirm field.

type Field

type Field interface {
	// Key is what the field's value is filed under in Values.
	Key() string
	// contains filtered or unexported methods
}

Field is one entry in a form. Build it with Text, Password, Select, Confirm, MultiSelect or TextArea; the set is closed, because the form draws every field's label, note, well and focus bar itself.

func Confirm

func Confirm(o ConfirmOptions) Field

Confirm is a yes/no answer: ←/→ (or y/n) pick a side and space flips it.

func MultiSelect added in v0.46.0

func MultiSelect(o MultiSelectOptions) Field

MultiSelect is any number of choices from a list that is always open. ↑/↓ move the cursor and space toggles the option under it; the value is the chosen options in list order.

func Password added in v0.22.0

func Password(o PasswordOptions) Field

Password is a Text field whose characters draw as MaskChar. Its value arrives in SubmittedMsg as the real text.

func Select

func Select(o SelectOptions) Field

Select is a single choice from a list that is always open. The option under the cursor is the choice: ↑/↓ move it.

func Text

func Text(o TextOptions) Field

Text is a single-line text field.

func TextArea added in v0.46.0

func TextArea(o TextAreaOptions) Field

TextArea is multi-line text. Enter inserts a newline; tab and shift+tab leave the field.

type InvalidMsg added in v0.18.1

type InvalidMsg struct{ Keys []string }

InvalidMsg is posted when a step or a submit is refused. Keys names the failing fields, in order. The form has already flagged them and moved focus to the first, so a screen needs this only to react (a log line, a count).

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is a form.

func New

func New(opts Options) Model

New builds a form, focused on its first field.

func (Model) Bool

func (m Model) Bool(key string) bool

Bool is one field's value as a bool, or false.

func (Model) FieldRect added in v0.18.1

func (m Model) FieldRect(i int) geom.Rect

FieldRect is where field i of the current step was last drawn — its label, value and note. Empty before the first View, or for an index out of range.

func (Model) FocusedIndex added in v0.18.1

func (m Model) FocusedIndex() int

FocusedIndex is the focused stop on the current step: a field's index, or len(fields) and up for the buttons.

func (Model) Height added in v0.46.0

func (m Model) Height(w int) int

Height is how many rows the form needs at width w: the card, and on an intro screen the banner above it and the help line below.

func (Model) Help

func (m Model) Help() []key.Binding

Help is the flat form of HelpSections.

func (Model) HelpSections added in v0.25.0

func (m Model) HelpSections() []help.Section

HelpSections groups moving between fields, committing the form, and the focused field's own keys.

func (Model) Init

func (m Model) Init() tea.Cmd

Init starts the cursor blink and the banner.

func (Model) IsCapturingKeys

func (m Model) IsCapturingKeys() bool

IsCapturingKeys is always true: the form owns the keyboard while it is up.

func (Model) Rect added in v0.18.0

func (m Model) Rect() geom.Rect

Rect is where the card was last drawn.

func (*Model) Restore added in v0.46.0

func (m *Model) Restore(s State)

Restore replays s onto the form, matching fields by Key. A State from a form never built restores nothing.

func (*Model) SetRect added in v0.18.0

func (m *Model) SetRect(r geom.Rect)

SetRect is the area the form may use; it draws itself centred inside.

func (*Model) SetTitle added in v0.40.0

func (m *Model) SetTitle(s string)

SetTitle changes the form's title.

func (Model) State added in v0.46.0

func (m Model) State() State

State snapshots the form.

func (Model) Step added in v0.46.0

func (m Model) Step() int

Step is the index of the wizard step on screen.

func (Model) String

func (m Model) String(key string) string

String is one field's value as a string, or "".

func (Model) Strings added in v0.46.0

func (m Model) Strings(key string) []string

Strings is one field's value as a []string, or nil.

func (Model) SubmitRect added in v0.18.1

func (m Model) SubmitRect() geom.Rect

SubmitRect is where the submit (or Next) button was last drawn.

func (Model) Title added in v0.40.0

func (m Model) Title() string

Title is the form's title.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update handles keys and clicks, advances the banner, and forwards everything else to the focused field.

func (Model) Value

func (m Model) Value(key string) any

Value is one field's current value, or nil for an unknown key.

func (Model) Values

func (m Model) Values() Values

Values is every field's current value by Key, on every step.

func (Model) View

func (m Model) View() string

View draws the form centred in its rect, and records where everything went so a click can find it.

type MultiSelectOptions added in v0.46.0

type MultiSelectOptions struct {
	Key, Label string
	Note       string
	Options    []string
	// Initial are the options chosen to begin with, by index.
	Initial []int
	Visible int
	// Min is how many must be chosen; a positive Min makes the field
	// required and puts a "*" on its label. Max caps the choice; zero is no
	// cap, and a toggle past it is ignored.
	Min, Max int
	Validate func(any) error
}

MultiSelectOptions configures a MultiSelect field.

type Options

type Options struct {
	// Title names the form on its frame.
	Title string
	// Fields is a single-page form. Steps, when set, replaces it.
	Fields []Field
	// Steps make the form a wizard: one page each, in order.
	Steps []Step

	// SubmitText labels the button that submits. Defaults to "Submit".
	SubmitText string
	// NextText, BackText and CancelText label the wizard's buttons and
	// Cancel. Default "Next", "Back" and "Cancel".
	NextText, BackText, CancelText string

	// MaxWidth caps the card's width. Defaults to DefaultMaxWidth.
	MaxWidth int
	// FieldSpacing is the number of blank rows between fields. Defaults to
	// 1.
	FieldSpacing *int
	// Modal draws the form as a modal: the help line moves inside the card,
	// since below it would sit on whatever the modal covers, and the banner
	// is never drawn.
	Modal bool
	// Banner is drawn above the card when its Text is set. theme.Form()
	// fills its colours.
	Banner banner.Options

	Styles Styles

	// The frame. theme.Form() fills them from the surface's overlay form.
	ActiveColor    lipgloss.TerminalColor
	InactiveColor  lipgloss.TerminalColor
	ActiveBorder   lipgloss.Border
	InactiveBorder lipgloss.Border
	Glyphs         glyph.Set
	SlotBrackets   pane.SlotBracketStyle
	Surface        surface.Resolved
}

Options configures a form. Start from theme.Form().

func (Options) With

func (opts Options) With(fields []Field) Options

With returns a copy of opts with Fields replaced, for chaining from a theme builder: t.Form().With([]form.Field{…}).

type PasswordOptions added in v0.22.0

type PasswordOptions struct {
	Key, Label, Placeholder, Initial string
	Note                             string
	CharLimit                        int
	// MaskChar is the glyph shown per typed character. Defaults to '•'.
	MaskChar rune
	Required bool
	Validate func(any) error
}

PasswordOptions configures a Password field. Every field means what its TextOptions namesake means; Validate sees the real text, not the mask.

type SelectOptions

type SelectOptions struct {
	Key, Label string
	Note       string
	Options    []string
	// Initial is the option chosen to begin with, or — with RequirePick —
	// the one chosen when the field is first reached.
	Initial int
	// Visible is how many options show at once; past it the list scrolls
	// under a fixed "↑ n · ↓ n more" row. Defaults to DefaultVisible.
	Visible int

	// RequirePick starts with nothing chosen. Reaching the field chooses
	// Initial, so the choice is never invisible, and a form submitted
	// without reaching it is refused. The label gains a "*".
	RequirePick bool
	// Validate reports why the chosen option is unacceptable, or nil.
	Validate func(any) error
}

SelectOptions configures a Select field.

type State added in v0.46.0

type State struct {
	// contains filtered or unexported fields
}

State is what the user did to a form: the step, focus, every value, and which fields are flagged. Take it before a theme rebuild and Restore it onto the form built from the new Options.

type Step added in v0.46.0

type Step struct {
	// Title names the step; it follows the form's title on the frame.
	Title  string
	Fields []Field
	// Skip leaves the step out when it reports true for the answers so far.
	// It is asked whenever the user moves past a neighbouring step, so a
	// step can come and go as earlier answers change.
	Skip func(Values) bool
}

Step is one page of a wizard.

type Styles

type Styles struct {
	// Text styles values and option labels.
	Text lipgloss.Style
	// Label and LabelFocused style a field's label, and LabelFocused the
	// frame's step progress too.
	Label, LabelFocused lipgloss.Style
	// Note styles the note under a field, and the "more" row of a long list.
	Note        lipgloss.Style
	Placeholder lipgloss.Style
	CursorColor lipgloss.TerminalColor
	// Bar styles the focus bar and a MultiSelect's cursor.
	Bar lipgloss.Style
	// Chosen and Unchosen style the radio and check glyphs.
	Chosen, Unchosen lipgloss.Style
	// Well and WellFocused are the background behind a typed value; nil
	// draws none.
	Well, WellFocused lipgloss.TerminalColor
	// Error styles an invalid field's label and its message.
	Error lipgloss.Style
	// Button and ButtonActive style the buttons, idle and focused. Chips
	// draws them as "  label  " filled, in place of "[ label ]".
	Button, ButtonActive lipgloss.Style
	Chips                bool
	// HelpKey and HelpDesc style the help line.
	HelpKey, HelpDesc lipgloss.Style
}

Styles are the form's colours. theme.Form() fills them; set a field only to deviate from the palette.

type SubmittedMsg

type SubmittedMsg struct{ Values Values }

SubmittedMsg is posted when the user submits a form whose every reached field is valid. Values holds each field's value by Key, from every step the user went through; steps skipped by their Skip are left out.

type TextAreaOptions added in v0.46.0

type TextAreaOptions struct {
	Key, Label, Placeholder, Initial string
	Note                             string
	// Rows is how many lines the field shows; longer text scrolls inside
	// them. Defaults to 3.
	Rows      int
	CharLimit int
	Required  bool
	Validate  func(any) error
}

TextAreaOptions configures a TextArea field.

type TextOptions

type TextOptions struct {
	Key, Label, Placeholder, Initial string
	// Note says what the field is for, drawn muted under it and wrapped to
	// its width.
	Note string
	// CharLimit caps the value's length; zero is no limit.
	CharLimit int

	// Required rejects an empty value. The label gains a "*" so the
	// obligation is visible before anyone submits.
	Required bool
	// Validate reports why the value is unacceptable, or nil. Runs after
	// Required.
	Validate func(any) error
}

TextOptions configures a Text field.

type Values added in v0.46.0

type Values map[string]any

Values are a form's answers by field Key: a string for Text, Password, TextArea and Select (empty until a RequirePick select is chosen), a bool for Confirm, and a []string for MultiSelect.

func (Values) Bool added in v0.46.0

func (v Values) Bool(key string) bool

Bool is v[key] as a bool, or false.

func (Values) String added in v0.46.0

func (v Values) String(key string) string

String is v[key] as a string, or "".

func (Values) Strings added in v0.46.0

func (v Values) Strings(key string) []string

Strings is v[key] as a []string, or nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL