Merge pull request #1119 from basecamp/nav-filter-handle-collapsed-sections

Nav filter handle collapsed sections
This commit is contained in:
Andy Smith
2025-09-18 13:32:01 -05:00
committed by GitHub
12 changed files with 62 additions and 12 deletions
@@ -11,7 +11,7 @@ export default class extends Controller {
filter() {
this.itemTargets.forEach(item => {
if (filterMatches(item.innerText, this.inputTarget.value)) {
if (filterMatches(item.textContent, this.inputTarget.value)) {
item.removeAttribute("hidden")
} else {
item.toggleAttribute("hidden", true)
@@ -0,0 +1,49 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = { key: String }
static targets = [ "input", "section" ]
sectionTargetConnected() {
this.#restoreToggles()
}
toggle(event) {
const section = event.target
if (section.hasAttribute("data-is-filtering")) return
const key = this.#localStorageKeyFor(section)
if (section.open) {
localStorage.removeItem(key)
} else {
localStorage.setItem(key, true)
}
}
showWhileFiltering() {
if (this.inputTarget.value) {
this.#expandAll();
} else {
this.#restoreToggles()
}
}
#expandAll() {
this.sectionTargets.forEach(section => {
section.setAttribute("data-is-filtering", true)
section.open = true
})
}
#restoreToggles() {
this.sectionTargets.forEach(section => {
const key = this.#localStorageKeyFor(section)
section.open = !localStorage.getItem(key)
section.removeAttribute("data-is-filtering")
})
}
#localStorageKeyFor(section) {
return section.getAttribute("data-nav-section-expander-key-value")
}
}