function initDropdowns() { document.querySelectorAll("[data-dropdown]").forEach(function (dropdown) { if (dropdown.dataset.initialized) return; dropdown.dataset.initialized = "true"; var trigger = dropdown.querySelector("[data-dropdown-trigger]"); var menu = dropdown.querySelector("[data-dropdown-menu]"); var search = dropdown.querySelector("[data-dropdown-search]"); var options = dropdown.querySelectorAll("[data-dropdown-option]"); var empty = dropdown.querySelector("[data-dropdown-empty]"); var hiddenInput = dropdown.querySelector("[data-dropdown-value]"); var label = dropdown.querySelector("[data-dropdown-label]"); trigger.addEventListener("click", function () { dropdown.classList.toggle("open"); if (dropdown.classList.contains("open") && search) { search.value = ""; filterOptions(""); search.focus(); } }); if (search) { search.addEventListener("input", function () { filterOptions(search.value.toLowerCase()); }); } options.forEach(function (option) { option.addEventListener("click", function () { if (hiddenInput) { hiddenInput.value = option.dataset.value; } if (label) { label.textContent = option.dataset.label; label.classList.remove("text-zinc-500"); label.classList.add("text-zinc-200"); } options.forEach(function (other) { other.classList.remove("selected"); }); option.classList.add("selected"); dropdown.classList.remove("open"); }); }); function filterOptions(query) { var visibleCount = 0; options.forEach(function (option) { var matches = option.dataset.label.toLowerCase().indexOf(query) !== -1; option.style.display = matches ? "" : "none"; if (matches) visibleCount++; }); if (empty) { empty.classList.toggle("hidden", visibleCount > 0); } } document.addEventListener("click", function (event) { if (!dropdown.contains(event.target)) { dropdown.classList.remove("open"); } }); }); } document.addEventListener("DOMContentLoaded", initDropdowns); document.body.addEventListener("htmx:afterSwap", initDropdowns);