TextField
Key bindings
Section titled “Key bindings”| Key | Action |
|---|---|
← / → | Move cursor |
Home | Jump to start |
End | Jump to end |
Backspace | Delete character before cursor |
Delete | Delete character at cursor |
Ctrl+U | Delete from cursor to start of line |
Ctrl+K | Delete from cursor to end of line |
Ctrl+W | Delete word backward |
Enter | Submit (fires onSubmit) |
Validation
Section titled “Validation”field := components.NewTextField(). SetLabel("Email"). SetValidator(validators.Email())The validator runs on every keystroke. The field draws a red border when the current value is invalid. FieldValue() returns the raw string regardless of validation state — check IsValid() before using the value.
Form integration
Section titled “Form integration”TextField implements FormField. The bound value type is string.
Usage
field := components.NewTextField("email"). SetLabel("Email Address"). SetPlaceholder("user@example.com")
field.SetValue("test@example.com")
fmt.Println(field.Value())SetValidator
field := components.NewTextField("email"). SetLabel("Email"). SetValidator(func(value string) error { return validators.Email()(value) })
field.SetValue("invalid-email")if err := field.Validate(); err != nil { fmt.Println("Validation failed:", err)}
field.SetValue("valid@example.com")if err := field.Validate(); err == nil { fmt.Println("Validation passed")}Methods
| Method | Signature | Description |
|---|---|---|
Clear | func (t *TextField) Clear() | Clear resets the text field to an empty value. |
ClearField | func (t *TextField) ClearField() | ClearField resets the field to its zero value. Implements FormField. |
CursorPos | func (t *TextField) CursorPos() int | CursorPos returns the text cursor position (rune index within the value). Useful for callers that render the field's text inline and need to draw the cursor themselves (e.g. an inline command bar). |
FieldValue | func (t *TextField) FieldValue() any | FieldValue returns the field's current value as an any. Implements FormField. |
GetError | func (t *TextField) GetError() string | GetError returns the current validation error. |
GetFieldHeight | func (t *TextField) GetFieldHeight() int | GetFieldHeight returns the preferred height for this field. |
GetName | func (t *TextField) GetName() string | GetName returns the field name. |
GetValue | func (t *TextField) GetValue() string | GetValue returns the current value. |
HandleKey | func (t *TextField) HandleKey(ev *tcell.EventKey) bool | |
HasError | func (t *TextField) HasError() bool | HasError returns whether the field has a validation error. |
HasValue | func (t *TextField) HasValue() bool | HasValue returns true if the text field has a non-empty value. |
SetFieldValue | func (t *TextField) SetFieldValue(value any) error | SetFieldValue sets the field's value from an any. Implements FormField. |
SetLabel | func (t *TextField) SetLabel(label string) *TextField | SetLabel sets the field label. |
SetMaskChar | func (t *TextField) SetMaskChar(char rune) *TextField | SetMaskChar sets the character used for password masking. Default is '•'. Common alternatives are '*' or '●'. |
SetMasked | func (t *TextField) SetMasked(masked bool) *TextField | SetMasked enables or disables password masking. When enabled, the displayed value will show mask characters instead of the actual text. |
SetOnChange | func (t *TextField) SetOnChange(handler ChangeHandler[string]) *TextField | SetOnChange sets the change handler (new API). |
SetOnSubmit | func (t *TextField) SetOnSubmit(handler SubmitHandler) *TextField | SetOnSubmit sets the submit handler (new API). |
SetPlaceholder | func (t *TextField) SetPlaceholder(placeholder string) *TextField | SetPlaceholder sets the placeholder text. |
SetValidator | func (t *TextField) SetValidator(fn func(string) error) *TextField | SetValidator sets the validation function. |
SetValidators | func (t *TextField) SetValidators(vs ...validators.Validator) *TextField | SetValidators sets multiple validators for the field. Validators are run in order and the first error is returned. |
SetValue | func (t *TextField) SetValue(value string) *TextField | SetValue sets the current value. |
Validate | func (t *TextField) Validate() error | Validate runs validation and returns any error. |
Value | func (t *TextField) Value() string | Value returns the current value (alias for GetValue). This method is part of the ValueProvider interface. |
Types
type ChangeHandler ChangeHandler handles value changes type SubmitHandler SubmitHandler handles submissions