ソースを参照

Merge pull request #1021 from pminne/for-loop-integer-zero

Allow integer n to be 0 in for-loop
Caleb Porzio 4 年 前
コミット
c935521da1
2 ファイル変更18 行追加1 行削除
  1. 1 1
      src/directives/for.js
  2. 17 0
      test/for.spec.js

+ 1 - 1
src/directives/for.js

@@ -95,7 +95,7 @@ function evaluateItemsAndReturnEmptyIfXIfIsPresentAndFalseOnElement(component, e
     let items = component.evaluateReturnExpression(el, iteratorNames.items, extraVars)
 
     // This adds support for the `i in n` syntax.
-    if (isNumeric(items) && items > 0) {
+    if (isNumeric(items) && items >= 0) {
         items = Array.from(Array(items).keys(), i => i + 1)
     }
 

+ 17 - 0
test/for.spec.js

@@ -556,3 +556,20 @@ test('x-for with an array of numbers', async () => {
         expect(document.querySelectorAll('span')[1].textContent).toEqual('3')
     })
 })
+
+test('x-for over range using i in x syntax with i <= 0', async () => {
+    document.body.innerHTML = `
+        <div x-data="{ count: 1 }">
+            <template x-for="i in count">
+                <span x-text="i"></span>
+            </template>
+            <button @click="count--"></button>
+        </div>
+    `
+
+    Alpine.start()
+
+    document.querySelector('button').click()
+
+    await wait(() => { expect(document.querySelectorAll('span').length).toEqual(0) })
+})