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

200 lines
7.8 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.
2026-01-04 14:39:32 +08:00
@param selected {string|array} [optional] - The initially selected value(s).
2025-05-01 12:50:40 +08:00
@param name {string} [optional] - The name attribute for the hidden input storing the selected value.
2026-01-04 14:39:32 +08:00
@param multiple {boolean} [optional] [default=false] - Enables multiple selection mode.
@param placeholder {string} [optional] - Placeholder text shown when no options are selected (multiple mode only).
2026-01-13 19:33:59 +08:00
@param close_on_select {boolean} [optional] [default=false] - Closes the popover when selecting an option in multiple mode.
2025-05-01 12:50:40 +08:00
@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-06-18 19:42:26 +08:00
items=[],
2026-01-04 14:39:32 +08:00
multiple=false,
placeholder=None,
2026-01-13 19:33:59 +08:00
close_on_select=false,
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)) %}
2026-01-05 12:15:43 +08:00
{% if selected is defined and selected is iterable and selected is not string %}
{% set selectedArray = selected %}
{% elif selected is defined %}
{% set selectedArray = [selected] %}
{% else %}
{% set selectedArray = [] %}
{% endif %}
2025-06-16 21:11:36 +08:00
{% set first_option = namespace(item=None) %}
2026-01-04 14:39:32 +08:00
{% set selected_options = namespace(items=[]) %}
2025-06-18 19:42:26 +08:00
{% if items %}
{% for item in items %}
{% if item.type == "group" %}
2025-12-21 14:20:09 +08:00
{% for sub_item in item['items'] %}
2025-06-18 19:42:26 +08:00
{% if not first_option.item %}
{% set first_option.item = sub_item %}
{% endif %}
2026-01-04 14:39:32 +08:00
{% if sub_item.value in selectedArray %}
{% set selected_options.items = selected_options.items + [sub_item] %}
2025-06-18 19:42:26 +08:00
{% endif %}
{% endfor %}
{% else %}
2025-06-16 21:11:36 +08:00
{% if not first_option.item %}
2025-06-18 19:42:26 +08:00
{% set first_option.item = item %}
{% endif %}
2026-01-04 14:39:32 +08:00
{% if item.value in selectedArray %}
{% set selected_options.items = selected_options.items + [item] %}
{% endif %}
{% endif %}
2025-06-18 19:42:26 +08:00
{% endfor %}
{% endif %}
2026-01-04 14:39:32 +08:00
{% set default_option = (selected_options.items[0] if selected_options.items else first_option.item) %}
{% set labels = [] %}
{% if multiple and selected_options.items %}
{% for opt in selected_options.items %}
{% set labels = labels + [opt.label] %}
{% endfor %}
{% endif %}
{% set default_label = (', '.join(labels) if (multiple and selected_options.items) else ((placeholder or '') if (multiple and not selected_options.items) else (default_option.label if default_option else ''))) %}
2025-06-16 21:11:36 +08:00
<div
2025-06-19 15:21:12 +08:00
id="{{ id }}"
2025-06-16 21:11:36 +08:00
class="select {{ main_attrs.class }}"
2026-01-04 14:39:32 +08:00
{% if multiple and placeholder %}data-placeholder="{{ placeholder }}"{% endif %}
2026-01-13 19:33:59 +08:00
{% if multiple and close_on_select %}data-close-on-select="true"{% endif %}
2025-06-16 21:11:36 +08:00
{% 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"
2026-01-04 21:22:53 +08:00
class="btn-outline {{ 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 %}
>
2026-01-04 14:39:32 +08:00
<span class="truncate">{{ default_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
2025-06-19 15:21:12 +08:00
id="{{ id }}-popover"
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"
2026-01-04 14:39:32 +08:00
{% if multiple %}aria-multiselectable="true"{% endif %}
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 %}
2026-01-04 14:39:32 +08:00
{{ render_select_items(items, selectedArray, 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>
2026-01-05 11:28:05 +08:00
<input
type="hidden"
name="{{ name or id ~ '-value' }}"
value="{% if multiple %}{{ selectedArray | tojson }}{% else %}{{ (default_option.value if default_option) or '' }}{% endif %}"
{% for key, value in input_attrs.items() %}
{% if key != 'name' and key != 'value' %}{{ key }}="{{ value }}"{% endif %}
{% endfor %}
2026-01-05 11:28:05 +08:00
>
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.
#}
2026-01-04 14:39:32 +08:00
{% macro render_select_items(items, selectedArray, 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-18 20:55:32 +08:00
<div role="heading" id="{{ group_label_id }}">{{ item.label }}</div>
2026-01-04 14:39:32 +08:00
{{ render_select_items(item['items'], selectedArray, 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"
2026-01-05 12:15:43 +08:00
{% if item.value is defined and item.value is not none %}data-value="{{ item.value }}"{% endif %}
2026-01-04 14:39:32 +08:00
{% if item.value in selectedArray %}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-12-21 14:20:09 +08:00
{% endmacro %}