-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enabled checkboxes for data type to govern which accordion is visible, …
…resolves #388
- Loading branch information
1 parent
a9b2ea6
commit 4fe53ac
Showing
3 changed files
with
76 additions
and
17 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
app/javascript/controllers/filter_visibility_controller.js
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Stimulus controller to toggle the visibility of accordions based on checkbox selection | ||
// and submit the form automatically when a checkbox is changed. | ||
export default class extends Controller { | ||
// Define targets for checkboxes and accordions | ||
static targets = ["checkbox", "accordion"]; | ||
|
||
// Called when the controller connects to the DOM | ||
connect() { | ||
// Attach event listeners to each checkbox | ||
this.checkboxTargets.forEach(checkbox => { | ||
checkbox.addEventListener("change", () => { | ||
this.toggleAccordions(); // Update the visibility of accordions | ||
|
||
// Submit the closest form when a checkbox is changed | ||
event.target.closest("form").submit(); | ||
}); | ||
}); | ||
|
||
// Initialize the visibility of accordions on page load | ||
this.toggleAccordions(); | ||
} | ||
|
||
// Function to toggle visibility of accordions based on checkbox states | ||
toggleAccordions() { | ||
this.accordionTargets.forEach(accordionWrapper => { | ||
// Find the checkbox that controls this accordion | ||
const checkbox = this.checkboxTargets.find(cb => cb.id === accordionWrapper.dataset.filterTarget); | ||
|
||
if (checkbox) { | ||
// Toggle the "d-none" class based on the checkbox state (hide if unchecked) | ||
accordionWrapper.classList.toggle("d-none", !checkbox.checked); | ||
} | ||
}); | ||
} | ||
} |
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
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