// ===== Custom Multi-Select Dropdown Component ===== // Replaces native this.items = Array.from(element.options).map(o => ({ value: o.value, label: o.textContent, selected: o.selected, })); this.selected = new Set(this.items.filter(i => i.selected).map(i => i.value)); this.searchTerm = ''; this.isOpen = false; this._build(); this._bindEvents(); } _build() { // Hide original select this.original.style.display = 'none'; // Create wrapper this.wrapper = document.createElement('div'); this.wrapper.className = 'ms-wrapper'; this.wrapper.setAttribute('data-for', this.id); // Trigger button this.trigger = document.createElement('div'); this.trigger.className = 'ms-trigger'; this.trigger.tabIndex = 0; this.triggerContent = document.createElement('div'); this.triggerContent.className = 'ms-trigger-content'; this.triggerArrow = document.createElement('div'); this.triggerArrow.className = 'ms-arrow'; this.triggerArrow.innerHTML = ''; this.trigger.appendChild(this.triggerContent); this.trigger.appendChild(this.triggerArrow); // Dropdown this.dropdown = document.createElement('div'); this.dropdown.className = 'ms-dropdown'; // Search input if (this.options.searchable && this.items.length > 5) { this.searchWrap = document.createElement('div'); this.searchWrap.className = 'ms-search-wrap'; this.searchInput = document.createElement('input'); this.searchInput.type = 'text'; this.searchInput.className = 'ms-search'; this.searchInput.placeholder = 'Search...'; this.searchWrap.appendChild(this.searchInput); this.dropdown.appendChild(this.searchWrap); } // Select All / Clear if (this.options.selectAll && this.items.length > 2) { this.actionsBar = document.createElement('div'); this.actionsBar.className = 'ms-actions'; this.actionsBar.innerHTML = ` `; this.dropdown.appendChild(this.actionsBar); } // Options list this.optionsList = document.createElement('div'); this.optionsList.className = 'ms-options'; this.dropdown.appendChild(this.optionsList); this.wrapper.appendChild(this.trigger); this.wrapper.appendChild(this.dropdown); // Insert after original this.original.parentNode.insertBefore(this.wrapper, this.original.nextSibling); this._renderOptions(); this._renderTrigger(); } _renderOptions() { const filtered = this.items.filter(item => !this.searchTerm || item.label.toLowerCase().includes(this.searchTerm.toLowerCase()) ); this.optionsList.innerHTML = filtered.map(item => ` `).join(''); if (filtered.length === 0) { this.optionsList.innerHTML = '
No matches
'; } } _renderTrigger() { if (this.selected.size === 0) { this.triggerContent.innerHTML = `${this.options.placeholder}`; } else if (this.selected.size <= this.options.maxDisplay) { const chips = Array.from(this.selected).map(val => { const item = this.items.find(i => i.value === val); return `${item ? item.label : val}`; }).join(''); this.triggerContent.innerHTML = chips; } else { const count = this.selected.size; const firstTwo = Array.from(this.selected).slice(0, 2).map(val => { const item = this.items.find(i => i.value === val); return item ? item.label : val; }).join(', '); this.triggerContent.innerHTML = `${firstTwo}+${count - 2} more`; } // Sync original select Array.from(this.original.options).forEach(o => { o.selected = this.selected.has(o.value); }); } _bindEvents() { // Toggle dropdown this.trigger.addEventListener('click', (e) => { if (e.target.closest('.ms-chip-remove')) { const val = e.target.closest('.ms-chip-remove').dataset.value; this.selected.delete(val); this._renderTrigger(); this._renderOptions(); e.stopPropagation(); return; } this.toggle(); }); // Keyboard this.trigger.addEventListener('keydown', (e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); this.toggle(); } else if (e.key === 'Escape') { this.close(); } }); // Option click this.optionsList.addEventListener('click', (e) => { const option = e.target.closest('.ms-option'); if (!option) return; const value = option.dataset.value; if (this.selected.has(value)) { this.selected.delete(value); } else { this.selected.add(value); } this._renderOptions(); this._renderTrigger(); }); // Search if (this.searchInput) { this.searchInput.addEventListener('input', (e) => { this.searchTerm = e.target.value; this._renderOptions(); }); this.searchInput.addEventListener('click', (e) => e.stopPropagation()); } // Select All / Clear if (this.actionsBar) { this.actionsBar.querySelector('.ms-select-all').addEventListener('click', (e) => { e.stopPropagation(); this.items.forEach(i => this.selected.add(i.value)); this._renderOptions(); this._renderTrigger(); }); this.actionsBar.querySelector('.ms-clear-all').addEventListener('click', (e) => { e.stopPropagation(); this.selected.clear(); this._renderOptions(); this._renderTrigger(); }); } // Close on outside click document.addEventListener('click', (e) => { if (!this.wrapper.contains(e.target)) { this.close(); } }); } toggle() { this.isOpen ? this.close() : this.open(); } open() { this.isOpen = true; this.wrapper.classList.add('open'); if (this.searchInput) { this.searchInput.value = ''; this.searchTerm = ''; this._renderOptions(); setTimeout(() => this.searchInput.focus(), 50); } } close() { this.isOpen = false; this.wrapper.classList.remove('open'); } getValues() { return Array.from(this.selected); } setValues(values) { this.selected = new Set(values); this._renderTrigger(); this._renderOptions(); } clear() { this.selected.clear(); this._renderTrigger(); this._renderOptions(); } selectAll() { this.items.forEach(i => this.selected.add(i.value)); this._renderTrigger(); this._renderOptions(); } } // Registry of all multi-select instances window._multiSelects = {}; // Auto-initialize all