|
| 1 | +// Custom Tag Dropdown Component |
| 2 | +// Usage: new TagDropdown(containerElement, tagsArray, onChange) |
| 3 | + |
| 4 | +class TagDropdown { |
| 5 | + constructor(container, tags, onChange) { |
| 6 | + this.container = container; |
| 7 | + this.tags = tags; |
| 8 | + this.onChange = onChange; |
| 9 | + this.selectedTag = 'all'; |
| 10 | + this.filteredTags = ['all', ...tags]; |
| 11 | + this.createDropdown(); |
| 12 | + } |
| 13 | + |
| 14 | + createDropdown() { |
| 15 | + this.container.innerHTML = ''; |
| 16 | + this.dropdown = document.createElement('div'); |
| 17 | + this.dropdown.className = 'tag-dropdown'; |
| 18 | + |
| 19 | + // Selected display |
| 20 | + this.selected = document.createElement('div'); |
| 21 | + this.selected.className = 'tag-dropdown-selected'; |
| 22 | + this.selected.tabIndex = 0; |
| 23 | + // Add text and chevron icon |
| 24 | + const selectedText = document.createElement('span'); |
| 25 | + selectedText.className = 'tag-dropdown-selected-text'; |
| 26 | + selectedText.textContent = 'All Tags'; |
| 27 | + this.selected.appendChild(selectedText); |
| 28 | + const chevron = document.createElement('i'); |
| 29 | + chevron.className = 'ri-arrow-down-s-line tag-dropdown-chevron'; |
| 30 | + this.selected.appendChild(chevron); |
| 31 | + this.selected.addEventListener('click', () => this.toggleList()); |
| 32 | + this.selected.addEventListener('keydown', (e) => { |
| 33 | + if (e.key === 'Enter' || e.key === ' ') this.toggleList(); |
| 34 | + }); |
| 35 | + this.dropdown.appendChild(this.selected); |
| 36 | + |
| 37 | + // Dropdown list |
| 38 | + this.list = document.createElement('div'); |
| 39 | + this.list.className = 'tag-dropdown-list'; |
| 40 | + this.list.style.display = 'none'; |
| 41 | + |
| 42 | + // Search input |
| 43 | + this.searchInput = document.createElement('input'); |
| 44 | + this.searchInput.type = 'text'; |
| 45 | + this.searchInput.className = 'tag-dropdown-search'; |
| 46 | + this.searchInput.placeholder = 'Search tags...'; |
| 47 | + this.searchInput.addEventListener('input', () => this.filterTags()); |
| 48 | + this.list.appendChild(this.searchInput); |
| 49 | + |
| 50 | + // Tag options |
| 51 | + this.optionsContainer = document.createElement('div'); |
| 52 | + this.optionsContainer.className = 'tag-dropdown-options'; |
| 53 | + this.list.appendChild(this.optionsContainer); |
| 54 | + |
| 55 | + this.dropdown.appendChild(this.list); |
| 56 | + this.container.appendChild(this.dropdown); |
| 57 | + |
| 58 | + this.renderOptions(); |
| 59 | + document.addEventListener('click', (e) => { |
| 60 | + if (!this.dropdown.contains(e.target)) this.closeList(); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + renderOptions() { |
| 65 | + this.optionsContainer.innerHTML = ''; |
| 66 | + this.filteredTags.forEach(tag => { |
| 67 | + const opt = document.createElement('div'); |
| 68 | + opt.className = 'tag-dropdown-option'; |
| 69 | + opt.textContent = tag === 'all' ? 'All Tags' : tag; |
| 70 | + if (tag === this.selectedTag) opt.classList.add('selected'); |
| 71 | + opt.addEventListener('click', () => this.selectTag(tag)); |
| 72 | + this.optionsContainer.appendChild(opt); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + filterTags() { |
| 77 | + const val = this.searchInput.value.toLowerCase(); |
| 78 | + this.filteredTags = ['all', ...this.tags.filter(tag => tag.toLowerCase().includes(val))]; |
| 79 | + this.renderOptions(); |
| 80 | + } |
| 81 | + |
| 82 | + selectTag(tag) { |
| 83 | + this.selectedTag = tag; |
| 84 | + // Update only the text span |
| 85 | + const textSpan = this.selected.querySelector('.tag-dropdown-selected-text'); |
| 86 | + if (textSpan) textSpan.textContent = tag === 'all' ? 'All Tags' : tag; |
| 87 | + this.closeList(); |
| 88 | + if (this.onChange) this.onChange(tag); |
| 89 | + } |
| 90 | + |
| 91 | + toggleList() { |
| 92 | + this.list.style.display = this.list.style.display === 'none' ? 'block' : 'none'; |
| 93 | + if (this.list.style.display === 'block') { |
| 94 | + this.searchInput.value = ''; |
| 95 | + this.filterTags(); |
| 96 | + this.searchInput.focus(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + closeList() { |
| 101 | + this.list.style.display = 'none'; |
| 102 | + } |
| 103 | + |
| 104 | + setTags(tags) { |
| 105 | + this.tags = tags; |
| 106 | + this.filteredTags = ['all', ...tags]; |
| 107 | + this.renderOptions(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +window.TagDropdown = TagDropdown; |
0 commit comments