forms

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package forms provides reusable form components for editing GCP resources. It includes FormField (individual inputs), FormSection (field groups), and FormView (complete forms with navigation and validation).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateAll

func ValidateAll(validators ...Validator) func(value any) []error

ValidateAll runs all validators and collects all errors

func ValidateGCPLabelKey

func ValidateGCPLabelKey(value any) error

ValidateGCPLabelKey validates a GCP label key

func ValidateGCPLabelValue

func ValidateGCPLabelValue(value any) error

ValidateGCPLabelValue validates a GCP label value

func ValidateGCPResourceName

func ValidateGCPResourceName(value any) error

ValidateGCPResourceName validates a GCP resource name

func ValidateGCSBucketName

func ValidateGCSBucketName(value any) error

ValidateGCSBucketName validates a GCS bucket name according to GCP rules

func ValidateNotEmpty

func ValidateNotEmpty(value any) error

ValidateNotEmpty is a simpler alias for ValidateRequired

func ValidateRequired

func ValidateRequired(value any) error

ValidateRequired returns an error if the value is empty

Types

type Component

type Component interface {
	Update(msg tea.Msg) tea.Cmd
	View() string
	SetSize(width, height int)
}

Component defines the interface for form-related UI components

type Field

type Field struct {
	// Configuration
	ID          string
	Label       string
	Type        FieldType
	Required    bool
	HelpText    string
	Placeholder string
	Validator   Validator

	// Options for dropdown/multiselect
	Options []Option

	// Visibility
	Hidden bool // When true, field is not rendered, not validated, not navigable
	// contains filtered or unexported fields
}

Field represents a single form input field

func NewDropdownField

func NewDropdownField(id, label string) *Field

NewDropdownField creates a dropdown selection field

func NewField

func NewField(id, label string, fieldType FieldType) *Field

NewField creates a new form field with the given configuration

func NewMultiSelectField

func NewMultiSelectField(id, label string) *Field

NewMultiSelectField creates a multi-selection field

func NewNumberField

func NewNumberField(id, label string) *Field

NewNumberField creates a numeric input field

func NewReadOnlyField

func NewReadOnlyField(id, label string, value string) *Field

NewReadOnlyField creates a read-only display field

func NewTextAreaField

func NewTextAreaField(id, label string) *Field

NewTextAreaField creates a multi-line text input field

func NewTextField

func NewTextField(id, label string) *Field

NewTextField creates a text input field

func NewToggleField

func NewToggleField(id, label string) *Field

NewToggleField creates a boolean toggle field

func (*Field) Blur

func (f *Field) Blur()

Blur removes focus from this field

func (*Field) EstimatedHeight

func (f *Field) EstimatedHeight() int

EstimatedHeight returns the approximate number of lines this field occupies. Used by scrollToFocused to calculate viewport offsets.

func (*Field) Focus

func (f *Field) Focus()

Focus sets focus on this field

func (*Field) GetStringValue

func (f *Field) GetStringValue() string

GetStringValue returns the field value as a string

func (*Field) GetValue

func (f *Field) GetValue() any

GetValue returns the current field value

func (*Field) HasError

func (f *Field) HasError() bool

HasError returns true if the field has a validation error

func (*Field) IsDirty

func (f *Field) IsDirty() bool

IsDirty returns true if the value has changed from the original

func (*Field) IsEditable

func (f *Field) IsEditable() bool

IsEditable returns true if the field can be edited

func (*Field) IsFocused

func (f *Field) IsFocused() bool

IsFocused returns true if the field is focused

func (*Field) IsTextInput

func (f *Field) IsTextInput() bool

IsTextInput returns true if this field accepts free text input (and thus should capture character keys instead of letting them be handled globally)

func (*Field) SetCharLimit

func (f *Field) SetCharLimit(limit int) *Field

SetCharLimit sets the character limit for text input

func (*Field) SetHelpText

func (f *Field) SetHelpText(text string) *Field

SetHelpText sets the help text shown below the field

func (*Field) SetHidden

func (f *Field) SetHidden(hidden bool) *Field

SetHidden controls whether this field is rendered, validated, or navigable.

func (*Field) SetOptions

func (f *Field) SetOptions(options []Option) *Field

SetOptions sets the available options for dropdown/multiselect fields. Clamps selectedIndex and scroll offset to prevent out-of-bounds access.

func (*Field) SetOptionsFromStrings

func (f *Field) SetOptionsFromStrings(values []string) *Field

SetOptionsFromStrings creates options from simple string values. Clamps selectedIndex and scroll offset to prevent out-of-bounds access.

func (*Field) SetPlaceholder

func (f *Field) SetPlaceholder(placeholder string) *Field

SetPlaceholder sets the placeholder text for text/number/textarea fields

func (*Field) SetRequired

func (f *Field) SetRequired(required bool) *Field

SetRequired marks the field as required

func (*Field) SetRows

func (f *Field) SetRows(rows int) *Field

SetRows sets the number of visible rows for textarea fields

func (*Field) SetShowLineNumbers

func (f *Field) SetShowLineNumbers(show bool) *Field

SetShowLineNumbers enables or disables line numbers for textarea

func (*Field) SetSize

func (f *Field) SetSize(width, height int)

SetSize sets the field width and height

func (*Field) SetValidator

func (f *Field) SetValidator(validator Validator) *Field

SetValidator sets the validation function for the field

func (*Field) SetValue

func (f *Field) SetValue(value any) *Field

SetValue sets the field value

func (*Field) Update

func (f *Field) Update(msg tea.Msg) tea.Cmd

Update handles input messages

func (*Field) Validate

func (f *Field) Validate() error

Validate runs the field's validator and returns any error

func (*Field) View

func (f *Field) View() string

View renders the field

type FieldChangedMsg

type FieldChangedMsg struct {
	FieldID string
	Value   any
}

FieldChangedMsg is sent when a field value changes

type FieldType

type FieldType int

FieldType defines the type of input for a form field

const (
	// FieldText is a single-line text input
	FieldText FieldType = iota
	// FieldNumber is a numeric input with optional min/max validation
	FieldNumber
	// FieldDropdown is a single selection from options
	FieldDropdown
	// FieldMultiSelect allows multiple selections
	FieldMultiSelect
	// FieldToggle is a boolean on/off switch
	FieldToggle
	// FieldReadOnly displays a value without allowing edits
	FieldReadOnly
	// FieldTextArea is a multi-line text input
	FieldTextArea
)

func (FieldType) String

func (ft FieldType) String() string

String returns a human-readable name for the field type

type Form

type Form struct {
	// Configuration
	Title    string
	Subtitle string
	Mode     FormMode
	// contains filtered or unexported fields
}

Form represents a complete form with multiple sections

func NewForm

func NewForm(title string, mode FormMode) *Form

NewForm creates a new form

func (*Form) AddSection

func (f *Form) AddSection(section *Section) *Form

AddSection adds a section to the form

func (*Form) EnableViewport

func (f *Form) EnableViewport() *Form

EnableViewport enables viewport scrolling for long forms

func (*Form) Focus

func (f *Form) Focus()

Focus sets initial focus on the form

func (*Form) FocusedSection

func (f *Form) FocusedSection() *Section

FocusedSection returns the currently focused section

func (*Form) GetData

func (f *Form) GetData() map[string]interface{}

GetData returns all field values from all sections

func (*Form) GetField

func (f *Form) GetField(id string) *Field

GetField returns a field by ID from any section

func (*Form) GetSection

func (f *Form) GetSection(id string) *Section

GetSection returns a section by ID

func (*Form) HasErrors

func (f *Form) HasErrors() bool

HasErrors returns true if the form has validation errors

func (*Form) HasTextInputFocused

func (f *Form) HasTextInputFocused() bool

HasTextInputFocused returns true if a text input field is currently focused. This is used by the app to know whether to consume character keys or pass them to global handlers (e.g., "q" for quit).

func (*Form) Init

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

Init initializes the form

func (*Form) IsDirty

func (f *Form) IsDirty() bool

IsDirty returns true if any field has changed

func (*Form) SectionCount

func (f *Form) SectionCount() int

SectionCount returns the number of sections

func (*Form) Sections

func (f *Form) Sections() []*Section

Sections returns all sections

func (*Form) SetData

func (f *Form) SetData(data map[string]interface{})

SetData populates fields from a map

func (*Form) SetSize

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

SetSize sets the form dimensions

func (*Form) SetSubtitle

func (f *Form) SetSubtitle(subtitle string) *Form

SetSubtitle sets the form subtitle

func (*Form) ShortHelp

func (f *Form) ShortHelp() string

ShortHelp returns a short help string

func (*Form) Update

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

Update handles input messages

func (*Form) Validate

func (f *Form) Validate() []string

Validate validates all sections and returns errors

func (*Form) View

func (f *Form) View() string

View renders the form

type FormCancelMsg

type FormCancelMsg struct{}

FormCancelMsg is sent when the form is canceled via Esc

type FormMode

type FormMode int

FormMode defines whether a form is for creating, editing, or cloning

const (
	// FormModeCreate is for creating new resources
	FormModeCreate FormMode = iota
	// FormModeEdit is for editing existing resources
	FormModeEdit
	// FormModeClone is for cloning an existing resource
	FormModeClone
)

func (FormMode) String

func (fm FormMode) String() string

String returns a human-readable name for the form mode

type FormSubmitMsg

type FormSubmitMsg struct {
	Data map[string]any // All field values keyed by field ID
}

FormSubmitMsg is sent when the form is submitted via Ctrl+S

type Option

type Option struct {
	Value       string // Internal value used for data
	Label       string // Display label shown to user
	Description string // Optional description shown on hover/focus
}

Option represents a selectable option in dropdown/multiselect fields

type Section

type Section struct {
	// Configuration
	ID          string
	Title       string
	Icon        string
	Description string
	Collapsible bool
	Collapsed   bool
	// contains filtered or unexported fields
}

Section represents a group of related form fields

func NewSection

func NewSection(id, title string) *Section

NewSection creates a new form section

func (*Section) AddField

func (s *Section) AddField(field *Field) *Section

AddField adds a field to the section

func (*Section) Blur

func (s *Section) Blur()

Blur removes focus from the section

func (*Section) EditableFieldCount

func (s *Section) EditableFieldCount() int

EditableFieldCount returns the number of editable fields

func (*Section) FieldCount

func (s *Section) FieldCount() int

FieldCount returns the number of fields

func (*Section) Fields

func (s *Section) Fields() []*Field

Fields returns all fields in the section

func (*Section) Focus

func (s *Section) Focus()

Focus sets focus on the section

func (*Section) FocusFirstEditable

func (s *Section) FocusFirstEditable() bool

FocusFirstEditable focuses the first editable field

func (*Section) FocusLastEditable

func (s *Section) FocusLastEditable() bool

FocusLastEditable focuses the last editable field

func (*Section) FocusedField

func (s *Section) FocusedField() *Field

FocusedField returns the currently focused field

func (*Section) FocusedFieldIndex

func (s *Section) FocusedFieldIndex() int

FocusedFieldIndex returns the index of the focused field

func (*Section) GetData

func (s *Section) GetData() map[string]interface{}

GetData returns all field values as a map (skips hidden fields)

func (*Section) GetField

func (s *Section) GetField(id string) *Field

GetField returns a field by ID

func (*Section) HasErrors

func (s *Section) HasErrors() bool

HasErrors returns true if any field has validation errors

func (*Section) HasTextInputFocused

func (s *Section) HasTextInputFocused() bool

HasTextInputFocused returns true if a text input field is currently focused

func (*Section) IsDirty

func (s *Section) IsDirty() bool

IsDirty returns true if any field has changed

func (*Section) IsFocused

func (s *Section) IsFocused() bool

IsFocused returns true if the section is focused

func (*Section) NextField

func (s *Section) NextField() bool

NextField moves focus to the next editable field Returns true if focus moved within section, false if at end

func (*Section) PrevField

func (s *Section) PrevField() bool

PrevField moves focus to the previous editable field Returns true if focus moved within section, false if at start

func (*Section) SetCollapsed

func (s *Section) SetCollapsed(collapsed bool) *Section

SetCollapsed sets the initial collapsed state

func (*Section) SetCollapsible

func (s *Section) SetCollapsible(collapsible bool) *Section

SetCollapsible makes the section collapsible

func (*Section) SetDescription

func (s *Section) SetDescription(desc string) *Section

SetDescription sets the section description

func (*Section) SetIcon

func (s *Section) SetIcon(icon string) *Section

SetIcon sets the section icon/prefix

func (*Section) SetSize

func (s *Section) SetSize(width, height int)

SetSize sets the section dimensions

func (*Section) ToggleCollapse

func (s *Section) ToggleCollapse()

ToggleCollapse toggles the collapsed state

func (*Section) Update

func (s *Section) Update(msg tea.Msg) tea.Cmd

Update handles input messages

func (*Section) Validate

func (s *Section) Validate() []error

Validate validates all fields in the section

func (*Section) View

func (s *Section) View() string

View renders the section

type ValidationErrorMsg

type ValidationErrorMsg struct {
	FieldID string
	Error   string
}

ValidationErrorMsg is sent when validation fails

type Validator

type Validator func(value any) error

Validator is a function that validates a field value. It returns nil if the value is valid, or an error describing the issue.

func ComposeValidators

func ComposeValidators(validators ...Validator) Validator

ComposeValidators chains multiple validators together All validators run, and the first error is returned

func ConditionalValidator

func ConditionalValidator(condition func(value any) bool, validator Validator) Validator

ConditionalValidator runs a validator only if a condition is met

func ValidateCIDR

func ValidateCIDR() Validator

ValidateCIDR returns a validator for CIDR notation (e.g., 10.0.0.0/8)

func ValidateDiskSize

func ValidateDiskSize(minGB, maxGB int64) Validator

ValidateDiskSize validates a disk size value (GCP constraints)

func ValidateEmail

func ValidateEmail() Validator

ValidateEmail returns a validator for email addresses

func ValidateIPAddress

func ValidateIPAddress() Validator

ValidateIPAddress returns a validator for IPv4 addresses

func ValidateNotOneOf

func ValidateNotOneOf(disallowed []string, errorMsg string) Validator

ValidateNotOneOf returns a validator that checks if value is NOT one of disallowed values

func ValidateNumber

func ValidateNumber(min, max int64) Validator

ValidateNumber returns a validator that checks if a number is within a range

func ValidateOneOf

func ValidateOneOf(allowed []string) Validator

ValidateOneOf returns a validator that checks if value is one of allowed values

func ValidatePattern

func ValidatePattern(pattern string, errorMsg string) Validator

ValidatePattern returns a validator that checks against a regex pattern

func ValidateStringLength

func ValidateStringLength(min, max int) Validator

ValidateStringLength returns a validator that checks string length

func ValidateURL

func ValidateURL() Validator

ValidateURL returns a validator for URLs

Jump to

Keyboard shortcuts

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