diff --git a/app/javascript/controllers/list_filter_controller.js b/app/javascript/controllers/list_filter_controller.js index d0504572..dafb214a 100644 --- a/app/javascript/controllers/list_filter_controller.js +++ b/app/javascript/controllers/list_filter_controller.js @@ -2,15 +2,29 @@ import { Controller } from "@hotwired/stimulus"; // Basic functionality to filter a list based on a provided text attribute. export default class extends Controller { - static targets = ["input", "list"]; + static targets = ["input", "list", "emptyMessage"]; filter() { const filterValue = this.inputTarget.value.toLowerCase(); const items = this.listTarget.querySelectorAll(".filterable-item"); + let noMatchFound = true; + + if (this.hasEmptyMessageTarget) { + this.emptyMessageTarget.classList.add("hidden"); + } items.forEach((item) => { const text = item.getAttribute("data-filter-name").toLowerCase(); - item.style.display = text.includes(filterValue) ? "" : "none"; + const shouldDisplay = text.includes(filterValue); + item.style.display = shouldDisplay ? "" : "none"; + + if (shouldDisplay) { + noMatchFound = false; + } }); + + if (noMatchFound && this.hasEmptyMessageTarget) { + this.emptyMessageTarget.classList.remove("hidden"); + } } } diff --git a/app/views/transactions/_category_dropdown.html.erb b/app/views/transactions/_category_dropdown.html.erb index bf96369e..528e872f 100644 --- a/app/views/transactions/_category_dropdown.html.erb +++ b/app/views/transactions/_category_dropdown.html.erb @@ -5,18 +5,21 @@