-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.js
58 lines (51 loc) · 2.33 KB
/
autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const createAutoComplete = ({
renderOption,
onOptionSelect,
inputValue,
fetchData,
rootElement
}) => {
//HTML for autocomplete dropdown
rootElement.innerHTML = `
<lable><b>Search</b></lable>
<input class="input" />
<div class="dropdown">
<div class="dropdown-menu">
<div class="dropdown-content results"></div>
</div>
</div>
`;
const inputElement = rootElement.querySelector('.input');
const dropdown = rootElement.querySelector('.dropdown');
const resultsWrapper = rootElement.querySelector('.results');
//fetching search results and renderning the results into the dropdown
const onInput = async (event) => {
const items = await fetchData(event.target.value); //wait until fetchData can return the search results
if(!items.length) {
dropdown.classList.remove('is-active');
return;
}
resultsWrapper.innerHTML = ""; // clearing the previous results before searching for a new item
dropdown.classList.add('is-active'); //is-active class to activate the dropdown
//looping through the items
for(let item of items) {
// each item should be an anchor element
const option = document.createElement('a');
option.classList.add('dropdown-item');
option.innerHTML = renderOption(item);
//appending the result as a child element of results
resultsWrapper.appendChild(option);
//if there is a click on any result, remove the dropdown menu and update the input text with the corresponding item's title
option.addEventListener('click', () => {
dropdown.classList.remove('is-active');
inputElement.value = inputValue(item);
onOptionSelect(item); //onOptionSelect takes the item and helps send a follow-up request with that item
});
}
}
inputElement.addEventListener('input', debounce(onInput)); //call debounce with a delay arg to set a delay, default 1000ms
// check if user clicks anywhere but the autocomplete widget to close the results bar
document.addEventListener('click', event => {
if(!rootElement.contains(event.target)) dropdown.classList.remove('is-active');
});
}