-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.html
131 lines (119 loc) · 3.91 KB
/
1.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.min.js"></script>
<style>
@import url("https://fonts.googleapis.com/css2?family=Raleway:wght@400;500;700&display=swap");
body {
font-family: "Raleway", sans-serif;
font-size: 15px;
cursor: pointer;
}
/* .form-element {
max-width: 900px;
margin: auto;
margin-top: 4em;
}
.form-element input {
width: calc(100% - 19px);
height: 45px;
border: none;
border-bottom: 1px solid #222;
outline: none;
margin: 0;
padding-left: 18px;
font-size: 40px;
padding-bottom: 0.4em;
} */
.query-filter {
position: relative;
}
.auto-complete {
position: absolute;
top: 100%;
width: 100%;
background: #fff;
}
.auto-complete__options {
display: none;
border: 1px solid #ddd;
padding: 1.6em 18px;
margin: 0;
list-style-type: none;
}
.auto-complete__options li {
margin-bottom: 1.5em;
font-size: 1.1em;
}
.auto-complete__options li:last-of-type {
margin-bottom: 0;
}
.form-element.active .auto-complete__options {
display: block;
}
</style>
</head>
<body>
<div>
<div class="form-element">
<form action="" autocomplete="off">
<div class="query-filter">
<input type="text" name="" id="search" placeholder="Search names">
<div class="auto-complete" tabindex="0">
<ul class="auto-complete__options">
</ul>
</div>
</div>
</form>
</div>
<script>
const searchInput = document.querySelector("#search");
const resultParent = document.querySelector(".auto-complete__options");
const formParent = document.querySelector(".form-element");
let searchOptions = [
"kenny",
"kenneth",
"ramey",
"dakota",
"kayden",
"thor",
"caroline",
"shadow",
"jelly"
];
const fuse = new Fuse(searchOptions, { threshold: 0.2 });
const updateResults = (event) => {
let list = "";
const inputField = event.currentTarget;
const searchResults = fuse.search(inputField.value);
// toggle auto complete dropdown
if (inputField.value.length > 0) {
formParent.classList.add("active");
} else {
formParent.classList.remove("active");
}
if (searchResults.length > 0) {
searchResults.map(
(listItem) => (list += `<li tabindex="0">${listItem.item}</li>`)
);
} else {
list = "No results found";
}
resultParent.innerHTML = list;
};
searchInput.addEventListener("keyup", (event) => updateResults(event));
resultParent.addEventListener("click", (event) => {
const clickedItem = event.target;
if (clickedItem.tagName === "LI") {
searchInput.value = clickedItem.textContent;
formParent.classList.remove("active");
}
});
</script>
</div>
</body>
</html>