|
| 1 | +{% comment %} |
| 2 | + V3 combo multi-select (searchable dropdown with multi-selection). |
| 3 | + Variables: |
| 4 | + name (required) — input name attribute (submitted as multiple values via getlist) |
| 5 | + label (optional) — label text |
| 6 | + placeholder (optional) — placeholder text when nothing selected / search input hint |
| 7 | + options (required) — list of (value, label) 2-tuples (e.g. from TextChoices.choices) |
| 8 | + selected_values (optional) — list of pre-selected values, e.g. ["a","b"] |
| 9 | + help_text (optional) — help text below the field |
| 10 | + error (optional) — error message (activates error state) |
| 11 | + required (optional) — if truthy, adds required attribute |
| 12 | + extra_class (optional) — additional classes on the wrapper |
| 13 | + Usage: |
| 14 | + {% include "v3/includes/_field_combo_multi.html" with name="tags" label="Tags" options=tags_options placeholder="Search tags..." %} |
| 15 | +{% endcomment %} |
| 16 | +{% load text_helpers %} |
| 17 | +<script type="application/json" id="field-{{ name }}-options">{{ options|to_json }}</script> |
| 18 | +{% if selected_values %} |
| 19 | + <script type="application/json" id="field-{{ name }}-selected">{{ selected_values|to_json }}</script> |
| 20 | +{% endif %} |
| 21 | +<div class="field{% if error %} field--error{% endif %}{% if extra_class %} {{ extra_class }}{% endif %}" |
| 22 | + {# djlint:off #} |
| 23 | + x-data="{ |
| 24 | + jsReady: false, |
| 25 | + open: false, |
| 26 | + query: '', |
| 27 | + selectedValues: [], |
| 28 | + options: [], |
| 29 | + get filtered() { |
| 30 | + if (!this.query) return this.options; |
| 31 | + const q = this.query.toLowerCase(); |
| 32 | + return this.options.filter(o => o.label.toLowerCase().includes(q)); |
| 33 | + }, |
| 34 | + get displayText() { |
| 35 | + if (this.selectedValues.length === 0) return ''; |
| 36 | + return this.options |
| 37 | + .filter(o => this.selectedValues.includes(o.value)) |
| 38 | + .map(o => o.label) |
| 39 | + .join(', '); |
| 40 | + }, |
| 41 | + init() { |
| 42 | + this.jsReady = true; |
| 43 | + this.options = JSON.parse(document.getElementById('field-{{ name }}-options').textContent); |
| 44 | + const selEl = document.getElementById('field-{{ name }}-selected'); |
| 45 | + if (selEl) this.selectedValues = JSON.parse(selEl.textContent); |
| 46 | + }, |
| 47 | + toggle(value) { |
| 48 | + const idx = this.selectedValues.indexOf(value); |
| 49 | + if (idx === -1) { |
| 50 | + this.selectedValues.push(value); |
| 51 | + } else { |
| 52 | + this.selectedValues.splice(idx, 1); |
| 53 | + } |
| 54 | + }, |
| 55 | + isSelected(value) { |
| 56 | + return this.selectedValues.includes(value); |
| 57 | + }, |
| 58 | + toggleOpen() { |
| 59 | + if (this.open) { |
| 60 | + this.open = false; |
| 61 | + } else { |
| 62 | + this.open = true; |
| 63 | + this.query = ''; |
| 64 | + this.$nextTick(() => this.$refs.searchInput.focus()); |
| 65 | + } |
| 66 | + } |
| 67 | + }"> |
| 68 | + {# djlint:on #} |
| 69 | + {% if label %}<label class="field__label" id="field-{{ name }}-label">{{ label }}</label>{% endif %} |
| 70 | + <select class="field__select" |
| 71 | + multiple |
| 72 | + name="{{ name }}" |
| 73 | + {% if required %}required{% endif %} |
| 74 | + {% if label %}aria-labelledby="field-{{ name }}-label"{% endif %} |
| 75 | + {% if error %}aria-invalid="true" aria-describedby="field-{{ name }}-error"{% elif help_text %}aria-describedby="field-{{ name }}-help"{% endif %} |
| 76 | + x-show="!jsReady" |
| 77 | + :disabled="jsReady"> |
| 78 | + {% for value, label in options %} |
| 79 | + <option value="{{ value }}"{% if value in selected_values %} selected{% endif %}>{{ label }}</option> |
| 80 | + {% endfor %} |
| 81 | + </select> |
| 82 | + <div class="dropdown dropdown--combo" :class="{ 'dropdown--open': open }" x-cloak> |
| 83 | + <div class="dropdown__trigger" |
| 84 | + :class="{ 'dropdown__trigger--active': open }" |
| 85 | + @click="if (!open) toggleOpen()" |
| 86 | + {% if label %}aria-labelledby="field-{{ name }}-label"{% endif %}> |
| 87 | + <!-- Closed state: show selected values or placeholder --> |
| 88 | + <template x-if="!open"> |
| 89 | + <span class="multiselect__trigger-text"> |
| 90 | + <span x-show="selectedValues.length > 0" |
| 91 | + x-text="displayText"></span> |
| 92 | + <span class="dropdown__trigger-placeholder" |
| 93 | + x-show="selectedValues.length === 0">{{ placeholder|default:"Search..." }}</span> |
| 94 | + </span> |
| 95 | + </template> |
| 96 | + <!-- Open state: show search icon + input --> |
| 97 | + <template x-if="open"> |
| 98 | + <span class="combo__search-row"> |
| 99 | + {% include "includes/icon.html" with icon_name="search" icon_class="combo__search-icon" %} |
| 100 | + <input type="text" |
| 101 | + class="combo__search-input" |
| 102 | + x-ref="searchInput" |
| 103 | + x-model="query" |
| 104 | + @keydown.escape.prevent="open = false; setTimeout(() => $refs.chevronBtn.focus())" |
| 105 | + @keydown.arrow-down.prevent="$refs.list.querySelector('[role=option]')?.focus()" |
| 106 | + placeholder="{{ placeholder|default:'Type to search...' }}" |
| 107 | + aria-autocomplete="list" |
| 108 | + aria-label="Search options"> |
| 109 | + </span> |
| 110 | + </template> |
| 111 | + <!-- Chevron: always present, animates via .dropdown--open --> |
| 112 | + <button type="button" |
| 113 | + class="combo__chevron-btn" |
| 114 | + x-ref="chevronBtn" |
| 115 | + @click.stop="toggleOpen()" |
| 116 | + :aria-expanded="open" |
| 117 | + aria-label="Toggle search"> |
| 118 | + {% include "includes/icon.html" with icon_name="chevron-down" icon_class="dropdown__chevron" %} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + <div class="dropdown__panel" |
| 122 | + x-show="open" |
| 123 | + @click.away="open = false" |
| 124 | + @keydown.escape.prevent="open = false; setTimeout(() => $refs.chevronBtn.focus())" |
| 125 | + x-transition:enter="transition ease-out duration-100" |
| 126 | + x-transition:enter-start="opacity-0" |
| 127 | + x-transition:enter-end="opacity-100" |
| 128 | + x-transition:leave="transition ease-in duration-75" |
| 129 | + x-transition:leave-start="opacity-100" |
| 130 | + x-transition:leave-end="opacity-0" |
| 131 | + style="display: none" |
| 132 | + role="listbox" |
| 133 | + aria-multiselectable="true" |
| 134 | + {% if label %}aria-labelledby="field-{{ name }}-label"{% endif %}> |
| 135 | + <div class="dropdown__list" x-ref="list"> |
| 136 | + <template x-for="option in filtered" :key="option.value"> |
| 137 | + <div class="multiselect__item" |
| 138 | + role="option" |
| 139 | + :aria-selected="isSelected(option.value)" |
| 140 | + :data-value="option.value" |
| 141 | + @click="toggle(option.value)" |
| 142 | + @keydown.enter.prevent="toggle(option.value)" |
| 143 | + @keydown.space.prevent="toggle(option.value)" |
| 144 | + @keydown.arrow-down.prevent="$el.nextElementSibling?.focus()" |
| 145 | + @keydown.arrow-up.prevent="$el.previousElementSibling?.hasAttribute('data-value') ? $el.previousElementSibling.focus() : $refs.searchInput.focus()" |
| 146 | + tabindex="0"> |
| 147 | + <span class="multiselect__check" |
| 148 | + :class="{ 'multiselect__check--active': isSelected(option.value) }"> |
| 149 | + {% include "includes/icon.html" with icon_name="check" icon_class="multiselect__check-icon" x_show="isSelected(option.value)" x_cloak="true" %} |
| 150 | + </span> |
| 151 | + <span x-text="option.label"></span> |
| 152 | + </div> |
| 153 | + </template> |
| 154 | + <div class="dropdown__empty" x-show="filtered.length === 0">No results found</div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + <!-- Hidden inputs for form submission --> |
| 159 | + <template x-for="val in selectedValues" :key="val"> |
| 160 | + <input type="hidden" :name="'{{ name }}'" :value="val"> |
| 161 | + </template> |
| 162 | + {% if error %} |
| 163 | + <p class="field__error" id="field-{{ name }}-error" role="alert">{{ error }}</p> |
| 164 | + {% elif help_text %} |
| 165 | + <p class="field__help" id="field-{{ name }}-help">{{ help_text }}</p> |
| 166 | + {% endif %} |
| 167 | +</div> |
0 commit comments