Select
Select input with options, labels, and validation support.
Preview
Usage
views.py
python
from djust import LiveView
class MyView(LiveView):
template_name = "my_template.html"
def mount(self, request, **kwargs):
self.name = 'gallery_select'
self.label = 'Select Field'
self.options = [{'value': 'opt1', 'label': 'Option 1'}, {'value': 'opt2', 'label': 'Option 2'}, {'value': 'opt3', 'label': 'Option 3'}]
self.placeholder = 'Choose...'my_template.html
django
{% load theme_components %}
{% theme_select name=name label=label options=options placeholder=placeholder %}Props
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| name | str | required | — | — |
| label | Optional[str] | — | — | |
| options | list | — | — | |
| placeholder | str | — | — | |
| field_id | str | — | — | |
| has_selected_option | bool | False | — | |
| extra_attrs | str | — | — | |
| css_prefix | str | — | — | |
| attrs | dict | — | — | |
| slot_label | str | — | — | |
| slot_select | str | — | — | |
| slot_help_text | str | — | — | |
| slot_error | str | — | — |
Accessibility
| Requirement | Element | Attribute | Value |
|---|---|---|---|
| Label must reference select via for attribute | label | for | (present) |
Slots
slot_label slot_select slot_help_text slot_error
Source & styles
| File | Path | Notes |
|---|---|---|
| template | djust_theming/components/select.html | Copy it to the same path in your project, or per theme under djust_theming/themes/<theme>/components/. |
| css | djust_theming/components.css | Defines 3 of this component's classes at line 1645, 1652, 1659. Override those rules, or the custom properties they read, in a stylesheet loaded after it. |
CSS variables
--background --foreground --input --radius --ring
Template source — select.html
django
<div class="{{ css_prefix }}select-group {% if attrs.class %}{{ attrs.class }}{% endif %}">
{% if slot_label %}
{{ slot_label|safe }}
{% elif label %}
<label for="{{ field_id|default:name }}" class="{{ css_prefix }}select-label">{{ label }}</label>
{% endif %}
{% if slot_select %}
{{ slot_select|safe }}
{% else %}
<select
name="{{ name }}"
id="{{ field_id|default:name }}"
class="{{ css_prefix }}select"
{% if attrs.required %}required{% endif %}
{% if attrs.disabled %}disabled{% endif %}
{{ extra_attrs }}
>
{% if placeholder %}
<option value="" disabled{% if not has_selected_option %} selected{% endif %}>{{ placeholder }}</option>
{% endif %}
{% for opt in options %}
<option value="{{ opt.value }}" {% if opt.selected %}selected{% endif %}>{{ opt.label }}</option>
{% endfor %}
</select>
{% endif %}
{% if slot_help_text %}
<div class="{{ css_prefix }}select-help">{{ slot_help_text|safe }}</div>
{% endif %}
{% if slot_error %}
<div class="{{ css_prefix }}select-error" role="alert">{{ slot_error|safe }}</div>
{% endif %}
</div>