Skip to content

Commit

Permalink
Refactor event dispatching in Stimulus controllers
Browse files Browse the repository at this point in the history
Switched from using `stimulus-use` dispatch to explicit bubbling.
While using `stimulus-use`, targeting an upstream controller without
specifying the controller's name wasn't feasible in this context.
I've reverted to explicitly setting the event's bubbling property.
  • Loading branch information
rainerdema committed Sep 28, 2023
1 parent 91f3597 commit ba7dcbf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions admin/app/components/solidus_admin/ui/table/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class extends Controller {
}

connect() {
this.element.addEventListener("ui--table--ransack-filter:showSearch", () => this.showSearch())
this.element.addEventListener("ui--table--ransack-filter:search", () => this.search())
this.element.addEventListener("showSearch", () => this.showSearch())
this.element.addEventListener("search", () => this.search())

if (this.searchFieldTarget.value !== "") this.modeValue = "search"

Expand Down Expand Up @@ -120,7 +120,7 @@ export default class extends Controller {
}

disconnect() {
this.element.removeEventListener("ui--table--ransack-filter:showSearch", () => this.showSearch())
this.element.removeEventListener("ui--table--ransack-filter:search", () => this.search())
this.element.removeEventListener("showSearch", () => this.showSearch())
this.element.removeEventListener("search", () => this.search())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default class extends Controller {
}

showSearch() {
if (this.isAnyCheckboxChecked()) this.dispatch('showSearch')
if (this.isAnyCheckboxChecked()) {
this.element.dispatchEvent(new CustomEvent("showSearch", { bubbles: true }))
}
}

filterOptions(event) {
Expand All @@ -39,7 +41,8 @@ export default class extends Controller {
}

search() {
this.dispatch('search')
this.element.dispatchEvent(new CustomEvent("search", { bubbles: true }))

this.highlightFilter()
}

Expand Down

0 comments on commit ba7dcbf

Please sign in to comment.