Documentation
¶
Overview ¶
Package form is a vertical form component: a sequence of Text, Select, and Confirm fields with tab/shift-tab focus cycling and a submit button.
Each field renders as a bordered, titled component (Text wraps pkg/input, Confirm wraps pkg/toggle, Select owns its own pane), so the field's Label sits on the border as a pane title. Focus is signalled by the border color flipping from BorderInactive to BorderActive — there's no "▸" prefix or inline label line.
Usage:
f := form.New(theme.Dark().Form().With([]form.Field{
form.Text(form.TextOptions{Key: "name", Label: "Name"}),
form.Select(form.SelectOptions{Key: "role", Label: "Role",
Options: []string{"admin", "user"}}),
form.Confirm(form.ConfirmOptions{Key: "agree", Label: "I agree"}),
}))
The form emits form.SubmittedMsg (with all values keyed by Field.Key) when the user presses enter on the submit button, and form.CancelledMsg on esc. The enclosing screen's IsCapturingKeys should mirror Model.IsCapturingKeys.
Index ¶
- type CancelledMsg
- type ConfirmOptions
- type Field
- type Model
- func (m Model) Bool(key string) bool
- func (m Model) Help() []key.Binding
- func (m Model) Init() tea.Cmd
- func (m Model) IsCapturingKeys() bool
- func (m *Model) SetDimensions(w, h int)
- func (m *Model) SetStyles(s Styles)
- func (m Model) String(key string) string
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) Value(key string) any
- func (m Model) Values() map[string]any
- func (m Model) View() string
- type Options
- type SelectOptions
- type Styles
- type SubmittedMsg
- type TextOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfirmOptions ¶
ConfirmOptions configures a Confirm field.
type Field ¶
type Field interface {
Key() string
Update(tea.Msg) (Field, tea.Cmd)
// View renders the field at the given outer width. focused tells the
// field whether it currently owns input — fields use it to flip border
// colors and expand inline (e.g. Select shows all options when focused).
View(width int, focused bool) string
Value() any
Focus() tea.Cmd
Blur()
SetStyles(*Styles)
// Help returns the keys this field responds to — typically delegates
// to the embedded component's Help().
Help() []key.Binding
}
Field is the contract each form entry satisfies. Use the Text, Select, and Confirm constructors — don't implement Field yourself unless you need a custom entry kind. The form stores fields by pointer and mutates them in place on Update/Focus/Blur.
func Confirm ¶
func Confirm(o ConfirmOptions) Field
Confirm returns a yes/no field backed by pkg/toggle. Label becomes the toggle pane's title.
func Select ¶
func Select(o SelectOptions) Field
Select returns a single-choice field backed by pkg/list. While focused, up/down move the cursor; Value returns the highlighted option. Label becomes the list pane's title. Long option lists scroll within Height.
func Text ¶
func Text(opts TextOptions) Field
Text returns a single-line text field backed by pkg/input. Label becomes the input pane's title.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the form component.
func (Model) Bool ¶
Bool returns a field's value as a bool, or false if the field is missing or the value isn't a bool.
func (Model) Help ¶
Help returns the form's own bindings plus the focused field's. The enclosing screen typically returns these directly from its own Help().
func (Model) IsCapturingKeys ¶
IsCapturingKeys always returns true while the form is active — use it from the enclosing screen's IsCapturingKeys() so the app shell keeps its global keys (q / t / esc) out of the form.
func (*Model) SetDimensions ¶
SetDimensions satisfies layout.Sized so the form can be placed into a layout tree via layout.Sized(&formModel).
func (Model) String ¶
String returns a field's value as a string, or "" if the field is missing or the value isn't a string.
func (Model) Update ¶
Update handles tab/shift-tab focus cycling, enter, esc, and forwards everything else to the focused field.
type Options ¶
type Options struct {
Width, Height int
Fields []Field
// SubmitText is the label on the submit button. Defaults to "Submit".
SubmitText string
// FieldSpacing is the number of blank lines between adjacent fields and
// before the submit button. Defaults to 0 (borders touch). Set higher
// for a looser layout.
FieldSpacing *int
Styles Styles
}
Options configures a new Form. All fields are optional except Fields.
type SelectOptions ¶
type SelectOptions struct {
Key, Label string
Options []string
Initial int // initial cursor index
// Height is the total field height including borders. Defaults to
// min(len(Options)+2, 6) — i.e. auto-fit up to 4 visible rows; longer
// option lists scroll within the fixed height.
Height int
}
SelectOptions configures a Select field.
type Styles ¶
type Styles struct {
// Input styles the text inside text fields (mapped to input.TextStyle).
Input lipgloss.Style
// Placeholder styles the placeholder text inside empty text fields.
Placeholder lipgloss.Style
// CursorColor is the foreground for the text-input cursor glyph.
CursorColor lipgloss.TerminalColor
// Selected styles the active item in Select and the chosen side of Confirm.
Selected lipgloss.Style
// PaneActive / PaneInactive color the field's border. Forwarded to each
// field's pane (input.SetActiveColor / SetInactiveColor or pane defaults).
PaneActive lipgloss.TerminalColor
PaneInactive lipgloss.TerminalColor
// Submit and SubmitActive style the submit button (unfocused / focused).
Submit lipgloss.Style
SubmitActive lipgloss.Style
}
Styles bundles the visual knobs the form passes down to its fields and uses for the submit button. Populate via theme.Form() or set directly on Options.
type SubmittedMsg ¶
SubmittedMsg is emitted on enter over the submit button. Values maps each field's Key to its Value() — type depends on the field (string for Text and Select, bool for Confirm).
type TextOptions ¶
type TextOptions struct {
Key, Label, Placeholder, Initial string
}
TextOptions configures a Text field.