Skip to content

Commit

Permalink
debounce blur should only trigger once
Browse files Browse the repository at this point in the history
... when no other event was received since the last blur

Fixes #3689.
  • Loading branch information
SteffenDE committed Feb 28, 2025
1 parent 18f8133 commit b51b835
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 4 additions & 3 deletions assets/js/phoenix_live_view/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ let DOM = {
case null: return callback()

case "blur":
this.incCycle(el, "debounce-blur-cycle", () => {
if(asyncFilter()){ callback() }
})
if(this.once(el, "debounce-blur")){
el.addEventListener("blur", () => {
if(asyncFilter()){ callback() }
})
el.addEventListener("blur", () => this.triggerCycle(el, "debounce-blur-cycle"))
}
return

Expand Down
6 changes: 4 additions & 2 deletions assets/test/debounce_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let container = () => {
}

describe("debounce", function (){
test("triggers on input blur", async () => {
test("triggers once on input blur", async () => {
let calls = 0
let el = container().querySelector("input[name=blur]")

Expand All @@ -46,7 +46,7 @@ describe("debounce", function (){
DOM.dispatchEvent(el, "blur")
DOM.dispatchEvent(el, "blur")
DOM.dispatchEvent(el, "blur")
expect(calls).toBe(4)
expect(calls).toBe(1)
})

test("triggers debounce on input blur", async () => {
Expand All @@ -60,6 +60,8 @@ describe("debounce", function (){
simulateInput(el, "two")
simulateInput(el, "three")
DOM.dispatchEvent(el, "blur")
DOM.dispatchEvent(el, "blur")
DOM.dispatchEvent(el, "blur")
expect(calls).toBe(1)
expect(el.value).toBe("three")
})
Expand Down

0 comments on commit b51b835

Please sign in to comment.