Skip to main content

Text Input

TextInput renders a single‑line edit box and returns the current text.

Signature

val := ui.TextInput(label string, opts ...textinput.Option) string

val is an empty string until the user types or a DefaultValue is supplied.

Option helpers

HelperUseDefault
textinput.WithPlaceholder("Your name")Hint text when empty""
textinput.WithDefaultValue("Alice")Pre‑fill on first rendernone
textinput.WithRequired(true)Inside a Form blocks submit if blankfalse
textinput.WithDisabled(true)Read‑only fieldfalse
textinput.WithMaxLength(64)Upper character limitunlimited
textinput.WithMinLength(3)Lower character limit (client‑side)0

Behaviour notes

  • The value is stored in session state, so it persists across page reruns.
  • ValidationMinLength, MaxLength, and Required are enforced in the browser; always re‑validate on the backend if critical.
  • Changing the label does not clear or change the stored value.

Examples

Basic input with placeholder

name := ui.TextInput("Name", textinput.WithPlaceholder("Enter your name"))

Required field inside a form

f, submitted := ui.Form("Save")
email := f.TextInput("Email", textinput.WithRequired(true))
if submitted {
log.Printf("email: %s", email)
}

Length‑constrained password

pwd := ui.TextInput("Password",
textinput.WithPlaceholder("8–64 chars"),
textinput.WithMinLength(8),
textinput.WithMaxLength(64),
)

Disabled, pre‑filled key

ui.TextInput("API Key",
textinput.WithDefaultValue(os.Getenv("API_KEY")),
textinput.WithDisabled(true),
)