TextField
A text input widget for user input.
Basic Usage
TextField()
Options
| Option | Type | Description | Example |
|---|---|---|---|
Hint |
string |
Sets the placeholder text | Hint("Enter your name") |
OnChange |
func(string) |
Sets the callback when text changes | OnChange(func(s string) { ... }) |
TextFieldID |
string |
Sets a unique ID for state persistence | TextFieldID("email-input") |
MultiLine |
- | Allows multiple lines of text | MultiLine() |
Examples
// Basic text field
TextField()
// With placeholder hint
TextField(Hint("Enter your name"))
// With change handler
TextField(
Hint("Email"),
OnChange(func(text string) {
fmt.Println("Input:", text)
}),
)
// With state binding
name := NewState("").Bind(w)
TextField(
Hint("Name"),
OnChange(func(s string) {
name.Set(s)
}),
)
// Multi-line text field
TextField(
Hint("Enter description"),
MultiLine(),
)
// Multiple text fields need unique IDs
TextField(Hint("First name"), TextFieldID("first-name"))
TextField(Hint("Last name"), TextFieldID("last-name"))
TextField(Hint("Email"), TextFieldID("email"))
// In a form layout
Column([]any{
Text("Registration", Style(H3)),
TextField(Hint("Username"), TextFieldID("username")),
TextField(Hint("Email"), TextFieldID("email")),
TextField(Hint("Password"), TextFieldID("password")),
Button("Submit"),
}, Spacing(12))
// With label using Row
Row([]any{
Text("Name:"),
Expanded(TextField(Hint("Enter name"), TextFieldID("name-field"))),
})