Delegate clearing the input to new controller

This commit is contained in:
Adrien Maston
2026-03-02 14:21:32 +01:00
parent 8a1a18562c
commit 340d9aa763
3 changed files with 29 additions and 7 deletions
+2 -5
View File
@@ -22,11 +22,8 @@ export default class extends Controller {
this.#hideItem(this.searchTarget)
}
clearInput() {
if (this.searchInputTarget.value) {
this.searchInputTarget.value = ""
this.searchInputTarget.focus()
} else {
clearInput(event) {
if (event.detail.isAlreadyEmpty) {
this.reset()
}
}
@@ -0,0 +1,17 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["searchInput"]
clearInput() {
this.dispatch("clear", { detail: { isAlreadyEmpty: this.isEmpty } })
if (!this.isEmpty) {
this.searchInputTarget.value = ""
this.searchInputTarget.focus()
}
}
get isEmpty() {
return !this.searchInputTarget.value
}
}