Skip to content

Commit d881e0e

Browse files
committed
add search functionality
1 parent 7bfdae0 commit d881e0e

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/problems-by-company/company.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,11 @@ select {
8888
background-color: #373737;
8989
color: #fff;
9090
padding: 5px;
91+
}
92+
93+
#companySearch {
94+
background-color: #373737;
95+
color: #fff;
96+
padding: 5px;
97+
margin-bottom: 20px;
9198
}

src/problems-by-company/company.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<nav id="navbar">
1111
</nav>
1212
<div class="row">
13-
<select id="companySelect"></select>
13+
<input type="text" id="companySearch" list="companyList" placeholder="Search for a company...">
14+
<datalist id="companyList"></datalist>
1415
<select id="dateSelect">
1516
<option value="freq_6months">6 months</option>
1617
<option value="freq_1year">1 year</option>

src/problems-by-company/company.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ function addCompanyProblems(sortMethod: string) {
182182
}
183183

184184
async function addCompaniesToSelect() {
185-
const companySelect = document.getElementById('companySelect') as HTMLSelectElement;
185+
const companySearch = document.getElementById('companySearch') as HTMLInputElement;
186+
const companyList = document.getElementById('companyList') as HTMLDataListElement;
186187
let uniqueCompanies = new Set<string>();
187188

188189
const data = await new Promise<{ leetcodeProblems: LeetcodeProblems }>(resolve => {
@@ -199,17 +200,46 @@ async function addCompaniesToSelect() {
199200
}
200201
});
201202

203+
// Add event listener to the search input
204+
companySearch.addEventListener('input', () => {
205+
// Get the search term
206+
const searchTerm = companySearch.value.trim().toLowerCase();
207+
208+
// Clear the existing options
209+
companySelect.innerHTML = '';
210+
211+
// Filter and add the options based on the search term
212+
sortedCompanies.forEach((company) => {
213+
if (company.toLowerCase().includes(searchTerm)) {
214+
const option = document.createElement('option');
215+
option.value = company;
216+
option.text = company;
217+
if (company === companyName) {
218+
option.selected = true;
219+
}
220+
companySelect.appendChild(option);
221+
}
222+
});
223+
});
224+
225+
companySearch.addEventListener('keydown', (event) => {
226+
if (event.key === 'Enter') {
227+
const selectedCompany = companySearch.value;
228+
if (selectedCompany) {
229+
chrome.storage.local.set({ clickedCompany: selectedCompany }, () => {
230+
location.reload();
231+
});
232+
}
233+
}
234+
});
235+
202236
// Convert the Set to an Array and sort it alphabetically
203237
const sortedCompanies = Array.from(uniqueCompanies).sort();
204238

205239
sortedCompanies.forEach((company) => {
206240
const option = document.createElement('option');
207241
option.value = company;
208-
option.text = company;
209-
if (company === companyName) {
210-
option.selected = true;
211-
}
212-
companySelect.appendChild(option);
242+
companyList.appendChild(option);
213243
});
214244

215245
companySelect.addEventListener('change', () => {
@@ -271,4 +301,4 @@ function sortBy(column: string) {
271301
}
272302

273303
/* Run the script */
274-
main();
304+
main();

0 commit comments

Comments
 (0)