Skip to content

Commit

Permalink
feat: allow filtering taxonomies with search box
Browse files Browse the repository at this point in the history
Closes #264
  • Loading branch information
razonyang committed Oct 25, 2024
1 parent d2edd1e commit 91b0dd0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
29 changes: 29 additions & 0 deletions assets/search/js/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,30 @@
show(dropdown)
}

const filtering = {}

Check warning on line 21 in assets/search/js/dropdown.ts

View workflow job for this annotation

GitHub Actions / lint

'filtering' is assigned a value but never used

document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('keyup', (e: Event) => {
const input = e.target as HTMLInputElement
if (!input.classList.contains('search-dropdown-input')) {
return
}

const value = input.value.toLowerCase().trim()
const dropdown = input.closest('.search-dropdown')
const items = Array.from(dropdown?.querySelectorAll<HTMLElement>('.search-dropdown-item') ?? [])
for (const item of items) {
if (value === '' || item.textContent?.toLowerCase().indexOf(value) !== -1) {
item.classList.remove('hide')
continue
}

if (!item.classList.contains('active')) {
item.classList.add('hide')
}
}
})

document.addEventListener('click', (e: Event) => {
const toggleEl = e.target.closest('.search-dropdown-toggle')
if (toggleEl) {
Expand All @@ -28,6 +51,12 @@
return
}

const input = e.target.closest('.search-dropdown-input')
if (input) {
// do not hide dropdown body when focus on input.
return
}

const item = e.target.closest('.search-dropdown-item')
if (item) {
const dropdown = item.closest('.search-dropdown')
Expand Down
25 changes: 17 additions & 8 deletions assets/search/js/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export default class Form {
${params.icons['lang']}
<span class="search-dropdown-label">${label}</span>
</button>
<ul class="search-dropdown-menu">${options}</ul>
<div class="search-dropdown-body">
<ul class="search-dropdown-menu">${options}</ul>
</div>
</div>`
}

Expand All @@ -91,11 +93,13 @@ export default class Form {
<button class="search-dropdown-toggle" type="button" aria-expanded="false" title="Sorting">
${params.icons['sort']} <span class="search-dropdown-label">${defaultSort}</span>
</button>
<ul class="search-dropdown-menu">
<li class="search-dropdown-item active" data-value="">${defaultSort}</li>
<li class="search-dropdown-item" data-value="date_asc">${translate('sort_by_date_asc')}</li>
<li class="search-dropdown-item" data-value="date_desc">${translate('sort_by_date_desc')}</li>
</ul>
<div class="search-dropdown-body">
<ul class="search-dropdown-menu">
<li class="search-dropdown-item active" data-value="">${defaultSort}</li>
<li class="search-dropdown-item" data-value="date_asc">${translate('sort_by_date_asc')}</li>
<li class="search-dropdown-item" data-value="date_desc">${translate('sort_by_date_desc')}</li>
</ul>
</div>
</div>`
}

Expand All @@ -114,7 +118,9 @@ export default class Form {
<button class="search-dropdown-toggle" type="button" aria-expanded="false" title="${label}">
${params.icons['year']} <span class="search-dropdown-label">${label}</span>
</button>
<ul class="search-dropdown-menu">${items}</ul>
<div class="search-dropdown-body">
<ul class="search-dropdown-menu">${items}</ul>
</div>
</div>`
}

Expand All @@ -137,7 +143,10 @@ export default class Form {
<button class="search-dropdown-toggle" type="button" aria-expanded="false" title="${label}">
${params.icons['taxonomies']} <span class="search-dropdown-label">${label}</span>
</button>
<ul class="search-dropdown-menu">${v}</ul>
<div class="search-dropdown-body">
<input class="search-dropdown-input">
<ul class="search-dropdown-menu">${v}</ul>
</div>
</div>`
}

Expand Down
38 changes: 29 additions & 9 deletions assets/search/scss/_dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,40 @@
}
}

.search-dropdown-menu {
.search-dropdown-body {
display: none;
left: 0;
list-style: none;
position: absolute;
background-color: var(--search-bg);
left: 0;
margin: 1.5rem 0 0;
min-width: 160px;
max-height: 50vh;
overflow: hidden auto;
background-color: var(--search-bg);
border: 1px solid var(--search-border-color);
border-radius: 0.25rem;
margin: 1.5rem 0 0;
max-height: 30vh;
overflow: hidden auto;
padding: 0.5rem 0 0.15rem;
z-index: 1;
scrollbar-width: thin;
}

.search-dropdown-input {
position: sticky;
top: 0;
outline: none;
border: none;
border-bottom: 2px solid var(--search-border-color);
padding: .25rem .5rem;
margin-bottom: .25rem;
}

.search-dropdown-menu {
list-style: none;
padding-left: 0;
margin-bottom: 0;
}

.search-dropdown-item {
color: var(--search-color);
margin: .25rem 0;
padding: 0.25rem 0.5rem;
white-space: nowrap;

Expand All @@ -42,6 +58,10 @@
background: var(--search-primary);
color: white;
}

&.hide {
display: none;
}
}

.search-dropdown {
Expand All @@ -51,7 +71,7 @@
position: relative;

&.show {
.search-dropdown-menu {
.search-dropdown-body {
display: block;
}
}
Expand Down

0 comments on commit 91b0dd0

Please sign in to comment.