|
| 1 | +export function initCustomSelects(): void { |
| 2 | + const selects = document.querySelectorAll<HTMLSelectElement>('select.brutal-select'); |
| 3 | + |
| 4 | + selects.forEach((select) => { |
| 5 | + if (select.classList.contains('brutal-select-hidden')) return; |
| 6 | + |
| 7 | + select.classList.add('brutal-select-hidden'); |
| 8 | + |
| 9 | + const wrapper = document.createElement('div'); |
| 10 | + wrapper.className = 'custom-brutal-select'; |
| 11 | + |
| 12 | + const trigger = document.createElement('div'); |
| 13 | + trigger.className = 'custom-brutal-trigger'; |
| 14 | + |
| 15 | + const selectedText = document.createElement('span'); |
| 16 | + selectedText.className = 'custom-select-value'; |
| 17 | + |
| 18 | + const activeOption = select.options[select.selectedIndex]; |
| 19 | + selectedText.textContent = activeOption ? activeOption.text : ''; |
| 20 | + |
| 21 | + const icon = document.createElement('div'); |
| 22 | + |
| 23 | + const svgNS = 'http://www.w3.org/2000/svg'; |
| 24 | + |
| 25 | + const svg = document.createElementNS(svgNS, 'svg'); |
| 26 | + svg.setAttribute('viewBox', '0 0 24 24'); |
| 27 | + svg.setAttribute('fill', 'none'); |
| 28 | + svg.setAttribute('stroke-width', '2'); |
| 29 | + |
| 30 | + const polyline = document.createElementNS(svgNS, 'polyline'); |
| 31 | + polyline.setAttribute('points', '6 9 12 15 18 9'); |
| 32 | + |
| 33 | + svg.appendChild(polyline); |
| 34 | + icon.appendChild(svg); |
| 35 | + |
| 36 | + trigger.appendChild(selectedText); |
| 37 | + trigger.appendChild(icon); |
| 38 | + |
| 39 | + const optionsList = document.createElement('ul'); |
| 40 | + optionsList.className = 'custom-brutal-options'; |
| 41 | + |
| 42 | + Array.from(select.options).forEach((option) => { |
| 43 | + const li = document.createElement('li'); |
| 44 | + li.className = 'custom-brutal-option'; |
| 45 | + if (option.selected) li.classList.add('selected'); |
| 46 | + li.textContent = option.text; |
| 47 | + li.dataset.value = option.value; |
| 48 | + |
| 49 | + li.addEventListener('click', (e: MouseEvent) => { |
| 50 | + e.stopPropagation(); |
| 51 | + |
| 52 | + wrapper.querySelectorAll('.custom-brutal-option').forEach((el) => { |
| 53 | + el.classList.remove('selected'); |
| 54 | + }); |
| 55 | + |
| 56 | + li.classList.add('selected'); |
| 57 | + selectedText.textContent = option.text; |
| 58 | + wrapper.classList.remove('open'); |
| 59 | + |
| 60 | + if (select.value !== option.value) { |
| 61 | + select.value = option.value; |
| 62 | + select.dispatchEvent(new Event('change', { bubbles: true })); |
| 63 | + } |
| 64 | + }); |
| 65 | + optionsList.appendChild(li); |
| 66 | + }); |
| 67 | + |
| 68 | + trigger.addEventListener('click', (e: MouseEvent) => { |
| 69 | + e.stopPropagation(); |
| 70 | + document.querySelectorAll('.custom-brutal-select').forEach((el) => { |
| 71 | + if (el !== wrapper) el.classList.remove('open'); |
| 72 | + }); |
| 73 | + wrapper.classList.toggle('open'); |
| 74 | + }); |
| 75 | + |
| 76 | + wrapper.appendChild(trigger); |
| 77 | + wrapper.appendChild(optionsList); |
| 78 | + |
| 79 | + if (select.parentNode) { |
| 80 | + select.parentNode.insertBefore(wrapper, select.nextSibling); |
| 81 | + } |
| 82 | + |
| 83 | + select.addEventListener('change', (e: Event) => { |
| 84 | + if (!e.isTrusted) return; |
| 85 | + const newActive = Array.from(select.options).find((opt) => opt.value === select.value); |
| 86 | + if (newActive) { |
| 87 | + selectedText.textContent = newActive.text; |
| 88 | + wrapper.querySelectorAll<HTMLElement>('.custom-brutal-option').forEach((el) => { |
| 89 | + el.classList.toggle('selected', el.dataset.value === select.value); |
| 90 | + }); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + document.addEventListener('click', () => { |
| 96 | + document.querySelectorAll('.custom-brutal-select').forEach((el) => { |
| 97 | + el.classList.remove('open'); |
| 98 | + }); |
| 99 | + }); |
| 100 | +} |
0 commit comments