Searchable Select
The SearchableSelect component creates a server-side searchable dropdown using HTMX, replacing client-side libraries with pure server-side filtering.
Quick Start
SearchableSelect(endpoint="/api/users/search", name="user_id", placeholder="Search users...", csp_safe=True)
Usage Scenarios
User Selection
SearchableSelect(
endpoint="/api/users/search",
name="assigned_to",
select_id="assigned_to",
placeholder="Search by name or email...",
debounce=300,
csp_safe=True
)
Country/Location Selection
SearchableSelect(
endpoint="/api/countries/search",
name="country",
placeholder="Search countries...",
initial_options=[
("us", "United States"),
("uk", "United Kingdom"),
("ca", "Canada"),
]
)
Product Search
SearchableSelect(
endpoint="/api/products/search",
name="product_id",
placeholder="Search products...",
min_chars=3,
debounce=400
)
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
endpoint |
str |
Required | Server endpoint for search (receives 'q' param) |
name |
str |
Required | Form field name |
placeholder |
str |
"Search..." | Search input placeholder |
initial_options |
list[tuple] |
None |
Initial options as (value, text) tuples |
debounce |
int |
300 | Milliseconds to wait after typing |
min_chars |
int |
2 | Minimum characters before triggering search |
select_id |
str \| None |
Auto | Unique ID for the select element |
csp_safe |
bool \| None |
None |
Recommended production mode that avoids inline JavaScript |
**kwargs |
Any |
- | Additional HTML attributes |
Accessibility
- Search input has proper
ariaattributes. - Results are returned as semantic
<a>elements with list-group styling. csp_safe=Trueavoids inline click handlers for strict Content Security Policy compliance.
API Reference
faststrap.components.forms.searchable_select.SearchableSelect(endpoint, name, placeholder='Search...', min_chars=2, debounce=300, initial_options=None, required=False, size=UNSET, select_id=None, csp_safe=None, **kwargs)
Server-side searchable dropdown using HTMX.
Replaces client-side libraries like Select2/Choices.js with pure server-side filtering. As user types, sends requests to server which returns filtered options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Server endpoint for search (receives 'q' query param) |
required |
name
|
str
|
Form field name |
required |
placeholder
|
str
|
Search input placeholder |
'Search...'
|
initial_options
|
list[tuple[str, str]] | None
|
Initial options as list of (value, text) tuples |
None
|
debounce
|
int
|
Milliseconds to wait after typing before searching |
300
|
min_chars
|
int
|
Minimum characters before triggering search |
2
|
select_id
|
str | None
|
Unique ID for the select element |
None
|
csp_safe
|
bool | None
|
Avoid inline click handlers by using delegated listeners
from FastStrap init script. |
None
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
Div containing search input and select dropdown |
Examples:
Basic usage:
>>> SearchableSelect(
... endpoint="/api/users/search",
... name="user_id",
... placeholder="Search users..."
... )
With initial options:
>>> SearchableSelect(
... endpoint="/api/countries/search",
... name="country",
... initial_options=[
... ("us", "United States"),
... ("uk", "United Kingdom"),
... ]
... )
Server-side handler:
@app.get("/api/users/search")
def search_users(q: str = ""):
if len(q) < 2:
return ""
users = db.query(User).filter(
User.name.ilike(f"%{q}%")
).limit(10).all()
return Div(*[
A(
user.name,
href="#",
cls="list-group-item list-group-item-action",
data_value=user.id,
)
for user in users
])
Note
The server endpoint should: 1. Receive 'q' query parameter with search term 2. Filter results server-side 3. Return HTML options to replace the results container
For better UX, consider: - Showing loading indicator during search - Handling empty results gracefully - Limiting results to prevent overwhelming the UI
Source code in src/faststrap/components/forms/searchable_select.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |