JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Search/Filter Dropdown</h2>
<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<a href="#about">About</a>
<a href="#base">Base</a>
<a href="#blog">Blog</a>
<a href="#contact">Contact</a>
<a href="#custom">Custom</a>
<a href="#support">Support</a>
<a href="#tools">Tools</a>
</div>
</div>
</body>
</html>
CSS
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
#myInput {
box-sizing: border-box;
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: none;
border-bottom: 1px solid #ddd;
}
#myInput:focus {outline: 3px solid #ddd;}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f6f6f6;
min-width: 230px;
overflow: auto;
border: 1px solid #ddd;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
JavaScript
// Original code from https://www.w3schools.com/howto/howto_js_filter_dropdown.asp
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
//Additional key press code starts here
document.getElementById("myInput").addEventListener("keydown", function (e) {CheckArrrow(e.keyCode);});
var item = 0; //global var holding current highlit item
function CheckArrrow(e) {
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
item_original = item;
if (e==40) { //Down arrow
item++;
while (item<a.length && a[item].style.display=="none") {
item++; //find next visible element
}
if (item>=a.length) {
item = item_original;
}
else { //unhighlight old selection, highlight new selection
a[item_original].style.backgroundColor = "";
a[item].style.backgroundColor = "#ddd";
console.log(a[item].innerText);
}
}
if (e==38) { //Up arrow
item--;
while (item>=0 && a[item].style.display=="none") {
item--; //find next visible element
}
if (item<0) {
item = item_original;
}
else { //unhighlight old selection, highlight new selection
a[item_original].style.backgroundColor = "";
a[item].style.backgroundColor = "#ddd";
console.log(a[item].innerText);
}
}
if (e==13) { //Enter key
link = a[item].href;
console.log("Going to " + link);
...