2025-05-01 12:50:40 +08:00
|
|
|
{#
|
|
|
|
|
Renders a tabs component.
|
|
|
|
|
|
|
|
|
|
@param id {string} - Unique identifier for the tabs component.
|
|
|
|
|
@param tabsets {array} - An array of objects, each representing a tab and its panel.
|
|
|
|
|
Each object should have:
|
|
|
|
|
- id {string}: Unique identifier prefix for the tab and panel.
|
|
|
|
|
- tab {string}: HTML content for the tab button.
|
|
|
|
|
- panel {string} [optional]: HTML content for the tab panel.
|
|
|
|
|
- tab_attrs {object} [optional]: Additional HTML attributes for the tab button.
|
|
|
|
|
- panel_attrs {object} [optional]: Additional HTML attributes for the tab panel div.
|
|
|
|
|
@param main_attrs {object} [optional] - Additional HTML attributes for the main container div.
|
|
|
|
|
@param tablist_attrs {object} [optional] - Additional HTML attributes for the tablist nav element.
|
|
|
|
|
@param default_tab_index {number} [optional] [default=1] - The 1-based index of the tab to be active initially.
|
|
|
|
|
#}
|
2025-04-22 16:09:56 +08:00
|
|
|
{% macro tabs(
|
2025-06-16 21:11:36 +08:00
|
|
|
id=None,
|
2025-04-25 16:42:10 +08:00
|
|
|
tabsets=[],
|
2025-06-16 21:11:36 +08:00
|
|
|
main_attrs={},
|
|
|
|
|
tablist_attrs={},
|
2025-05-01 20:53:03 +08:00
|
|
|
default_tab_index=1
|
2025-04-22 16:09:56 +08:00
|
|
|
)
|
|
|
|
|
%}
|
2025-06-16 21:11:36 +08:00
|
|
|
{% set id = id or ("tabs-" + (range(100000, 999999) | random | string)) %}
|
2025-04-22 16:09:56 +08:00
|
|
|
<div
|
2025-04-25 16:42:10 +08:00
|
|
|
class="tabs {{ main_attrs.class }}"
|
2025-06-16 21:11:36 +08:00
|
|
|
id="{{ id }}"
|
2025-05-01 20:53:03 +08:00
|
|
|
{% for key, value in main_attrs.items() %}
|
2025-04-25 16:42:10 +08:00
|
|
|
{% if key != 'class' %}{{ key }}="{{ value }}"{% endif %}
|
2025-04-22 16:09:56 +08:00
|
|
|
{% endfor %}
|
|
|
|
|
>
|
2025-04-29 19:14:31 +08:00
|
|
|
<nav
|
2025-04-25 16:42:10 +08:00
|
|
|
role="tablist"
|
|
|
|
|
aria-orientation="horizontal"
|
2025-05-01 20:53:03 +08:00
|
|
|
{% for key, value in tablist_attrs.items() %}
|
2025-04-22 16:09:56 +08:00
|
|
|
{{ key }}="{{ value }}"
|
|
|
|
|
{% endfor %}
|
|
|
|
|
>
|
2025-06-16 21:11:36 +08:00
|
|
|
{% for tabset in tabsets %}
|
2025-04-25 16:42:10 +08:00
|
|
|
<button
|
2025-04-27 23:19:52 +08:00
|
|
|
type="button"
|
2025-04-25 16:42:10 +08:00
|
|
|
role="tab"
|
2025-06-16 21:11:36 +08:00
|
|
|
id="{{ id }}-tab-{{ loop.index }}"
|
|
|
|
|
aria-controls="{{ id }}-panel-{{ loop.index }}"
|
2025-04-25 16:42:10 +08:00
|
|
|
aria-selected="{{ 'true' if loop.index == default_tab_index else 'false' }}"
|
|
|
|
|
tabindex="0"
|
2025-06-16 21:11:36 +08:00
|
|
|
{% if tabset.tab_attrs %}
|
|
|
|
|
{% for key, value in tabset.tab_attrs.items() %}
|
|
|
|
|
{{ key }}="{{ value }}"
|
|
|
|
|
{% endfor %}
|
|
|
|
|
{% endif %}
|
2025-04-25 16:42:10 +08:00
|
|
|
>
|
|
|
|
|
{{ tabset.tab | safe }}
|
|
|
|
|
</button>
|
2025-04-22 16:09:56 +08:00
|
|
|
{% endfor %}
|
2025-04-29 19:14:31 +08:00
|
|
|
</nav>
|
2025-04-21 10:37:15 +08:00
|
|
|
|
2025-06-16 21:11:36 +08:00
|
|
|
{% for tabset in tabsets %}
|
2025-04-25 16:42:10 +08:00
|
|
|
{% if tabset.panel %}
|
|
|
|
|
<div
|
|
|
|
|
role="tabpanel"
|
2025-06-16 21:11:36 +08:00
|
|
|
id="{{ id }}-panel-{{ loop.index }}"
|
|
|
|
|
aria-labelledby="{{ id }}-tab-{{ loop.index }}"
|
2025-04-25 16:42:10 +08:00
|
|
|
tabindex="-1"
|
|
|
|
|
aria-selected="{{ 'true' if loop.index == default_tab_index else 'false' }}"
|
|
|
|
|
{% if loop.index != default_tab_index %}hidden{% endif %}
|
2025-06-16 21:11:36 +08:00
|
|
|
{% if tabset.panel_attrs %}
|
|
|
|
|
{% for key, value in tabset.panel_attrs.items() %}
|
|
|
|
|
{{ key }}="{{ value }}"
|
|
|
|
|
{% endfor %}
|
|
|
|
|
{% endif %}
|
2025-04-25 16:42:10 +08:00
|
|
|
>
|
|
|
|
|
{{ tabset.panel | safe }}
|
|
|
|
|
</div>
|
|
|
|
|
{% endif %}
|
|
|
|
|
{% endfor %}
|
|
|
|
|
</div>
|
|
|
|
|
{% endmacro %}
|