form

package
v0.1.35 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package form provides a reusable, typed form model for create/edit flows.

A Form is composed of typed fields (text, select/enum, bool toggle, numeric) with vertical focus traversal, per-field validation and Submit/Cancel actions. It renders as a plain string for a hosting page's content area (not a modal), and emits FormSubmitMsg / FormCancelMsg on the respective actions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name      string    // key used in the submitted Values map
	Label     string    // human-readable label
	Type      FieldType // field kind
	Required  bool      // whether an empty value is rejected
	Options   []string  // choices for Select fields
	Validator Validator // optional custom validation
	Default   string    // initial value (text/numeric) or default option
}

Field describes a single form field.

type FieldType

type FieldType int

FieldType enumerates the supported field kinds.

const (
	// Text is a free-form text input.
	Text FieldType = iota
	// Select cycles through a fixed set of Options.
	Select
	// Bool is a true/false toggle.
	Bool
	// Numeric is a text input constrained to numeric values.
	Numeric
)

type Form

type Form struct {
	core.BaseComponent
	// contains filtered or unexported fields
}

Form is a reusable form component.

func New

func New(fields []Field) *Form

New creates a Form from the given field definitions.

func (*Form) Focus

func (f *Form) Focus() tea.Cmd

Focus focuses the form's first field.

func (*Form) Init

func (f *Form) Init() tea.Cmd

Init implements the Bubble Tea model contract.

func (*Form) SetDimensions

func (f *Form) SetDimensions(width, height int)

SetDimensions sets the component dimensions and adjusts input widths.

func (*Form) Update

func (f *Form) Update(msg tea.Msg) (tea.Cmd, bool)

Update handles a message and reports whether it was consumed.

func (*Form) Validate

func (f *Form) Validate() bool

Validate runs validation on every field, recording inline errors, and reports whether the whole form is valid.

func (*Form) Values

func (f *Form) Values() map[string]string

Values returns the current field values keyed by field name.

func (*Form) View

func (f *Form) View() string

View renders the form as a plain string for the content area.

type FormCancelMsg

type FormCancelMsg struct{}

FormCancelMsg is emitted when the form is cancelled.

type FormSubmitMsg

type FormSubmitMsg struct {
	Values map[string]string
}

FormSubmitMsg is emitted when the form is submitted with all fields valid.

type Validator

type Validator func(value string) error

Validator validates a raw field value, returning a non-nil error to reject it.

Jump to

Keyboard shortcuts

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