Toggle Group
The ToggleGroup component renders a button group where only one item stays active at a time.
Quick Start
ToggleGroup(
Button("List View", variant="outline-primary"),
Button("Grid View", variant="outline-primary"),
Button("Table View", variant="outline-primary"),
name="view",
values=["list", "grid", "table"],
)
Usage Scenarios
View Switcher
ToggleGroup(
Button("List", variant="outline-primary"),
Button("Grid", variant="outline-primary"),
Button("Map", variant="outline-primary"),
name="view_mode",
values=["list", "grid", "map"],
active_index=0,
)
Segmented Control
ToggleGroup(
Button("Day", variant="outline-secondary"),
Button("Week", variant="outline-secondary"),
Button("Month", variant="outline-secondary"),
name="period",
values=["day", "week", "month"],
active_index=1,
)
With Custom Active Class
ToggleGroup(
Button("On", variant="success"),
Button("Off", variant="outline-danger"),
name="power",
values=["on", "off"],
active_cls="active",
)
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
*buttons |
Any |
Required | Button elements to include in the group |
name |
str \| None |
None |
Name for the hidden input that stores the selected value |
values |
list[str] \| None |
None |
Values corresponding to each button |
active_index |
int |
0 |
Index of the initially active button |
active_cls |
str |
"active" |
CSS class applied to the active button |
hidden_input |
bool |
True |
Whether to render a hidden input for form submission |
**kwargs |
Any |
- | Additional HTML attributes |
Accessibility
- Active button gets
aria-pressed="true"andaria-current="true". - Inactive buttons get
aria-pressed="false"andaria-current="false". - A hidden input is rendered for form submission when
nameis provided.
API Reference
faststrap.components.forms.toggle_group.ToggleGroup(*buttons, name=None, values=None, active_index=0, active_cls='active', hidden_input=True, **kwargs)
Render a button group where only one item stays active at a time.