TextField#
Single-line text input with optional label, helper text, placeholder, validation, and reactive signal binding.
Usage#
Basic#
bs.TextField(placeholder="Type something…")
Label and message#
Use label= for a field title and message= for helper text below.
bs.TextField(
label="Email address",
placeholder="you@example.com",
message="We'll never share your email.",
)
Required#
Set required=True to mark the field visually and prevent empty submission.
bs.TextField(label="Username", required=True, placeholder="Required field")
bs.TextField(label="Email address", placeholder="Optional field")
Value formatting#
Use value_format= to display the value with a locale-aware ICU pattern.
The raw string value is preserved internally; only the display changes.
Requires localization to be enabled.
bs.TextField(value_format="#,##0.00", label="Decimal")
bs.TextField(value_format="percent", label="Percent")
bs.TextField(value_format="currency", label="Currency")
bs.TextField(value_format="yyyy-MM-dd", label="Date")
States#
bs.TextField(value="Editable", label="Normal")
bs.TextField(value="Read only", label="Read only", read_only=True)
bs.TextField(value="Disabled", label="Disabled", disabled=True)
Reactive binding#
Bind a Signal[str] with textsignal=. The field and signal stay in
sync automatically — typing updates the signal, setting the signal updates
the field.
name = bs.Signal("World")
bs.TextField(label="Name", textsignal=name)
bs.Label(textsignal=name, accent="secondary") # updates as you type
Live input events#
on_input() fires on every keystroke. Use it for real-time feedback,
character counting, or live search — anything that needs to respond to
typing, not just field exit.
count = bs.Label("0 / 100", accent="secondary", font="caption")
field = bs.TextField(placeholder="Type…")
def _update_count(e):
count.text = f"{len(field.value)} / 100"
field.on_input(_update_count)
# Or as a debounced Stream
field.on_input().debounce(300).listen(lambda e: search(field.value))
Submit on Enter#
field = bs.TextField(placeholder="Search…")
field.on_submit(lambda e: run_search(field.value))
Validation#
Attach rules with add_validation_rule(). Rules run on the configured
trigger ('blur', 'key', or 'manual').
field = bs.TextField(label="Username")
field.add_validation_rule(
"stringLength",
message="Must be at least 3 characters.",
min=3,
trigger="blur",
)
# Explicit validation check
is_valid = field.validate()
Widget sizing#
All widgets accept self-placement kwargs via **kwargs. The parent
container determines which options apply — stack-based parents use stack
kwargs, grid-based parents use grid kwargs. Unrecognised keys are
silently ignored.
Stack#
Used inside VStack, HStack, App, and other stack containers.
|
Fill direction: |
|
Grow to consume extra space in the parent. |
|
Alignment when the widget does not fill the available slot:
|
|
External spacing in pixels. Accepts an integer (equal on all
sides), a 2-tuple |
|
Horizontal external spacing (left and right). Accepts an integer
or a 2-tuple |
|
Vertical external spacing (top and bottom). Accepts an integer
or a 2-tuple |
Grid#
Used inside a Grid container.
|
Zero-based row and column indices. |
|
Number of rows or columns to span. |
|
Alignment and fill within the grid cell. Any combination of
|
|
External spacing in pixels. Accepts an integer, a 2-tuple
|
|
Horizontal external spacing. Accepts an integer or |
|
Vertical external spacing. Accepts an integer or |
API#
The complete reference for TextField lives on the
Widgets API page. At a glance:
Single-line text input with optional label, message, and validation. |
Full Example#
1
2with bs.App(title="TextField Demo", padding=20, gap=16) as app:
3
4 # Basic
5 bs.Label("Basic", font="heading-sm")
6 basic = bs.TextField(value="Hello, bootstack!", fill="x")
7
8 # Label and message
9 bs.Label("Label and Message", font="heading-sm")
10 bs.TextField(
11 label="Email address",
12 placeholder="you@example.com",
13 message="We'll never share your email.",
14 fill="x",
15 )
16
17 # Required
18 bs.Label("Required", font="heading-sm")
19 bs.TextField(label="Username", placeholder="Choose a username", required=True, fill="x")
20
21 # States
22 bs.Label("States", font="heading-sm")
23 with bs.HStack(gap=8, fill="x", fill_items="x", expand_items=True):
24 bs.TextField(value="Editable", label="Normal")
25 bs.TextField(value="Read only", label="Read only", read_only=True)
26 bs.TextField(value="Disabled", label="Disabled", disabled=True)
27
28 # Reactive binding
29 bs.Label("Reactive Binding", font="heading-sm")
30 with bs.VStack(gap=6, fill="x"):
31 name = bs.Signal("bootstack")
32 bs.TextField(label="Name", textsignal=name, fill="x")
33 bs.Label(textsignal=name, accent="secondary", font="caption")
34
35 # Live character count via on_input
36 bs.Label("Live Character Count (on_input)", font="heading-sm")
37 with bs.VStack(gap=4, fill="x"):
38 count_lbl = bs.Label("0 / 50", accent="secondary", font="caption")
39 field = bs.TextField(placeholder="Type to count…", fill="x")
40
41 def _update_count(e):
42 count_lbl.text = f"{len(field.value)} / 50"
43
44 field.on_input(_update_count)
45
46 # on_submit
47 bs.Label("Submit on Enter (on_submit)", font="heading-sm")
48 with bs.VStack(gap=4, fill="x"):
49 result_lbl = bs.Label("", accent="success", font="caption")
50 submit_field = bs.TextField(placeholder="Type and press Enter…", fill="x")
51 def _on_submit(e):
52 result_lbl.text = f"Submitted: {submit_field.value}"
53
54 submit_field.on_submit(_on_submit)
55
56 # Validation
57 bs.Label("Validation", font="heading-sm")
58 from bootstack.validation import ValidationRule
59 vf = bs.TextField(label="Min 3 characters", placeholder="Enter text…", fill="x")
60 vf.add_validation_rule(ValidationRule(
61 "stringLength",
62 message="Must be at least 3 characters.",
63 min=3,
64 trigger="blur",
65 ))
66
67app.run()