{# Renders a select component. @param id {string} [optional] - Unique identifier for the select component. @param selected {string|array} [optional] - The initially selected value(s). @param name {string} [optional] - The name attribute for the hidden input storing the selected value. @param multiple {boolean} [optional] [default=false] - Enables multiple selection mode. @param placeholder {string} [optional] - Placeholder text shown when no option is selected. @param close_on_select {boolean} [optional] [default=false] - Closes the popover when selecting an option in multiple mode. @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. @param popover_attrs {object} [optional] - Additional HTML attributes for the popover content div. @param listbox_attrs {object} [optional] - Additional HTML attributes for the listbox div. @param input_attrs {object} [optional] - Additional HTML attributes for the hidden input. #} {% macro select( id=None, selected=None, name=None, items=[], multiple=false, placeholder=None, close_on_select=false, format="value", main_attrs={}, trigger_attrs={}, popover_attrs={}, listbox_attrs={}, input_attrs={} ) %} {% set id = id or ("select-" + (range(100000, 999999) | random | string)) %} {% 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 %} {% set first_option = namespace(item=None) %} {% set selected_options = namespace(items=[]) %} {% if items %} {% for item in items %} {% if item.type == "group" %} {% for sub_item in item['items'] %} {% if not first_option.item %} {% set first_option.item = sub_item %} {% endif %} {% if sub_item.value in selectedArray %} {% set selected_options.items = selected_options.items + [sub_item] %} {% endif %} {% endfor %} {% else %} {% if not first_option.item %} {% set first_option.item = item %} {% endif %} {% if item.value in selectedArray %} {% set selected_options.items = selected_options.items + [item] %} {% endif %} {% endif %} {% endfor %} {% endif %} {% set default_option = (selected_options.items[0] if selected_options.items else (none if placeholder 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 (placeholder and not selected_options.items) else (default_option.label if default_option else ''))) %} {% set selected_payload = namespace(items=[]) %} {% if multiple and selected_options.items %} {% for opt in selected_options.items %} {% set selected_payload.items = selected_payload.items + [{'value': opt.value, 'label': opt.label}] %} {% endfor %} {% elif default_option %} {% set selected_payload.items = [{'value': default_option.value, 'label': default_option.label}] %} {% endif %}