-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c337213
commit f90c7e7
Showing
4 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
app/javascript/controllers/nested_checkbox_filter_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,71 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
// Connects to data-controller="nested-checkbox-filter" | ||
export default class extends Controller { | ||
static targets = ["parent", "child", "childrenContainer"] | ||
|
||
initialize() { | ||
this.toggle = this.toggle.bind(this) | ||
this.refresh = this.refresh.bind(this) | ||
} | ||
|
||
parentTargetConnected(parentCheckbox) { | ||
parentCheckbox.addEventListener("change", this.toggle) | ||
|
||
this.refresh() | ||
} | ||
|
||
parentTargetConnectedDisconnected(parentCheckbox) { | ||
parentCheckbox.removeEventListener("change", this.toggle) | ||
|
||
this.refresh() | ||
} | ||
|
||
childTargetConnected(childCheckbox) { | ||
childCheckbox.addEventListener("change", this.refresh) | ||
|
||
this.refresh() | ||
} | ||
|
||
childTargetDisconnected(childCheckbox) { | ||
childCheckbox.removeEventListener("change", this.refresh) | ||
|
||
this.refresh() | ||
} | ||
|
||
toggle(e) { | ||
e.preventDefault() | ||
|
||
this.childrenContainerTarget.classList.toggle("hidden", !e.target.checked) | ||
|
||
this.childTargets.forEach((checkbox) => { | ||
checkbox.checked = e.target.checked | ||
this.triggerInputEvent(checkbox) | ||
}) | ||
|
||
this.parentTarget.form.requestSubmit() | ||
} | ||
|
||
refresh() { | ||
const checkboxesCount = this.childTargets.length | ||
const checkboxesCheckedCount = this.checked.length | ||
|
||
this.parentTarget.checked = checkboxesCheckedCount > 0 | ||
this.parentTarget.indeterminate = checkboxesCheckedCount > 0 && checkboxesCheckedCount < checkboxesCount | ||
this.childrenContainerTarget.classList.toggle("hidden", !this.parentTarget.checked) | ||
} | ||
|
||
triggerInputEvent(checkbox) { | ||
const event = new Event("input", { bubbles: false, cancelable: true }) | ||
|
||
checkbox.dispatchEvent(event) | ||
} | ||
|
||
get checked() { | ||
return this.childTargets.filter((checkbox) => checkbox.checked) | ||
} | ||
|
||
get unchecked() { | ||
return this.childTargets.filter((checkbox) => !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