1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-20 05:39:39 +02:00
Maybe/app/javascript/controllers/tabs_controller.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

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() {
2024-03-01 17:14:06 -05:00
this.updateClasses(this.defaultTabValue);
document.addEventListener("turbo:load", this.onTurboLoad);
}
2024-03-01 17:14:06 -05:00
disconnect() {
document.removeEventListener("turbo:load", this.onTurboLoad);
}
2024-03-01 17:14:06 -05:00
select(event) {
const element = event.target.closest("[data-id]");
if (element) {
this.updateClasses(element.dataset.id);
}
2024-03-01 17:14:06 -05:00
}
onTurboLoad = () => {
this.updateClasses(this.defaultTabValue);
};
updateClasses = (selectedId) => {
this.btnTargets.forEach((btn) =>
btn.classList.remove(...this.activeClasses)
);
2024-03-01 17:14:06 -05:00
this.tabTargets.forEach((tab) => tab.classList.add("hidden"));
this.btnTargets.forEach((btn) => {
if (btn.dataset.id === selectedId) {
btn.classList.add(...this.activeClasses);
2024-03-01 17:14:06 -05:00
}
});
this.tabTargets.forEach((tab) => {
if (tab.id === selectedId) {
tab.classList.remove("hidden");
}
});
};
}