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
- Variables
- type CancelledMsg
- type ConfirmOptions
- type Field
- type InvalidMsg
- type Model
- func (m Model) Bool(key string) bool
- func (m Model) FieldRect(i int) geom.Rect
- func (m Model) FocusedIndex() int
- func (m Model) Height(w int) int
- func (m Model) Help() []key.Binding
- func (m Model) HelpSections() []help.Section
- func (m Model) Init() tea.Cmd
- func (m Model) IsCapturingKeys() bool
- func (m Model) Rect() geom.Rect
- func (m *Model) Restore(s State)
- func (m *Model) SetRect(r geom.Rect)
- func (m *Model) SetTitle(s string)
- func (m Model) State() State
- func (m Model) Step() int
- func (m Model) String(key string) string
- func (m Model) Strings(key string) []string
- func (m Model) SubmitRect() geom.Rect
- func (m Model) Title() string
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) Value(key string) any
- func (m Model) Values() Values
- func (m Model) View() string
- type MultiSelectOptions
- type Options
- type PasswordOptions
- type SelectOptions
- type State
- type Step
- type Styles
- type SubmittedMsg
- type TextAreaOptions
- type TextOptions
- type Values
Constants ¶
const DefaultMaxWidth = 60
DefaultMaxWidth is how wide the card is when Options.MaxWidth is zero.
const DefaultVisible = 5
DefaultVisible is how many options a Select or MultiSelect shows before it scrolls.
Variables ¶
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 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 (Model) FieldRect ¶ added in v0.18.1
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
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
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) HelpSections ¶ added in v0.25.0
HelpSections groups moving between fields, committing the form, and the focused field's own keys.
func (Model) IsCapturingKeys ¶
IsCapturingKeys is always true: the form owns the keyboard while it is up.
func (*Model) Restore ¶ added in v0.46.0
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
SetRect is the area the form may use; it draws itself centred inside.
func (Model) SubmitRect ¶ added in v0.18.1
SubmitRect is where the submit (or Next) button was last drawn.
func (Model) Update ¶
Update handles keys and clicks, advances the banner, and forwards everything else to the focused field.
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().
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
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.