-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from wearepal/persisting-search-query
perist search query
- Loading branch information
Showing
2 changed files
with
31 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,33 @@ | ||
import { Controller } from "stimulus" | ||
import { Controller } from "stimulus"; | ||
|
||
export default class extends Controller { | ||
static targets = [ | ||
"item" | ||
] | ||
static targets = ["input", "item"] | ||
|
||
connect() { | ||
const savedQuery = localStorage.getItem("searchQuery") | ||
if (savedQuery) { | ||
this.inputTarget.value = savedQuery | ||
this.update() | ||
} | ||
} | ||
|
||
update(event) { | ||
|
||
const query = this.inputTarget.value.toLowerCase() | ||
|
||
localStorage.setItem("searchQuery", query) | ||
|
||
this.itemTargets.forEach(target => { | ||
const searchText = target.innerText.toLocaleLowerCase() | ||
const searchTerms = event.target.value.toLocaleLowerCase().split(" ") | ||
const searchText = target.innerText.toLowerCase() | ||
const searchTerms = query.split(" ") | ||
|
||
if (searchTerms.every(term => searchText.includes(term))) { | ||
target.classList.remove("d-none") | ||
} | ||
else { | ||
target.style.display = "flex" | ||
} else { | ||
target.classList.add("d-none") | ||
target.style.display = "none" | ||
} | ||
}) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters