Skip to content

Commit 4b326c7

Browse files
committed
fix autocomplete controller
1 parent 7e7e88b commit 4b326c7

File tree

1 file changed

+9
-80
lines changed

1 file changed

+9
-80
lines changed

service/src/main/resources/web/static/js/autocomplete_controller.js

Lines changed: 9 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,37 @@
33
import { Application, Controller } from "/static/cache/stimulus/3.1.0/stimulus.min.js" // provided by Misk
44
window.Stimulus = Application.start()
55

6-
/*
7-
* Usage
8-
* =====
9-
*
10-
* <div data-controller="autocomplete" data-autocomplete-url-value="/birds/search" role="combobox">
11-
* <input type="text" data-autocomplete-target="input"/>
12-
* <input type="hidden" name="bird_id" data-autocomplete-target="hidden"/>
13-
* <ul class="list-group" data-autocomplete-target="results"></ul>
14-
* </div>
15-
*
16-
* The server is expected to send back snippets as such:
17-
*
18-
* <li class="list-group-item" role="option" data-autocomplete-value="1">Blackbird</li>
19-
* <li class="list-group-item" role="option" data-autocomplete-value="2">Bluebird</li>
20-
* <li class="list-group-item" role="option" data-autocomplete-value="3">Mockingbird</li>
21-
*
22-
*/
236
class AutocompleteController extends Controller {
247
static targets = ["input", "results"]
25-
static values = {
26-
url: String,
27-
minLength: { type: Number, default: 2 },
28-
delay: { type: Number, default: 300 }
29-
}
8+
static values = { url: String }
309

3110
connect() {
32-
this.inputTarget.addEventListener('input', this.debounce(this.onInput.bind(this), this.delayValue));
33-
this.inputTarget.addEventListener('blur', this.onBlur.bind(this));
11+
this.inputTarget.addEventListener('input', this.onInput.bind(this));
12+
this.inputTarget.addEventListener('blur', () => setTimeout(() => this.hideResults(), 100));
3413
this.resultsTarget.addEventListener('click', this.onClick.bind(this));
35-
this.resultsTarget.addEventListener('mousedown', this.onMousedown.bind(this));
36-
37-
// Set autocomplete attributes
3814
this.inputTarget.setAttribute('autocomplete', 'off');
39-
this.inputTarget.setAttribute('spellcheck', 'false');
40-
41-
this.mouseDown = false;
4215
}
4316

4417
onInput(event) {
4518
const query = event.target.value.trim();
46-
47-
if (query.length >= this.minLengthValue) {
48-
this.fetchResults(query);
49-
} else {
50-
this.hideResults();
51-
}
52-
}
53-
54-
onBlur() {
55-
if (!this.mouseDown) {
56-
this.hideResults();
57-
}
58-
}
59-
60-
onMousedown() {
61-
this.mouseDown = true;
62-
setTimeout(() => { this.mouseDown = false; }, 100);
19+
query.length >= 2 ? this.fetchResults(query) : this.hideResults();
6320
}
6421

6522
onClick(event) {
6623
const option = event.target.closest('[role="option"]');
6724
if (option) {
68-
this.selectOption(option);
25+
this.inputTarget.value = option.textContent.trim();
26+
this.hideResults();
6927
}
7028
}
7129

72-
selectOption(option) {
73-
const value = option.getAttribute('data-autocomplete-value') || option.textContent.trim();
74-
this.inputTarget.value = value;
75-
this.hideResults();
76-
this.inputTarget.focus();
77-
}
78-
7930
async fetchResults(query) {
8031
try {
81-
const url = `${this.urlValue}?q=${encodeURIComponent(query)}`;
82-
const response = await fetch(url, {
83-
headers: { 'X-Requested-With': 'XMLHttpRequest' }
84-
});
32+
const response = await fetch(`${this.urlValue}?q=${encodeURIComponent(query)}`);
8533
const html = await response.text();
86-
8734
this.resultsTarget.innerHTML = html;
88-
89-
if (this.resultsTarget.children.length > 0) {
90-
this.showResults();
91-
} else {
92-
this.hideResults();
93-
}
94-
} catch (error) {
35+
html.trim() ? this.showResults() : this.hideResults();
36+
} catch {
9537
this.hideResults();
9638
}
9739
}
@@ -103,19 +45,6 @@ class AutocompleteController extends Controller {
10345
hideResults() {
10446
this.resultsTarget.style.display = 'none';
10547
}
106-
107-
debounce(func, wait) {
108-
let timeout;
109-
return function executedFunction(...args) {
110-
const later = () => {
111-
clearTimeout(timeout);
112-
func(...args);
113-
};
114-
clearTimeout(timeout);
115-
timeout = setTimeout(later, wait);
116-
};
117-
}
11848
}
11949

12050
Stimulus.register("autocomplete", AutocompleteController)
121-

0 commit comments

Comments
 (0)