1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00
Maybe/app/javascript/controllers/tabs_controller.js
Zach Gollwitzer f0c2d4ead0
Implement transaction filtering UI (#578)
* Rough sketch of implementation

* Consolidate auto submit controller

* Store ransack params in session

* Improve how summary is calculated for txns

* Implement filters UI
2024-03-28 13:23:54 -04:00

44 lines
1.1 KiB
JavaScript

import { Controller } from "@hotwired/stimulus";
// Connects to data-controller="tabs"
export default class extends Controller {
static classes = ["active"];
static targets = ["btn", "tab"];
static values = { defaultTab: String };
connect() {
this.updateClasses(this.defaultTabValue);
document.addEventListener("turbo:load", this.onTurboLoad);
}
disconnect() {
document.removeEventListener("turbo:load", this.onTurboLoad);
}
select(event) {
this.updateClasses(event.target.dataset.id);
}
onTurboLoad = () => {
this.updateClasses(this.defaultTabValue);
};
updateClasses = (selectedId) => {
this.btnTargets.forEach((btn) =>
btn.classList.remove(...this.activeClasses)
);
this.tabTargets.forEach((tab) => tab.classList.add("hidden"));
this.btnTargets.forEach((btn) => {
if (btn.dataset.id === selectedId) {
btn.classList.add(...this.activeClasses);
}
});
this.tabTargets.forEach((tab) => {
if (tab.id === selectedId) {
tab.classList.remove("hidden");
}
});
};
}