Skip to content

Commit

Permalink
Merge pull request #426 from wearepal/persisting-search-query
Browse files Browse the repository at this point in the history
perist search query
  • Loading branch information
paulthatjazz authored Sep 13, 2024
2 parents 47c17a3 + 089c704 commit 963b4ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
31 changes: 22 additions & 9 deletions app/javascript/controllers/filter_controller.js
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"
}
})
});
}
}
11 changes: 9 additions & 2 deletions app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
</div>
<div class="list-group list-group-flush" data-controller="filter">
<div class="list-group-item list-group-item-light">
<input placeholder="Search" class="form-control" data-action="input->filter#update" autofocus>
<input
type="text"
placeholder="Search"
class="form-control"
data-action="input->filter#update"
data-filter-target="input"
autofocus
>
</div>
<% @team_projects.sort_by { |project| project.name.downcase }.each do |project| %>
<div class="list-group-item list-group-item-action p-0 align-items-center" data-filter-target="item" style="display: flex;">
Expand Down Expand Up @@ -35,4 +42,4 @@
</div>
<% end %>
</div>
</div>
</div>

0 comments on commit 963b4ab

Please sign in to comment.