Files
basecoat/src/jinja/select.html.jinja
T

173 lines
6.4 KiB
Django/Jinja
Raw Normal View History

2025-05-01 12:50:40 +08:00
{#
Renders a select or combobox component.
@param id {string} [optional] - Unique identifier for the select component.
@param selected {string} [optional] - The initially selected value.
@param name {string} [optional] - The name attribute for the hidden input storing the selected value.
@param main_attrs {object} [optional] - Additional HTML attributes for the main container div.
@param trigger_attrs {object} [optional] - Additional HTML attributes for the trigger button.
2025-06-16 21:11:36 +08:00
@param popover_attrs {object} [optional] - Additional HTML attributes for the popover content div.
2025-05-01 12:50:40 +08:00
@param listbox_attrs {object} [optional] - Additional HTML attributes for the listbox div.
@param input_attrs {object} [optional] - Additional HTML attributes for the hidden input.
@param search_placeholder {string} [optional] [default="Search entries..."] - Placeholder text for the search input (combobox only).
@param is_combobox {boolean} [optional] [default=false] - Renders a combobox with search functionality if true.
2025-05-01 12:50:40 +08:00
#}
2025-04-22 16:09:56 +08:00
{% macro select(
id=None,
selected=None,
2025-04-27 23:19:52 +08:00
name=None,
2025-05-07 22:24:31 +08:00
items=None,
2025-06-16 21:11:36 +08:00
main_attrs={},
2025-04-27 23:19:52 +08:00
trigger_attrs={},
popover_attrs={},
2025-04-27 23:19:52 +08:00
listbox_attrs={},
2025-06-16 21:11:36 +08:00
input_attrs={},
2025-04-27 23:19:52 +08:00
search_placeholder="Search entries...",
is_combobox=false
2025-04-22 16:09:56 +08:00
) %}
{% set id = id or ("select-" + (range(100000, 999999) | random | string)) %}
2025-06-16 21:11:36 +08:00
{% set first_option = namespace(item=None) %}
{% set selected_option = namespace(item=None) %}
2025-06-16 21:11:36 +08:00
{% for item in items %}
{% if item.type == "group" %}
2025-06-16 21:11:36 +08:00
{% for sub_item in item.items %}
{% if not first_option.item %}
{% set first_option.item = sub_item %}
{% endif %}
2025-06-16 21:11:36 +08:00
{% if selected and sub_item.value == selected and not selected_option.item %}
{% set selected_option.item = sub_item %}
{% endif %}
{% endfor %}
{% else %}
2025-06-16 21:11:36 +08:00
{% if not first_option.item %}
{% set first_option.item = item %}
{% endif %}
2025-06-16 21:11:36 +08:00
{% if selected and item.value == selected and not selected_option.item %}
{% set selected_option.item = item %}
{% endif %}
{% endif %}
{% endfor %}
2025-06-16 21:11:36 +08:00
{% set default_option = selected_option.item or first_option.item or None %}
2025-06-16 21:11:36 +08:00
<div
class="select {{ main_attrs.class }}"
{% for key, value in main_attrs.items() %}
{% if key != 'class' %}{{ key }}="{{ value }}"{% endif %}
{% endfor %}
>
2025-04-22 16:09:56 +08:00
<button
type="button"
class="btn-outline justify-between font-normal {{ trigger_attrs.class }}"
id="{{ id }}-trigger"
2025-04-22 16:09:56 +08:00
aria-haspopup="listbox"
aria-expanded="false"
aria-controls="{{ id }}-listbox"
2025-06-16 21:11:36 +08:00
{% for key, value in trigger_attrs.items() %}
2025-04-22 16:09:56 +08:00
{% if key != 'class' %}{{ key }}="{{ value }}"{% endif %}
{% endfor %}
>
<span class="truncate">{{ default_option.label }}</span>
2025-04-25 16:42:10 +08:00
{% if is_combobox %}
2025-05-01 12:50:40 +08:00
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevrons-up-down-icon lucide-chevrons-up-down text-muted-foreground opacity-50 shrink-0"><path d="m7 15 5 5 5-5"/><path d="m7 9 5-5 5 5"/></svg>
2025-04-25 16:42:10 +08:00
{% else %}
2025-05-01 12:50:40 +08:00
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-down-icon lucide-chevron-down text-muted-foreground opacity-50 shrink-0"><path d="m6 9 6 6 6-6"/></svg>
2025-04-25 16:42:10 +08:00
{% endif %}
2025-04-22 16:09:56 +08:00
</button>
<div
id="{{ id }}"
2025-06-16 21:11:36 +08:00
data-popover
aria-hidden="true"
{% for key, value in popover_attrs.items() %}
2025-06-16 21:11:36 +08:00
{{ key }}="{{ value }}"
2025-04-22 16:09:56 +08:00
{% endfor %}
>
{% if is_combobox %}
<header>
2025-05-01 12:50:40 +08:00
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-search-icon lucide-search"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
2025-04-22 16:09:56 +08:00
<input
type="text"
value=""
2025-04-27 23:19:52 +08:00
placeholder="{{ search_placeholder }}"
2025-04-22 16:09:56 +08:00
autocomplete="off"
autocorrect="off"
spellcheck="false"
aria-autocomplete="list"
role="combobox"
2025-06-16 21:11:36 +08:00
aria-expanded="false"
aria-controls="{{ id }}-listbox"
2025-04-22 16:09:56 +08:00
aria-labelledby="{{ id }}-trigger"
>
</header>
{% endif %}
<div
role="listbox"
id="{{ id }}-listbox"
2025-04-22 16:09:56 +08:00
aria-orientation="vertical"
aria-labelledby="{{ id }}-trigger"
2025-06-16 21:11:36 +08:00
{% for key, value in listbox_attrs.items() %}
2025-04-22 16:09:56 +08:00
{{ key }}="{{ value }}"
{% endfor %}
>
2025-06-16 21:11:36 +08:00
{% if items %}
{{ render_select_items(items, default_option.value, id ~ "-items" if id else "items") }}
2025-05-07 22:24:31 +08:00
{% else %}
{{ caller() if caller }}
{% endif %}
2025-04-22 16:09:56 +08:00
</div>
2025-04-18 12:58:28 +08:00
</div>
<input
type="hidden"
name="{{ name or id ~ '-value' }}"
2025-06-16 21:11:36 +08:00
value="{{ (default_option.value if default_option) or '' }}"
{% for key, value in input_attrs.items() %}
{% if key != 'name' and key != 'value' %}{{ key }}="{{ value }}"{% endif %}
{% endfor %}
>
2025-04-22 16:09:56 +08:00
</div>
2025-05-07 22:24:31 +08:00
{% endmacro %}
{#
Renders a list of items for the select component.
@param items {array} - The array of items to render.
@param parent_id_prefix {string} [optional] - The prefix for the item id.
#}
{% macro render_select_items(items, selected, parent_id_prefix="items") %}
2025-06-16 21:11:36 +08:00
{% for item in items %}
2025-05-07 22:24:31 +08:00
{% set item_id = parent_id_prefix ~ "-" ~ loop.index %}
{% if item.type == "group" %}
{% set group_label_id = item.id if item.id else "group-label-" + item_id %}
<div
role="group"
aria-labelledby="{{ group_label_id }}"
2025-06-16 21:11:36 +08:00
{% if item.attrs %}
{% for key, value in item.attrs.items() %}
{{ key }}="{{ value }}"
{% endfor %}
{% endif %}
2025-05-07 22:24:31 +08:00
>
2025-06-16 21:11:36 +08:00
<div role="presentation" id="{{ group_label_id }}">{{ item.label }}</div>
{{ render_select_items(item.items, selected, item_id) if item.items }}
2025-05-07 22:24:31 +08:00
</div>
{% elif item.type == "separator" %}
<hr role="separator" />
{% elif item.type == "item" or not item.type %}
<div
id="{{ item_id }}"
2025-05-07 22:24:31 +08:00
role="option"
data-value="{{ item.value }}"
{% if selected == item.value %}aria-selected="true"{% endif %}
2025-06-16 21:11:36 +08:00
{% if item.attrs %}
{% for key, value in item.attrs.items() %}
{{ key }}="{{ value }}"
{% endfor %}
{% endif %}
2025-05-07 22:24:31 +08:00
>
{{ item.label | safe }}
</div>
{% endif %}
{% endfor %}
2025-04-22 16:09:56 +08:00
{% endmacro %}