1
0
Эх сурвалжийг харах

Make .once honor false return value

Caleb Porzio 5 жил өмнө
parent
commit
024f5acf85

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/alpine.js


Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
dist/alpine.js.map


+ 4 - 4
src/directives/on.js

@@ -38,10 +38,10 @@ export function registerListener(component, el, event, modifiers, expression, ex
 
             if (returnValue === false) {
                 e.preventDefault()
-            }
-
-            if (modifiers.includes('once')) {
-                listenerTarget.removeEventListener(event, handler)
+            } else {
+                if (modifiers.includes('once')) {
+                    listenerTarget.removeEventListener(event, handler)
+                }
             }
         }
 

+ 27 - 1
test/on.spec.js

@@ -120,7 +120,7 @@ test('.once modifier', async () => {
         <div x-data="{ count: 0 }">
             <button x-on:click.once="count = count+1"></button>
 
-            <span x-bind:foo="count"
+            <span x-bind:foo="count"></span>
         </div>
     `
 
@@ -139,6 +139,32 @@ test('.once modifier', async () => {
     expect(document.querySelector('span').getAttribute('foo')).toEqual('1')
 })
 
+test('.once modifier doest remove listener if false is returned', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ count: 0 }">
+            <button x-on:click.once="return ++count === 2"></button>
+
+            <span x-bind:foo="count"></span>
+        </div>
+    `
+
+    Alpine.start()
+
+    expect(document.querySelector('span').getAttribute('foo')).toEqual('0')
+
+    document.querySelector('button').click()
+
+    await wait(() => { expect(document.querySelector('span').getAttribute('foo')).toEqual('1') })
+
+    document.querySelector('button').click()
+
+    await wait(() => { expect(document.querySelector('span').getAttribute('foo')).toEqual('2') })
+
+    await timeout(25)
+
+    expect(document.querySelector('span').getAttribute('foo')).toEqual('2')
+})
+
 test('keydown modifiers', async () => {
     document.body.innerHTML = `
         <div x-data="{ count: 0 }">

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно