Skip to content

Commit ef39308

Browse files
authored
[Docs Site] Add search bar to Glossary.astro (#23290)
1 parent fcaa125 commit ef39308

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

src/components/Glossary.astro

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ terms = terms.sort((a, b) => a.term.localeCompare(b.term));
1515
const INITIAL_VISIBLE_ROWS = 20;
1616
---
1717

18-
<table id="glossary-table">
18+
<div class="my-4">
19+
<input
20+
type="text"
21+
id="glossary-search"
22+
placeholder="Search terms..."
23+
class="focus:ring-cl1-brand-orange w-full rounded-md border border-gray-300 px-4 py-2 focus:border-transparent focus:ring-2 focus:outline-none"
24+
/>
25+
</div>
26+
<table id="glossary-table" class="table-fixed">
1927
<thead>
2028
<tr>
21-
<td>Term</td>
22-
<td>Definition</td>
23-
{!product && <td>Product</td>}
29+
<th class="min-w-64">Term</th>
30+
<th class="w-full">Definition</th>
31+
{!product && <th class="min-w-48">Product</th>}
2432
</tr>
2533
</thead>
2634
<tbody>
@@ -46,7 +54,7 @@ const INITIAL_VISIBLE_ROWS = 20;
4654
<div class="flex items-center justify-center">
4755
<button
4856
id="glossary-button"
49-
class="h-12 cursor-pointer rounded-sm bg-cl1-brand-orange px-6 font-medium text-cl1-black"
57+
class="bg-cl1-brand-orange text-cl1-black h-12 cursor-pointer rounded-sm px-6 font-medium"
5058
>
5159
View more terms
5260
</button>
@@ -55,39 +63,14 @@ const INITIAL_VISIBLE_ROWS = 20;
5563
}
5664

5765
<script>
58-
// function filterRows(search: string) {
59-
// const table =
60-
// document.querySelector<HTMLTableElement>("#glossary-table");
61-
62-
// if (!table) return;
63-
64-
// const rows =
65-
// table.querySelectorAll<HTMLTableRowElement>("tbody > tr");
66-
67-
// if (!rows) return;
68-
69-
// for (const row of rows) {
70-
// if (!row.textContent?.includes(search)) {
71-
// row.classList.add("hidden")
72-
// } else {
73-
// row.classList.remove("hidden")
74-
// }
75-
// }
76-
// }
77-
78-
// const search = document.querySelector<HTMLInputElement>("#glossary-search");
79-
80-
// if (search) {
81-
// search.addEventListener("input", () => {
82-
// filterRows(search.value)
83-
// })
84-
// }
85-
8666
const button = document.querySelector<HTMLAnchorElement>("#glossary-button");
67+
const searchInput =
68+
document.querySelector<HTMLInputElement>("#glossary-search");
69+
const table = document.querySelector<HTMLTableElement>("#glossary-table");
8770

88-
function showRows() {
89-
const table = document.querySelector<HTMLTableElement>("#glossary-table");
71+
let isSearching = false;
9072

73+
function showRows() {
9174
if (!table) return;
9275

9376
const rows =
@@ -102,10 +85,59 @@ const INITIAL_VISIBLE_ROWS = 20;
10285
extraRows.forEach((row) => row.classList.remove("hidden"));
10386
}
10487

88+
function filterRows(searchTerm: string) {
89+
if (!table) return;
90+
91+
const rows = table.querySelectorAll<HTMLTableRowElement>("tbody > tr");
92+
const normalizedSearchTerm = searchTerm.toLowerCase().trim();
93+
94+
isSearching = normalizedSearchTerm.length > 0;
95+
96+
if (button) {
97+
if (isSearching) {
98+
button.classList.add("hidden");
99+
} else {
100+
const hiddenRows =
101+
table.querySelectorAll<HTMLTableRowElement>("tbody > tr.hidden");
102+
if (hiddenRows.length > 0) {
103+
button.classList.remove("hidden");
104+
}
105+
}
106+
}
107+
108+
let index = 0;
109+
for (const row of rows) {
110+
if (isSearching) {
111+
const rowText = row.textContent?.toLowerCase() || "";
112+
const matches = rowText.includes(normalizedSearchTerm);
113+
114+
if (matches) {
115+
row.classList.remove("hidden");
116+
} else {
117+
row.classList.add("hidden");
118+
}
119+
} else {
120+
if (index > 20) {
121+
row.classList.add("hidden");
122+
} else {
123+
row.classList.remove("hidden");
124+
}
125+
}
126+
index++;
127+
}
128+
}
129+
105130
if (button) {
106131
button.addEventListener("click", (e) => {
107132
e.preventDefault();
108133
showRows();
109134
});
110135
}
136+
137+
if (searchInput) {
138+
searchInput.addEventListener("input", (e) => {
139+
const target = e.target as HTMLInputElement;
140+
filterRows(target.value);
141+
});
142+
}
111143
</script>

0 commit comments

Comments
 (0)