|
| 1 | +import { Controller } from "@hotwired/stimulus" |
| 2 | + |
| 3 | +export default class extends Controller { |
| 4 | + static targets = ["select", "input"] |
| 5 | + static values = { |
| 6 | + searchUrl: String, |
| 7 | + searchDelay: { type: Number, default: 300 } |
| 8 | + } |
| 9 | + |
| 10 | + connect() { |
| 11 | + this.setupPersonSearch() |
| 12 | + } |
| 13 | + |
| 14 | + setupPersonSearch() { |
| 15 | + const select = this.selectTarget |
| 16 | + |
| 17 | + // Convert select to a searchable input |
| 18 | + this.createSearchInput(select) |
| 19 | + |
| 20 | + // Hide the original select |
| 21 | + select.style.display = 'none' |
| 22 | + } |
| 23 | + |
| 24 | + createSearchInput(select) { |
| 25 | + const searchContainer = select.parentElement |
| 26 | + |
| 27 | + // Create search input |
| 28 | + const searchInput = document.createElement('input') |
| 29 | + searchInput.type = 'text' |
| 30 | + searchInput.className = 'form-control person-search-input' |
| 31 | + searchInput.placeholder = select.options[0]?.text || 'Search for people...' |
| 32 | + searchInput.setAttribute('data-person-search-target', 'input') |
| 33 | + |
| 34 | + // Create results dropdown |
| 35 | + const resultsDropdown = document.createElement('div') |
| 36 | + resultsDropdown.className = 'person-search-results' |
| 37 | + resultsDropdown.style.cssText = ` |
| 38 | + position: absolute; |
| 39 | + top: 100%; |
| 40 | + left: 0; |
| 41 | + right: 0; |
| 42 | + background: white; |
| 43 | + border: 1px solid #ced4da; |
| 44 | + border-top: none; |
| 45 | + border-radius: 0 0 0.375rem 0.375rem; |
| 46 | + max-height: 200px; |
| 47 | + overflow-y: auto; |
| 48 | + z-index: 1000; |
| 49 | + display: none; |
| 50 | + ` |
| 51 | + |
| 52 | + // Insert elements |
| 53 | + searchContainer.style.position = 'relative' |
| 54 | + searchContainer.insertBefore(searchInput, select) |
| 55 | + searchContainer.appendChild(resultsDropdown) |
| 56 | + |
| 57 | + // Setup event listeners |
| 58 | + let searchTimeout |
| 59 | + searchInput.addEventListener('input', (e) => { |
| 60 | + clearTimeout(searchTimeout) |
| 61 | + searchTimeout = setTimeout(() => { |
| 62 | + this.performSearch(e.target.value, resultsDropdown, select) |
| 63 | + }, this.searchDelayValue) |
| 64 | + }) |
| 65 | + |
| 66 | + searchInput.addEventListener('focus', () => { |
| 67 | + if (searchInput.value) { |
| 68 | + this.performSearch(searchInput.value, resultsDropdown, select) |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + // Hide dropdown when clicking outside |
| 73 | + document.addEventListener('click', (e) => { |
| 74 | + if (!searchContainer.contains(e.target)) { |
| 75 | + resultsDropdown.style.display = 'none' |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + async performSearch(query, resultsDropdown, select) { |
| 81 | + if (query.length < 2) { |
| 82 | + resultsDropdown.style.display = 'none' |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + const response = await fetch(`${this.searchUrlValue}?search=${encodeURIComponent(query)}`, { |
| 88 | + headers: { |
| 89 | + 'Accept': 'application/json', |
| 90 | + 'X-Requested-With': 'XMLHttpRequest' |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + if (!response.ok) throw new Error('Search failed') |
| 95 | + |
| 96 | + const people = await response.json() |
| 97 | + this.displayResults(people, resultsDropdown, select) |
| 98 | + } catch (error) { |
| 99 | + console.error('Person search error:', error) |
| 100 | + resultsDropdown.innerHTML = '<div class="p-2 text-danger">Search failed</div>' |
| 101 | + resultsDropdown.style.display = 'block' |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + displayResults(people, resultsDropdown, select) { |
| 106 | + if (people.length === 0) { |
| 107 | + resultsDropdown.innerHTML = '<div class="p-2 text-muted">No people found</div>' |
| 108 | + resultsDropdown.style.display = 'block' |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + const resultsHtml = people.map(person => ` |
| 113 | + <div class="person-result p-2 border-bottom" |
| 114 | + style="cursor: pointer; display: flex; align-items: center;" |
| 115 | + data-person-id="${person.id}" |
| 116 | + data-person-name="${person.name}"> |
| 117 | + <div class="me-2" style="width: 32px; height: 32px; background-color: #dee2e6; border-radius: 50%; display: flex; align-items: center; justify-content: center;"> |
| 118 | + <i class="fas fa-user text-muted"></i> |
| 119 | + </div> |
| 120 | + <div> |
| 121 | + <div class="fw-medium">${person.name}</div> |
| 122 | + <small class="text-muted">@${person.slug}</small> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + `).join('') |
| 126 | + |
| 127 | + resultsDropdown.innerHTML = resultsHtml |
| 128 | + resultsDropdown.style.display = 'block' |
| 129 | + |
| 130 | + // Add click handlers to results |
| 131 | + resultsDropdown.querySelectorAll('.person-result').forEach(result => { |
| 132 | + result.addEventListener('click', () => { |
| 133 | + this.selectPerson(result, select) |
| 134 | + resultsDropdown.style.display = 'none' |
| 135 | + }) |
| 136 | + }) |
| 137 | + } |
| 138 | + |
| 139 | + selectPerson(resultElement, select) { |
| 140 | + const personId = resultElement.dataset.personId |
| 141 | + const personName = resultElement.dataset.personName |
| 142 | + |
| 143 | + // Update the hidden select |
| 144 | + select.innerHTML = `<option value="${personId}" selected>${personName}</option>` |
| 145 | + select.value = personId |
| 146 | + |
| 147 | + // Update the search input |
| 148 | + const searchInput = this.inputTarget |
| 149 | + searchInput.value = personName |
| 150 | + |
| 151 | + // Trigger change event for form handling |
| 152 | + select.dispatchEvent(new Event('change', { bubbles: true })) |
| 153 | + } |
| 154 | +} |
0 commit comments