Skip to content

Commit

Permalink
Fix x-if inside x-for
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Apr 2, 2020
1 parent d5831bc commit b16a514
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 3 additions & 3 deletions dist/alpine-ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -5708,7 +5708,7 @@
component.showDirectiveLastElement = el;
}

function handleIfDirective(component, el, expressionResult, initialUpdate) {
function handleIfDirective(component, el, expressionResult, initialUpdate, extraVars) {
var _this = this;

if (el.nodeName.toLowerCase() !== 'template') console.warn("Alpine: [x-if] directive should only be added to <template> tags. See https://github.com/alpinejs/alpine#x-if");
Expand All @@ -5721,7 +5721,7 @@
transitionIn(el.nextElementSibling, function () {
_newArrowCheck(this, _this);
}.bind(this), initialUpdate);
component.initializeElements(el.nextElementSibling);
component.initializeElements(el.nextElementSibling, extraVars);
} else if (!expressionResult && elementHasAlreadyBeenAdded) {
transitionOut(el.nextElementSibling, function () {
_newArrowCheck(this, _this);
Expand Down Expand Up @@ -6407,7 +6407,7 @@
return i.type === 'for';
}.bind(this)).length > 0) return;
var output = this.evaluateReturnExpression(el, expression, extraVars);
handleIfDirective(this, el, output, initialUpdate);
handleIfDirective(this, el, output, initialUpdate, extraVars);
break;

case 'for':
Expand Down
6 changes: 3 additions & 3 deletions dist/alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
component.showDirectiveLastElement = el;
}

function handleIfDirective(component, el, expressionResult, initialUpdate) {
function handleIfDirective(component, el, expressionResult, initialUpdate, extraVars) {
if (el.nodeName.toLowerCase() !== 'template') console.warn(`Alpine: [x-if] directive should only be added to <template> tags. See https://github.com/alpinejs/alpine#x-if`);
const elementHasAlreadyBeenAdded = el.nextElementSibling && el.nextElementSibling.__x_inserted_me === true;

Expand All @@ -681,7 +681,7 @@
el.parentElement.insertBefore(clone, el.nextElementSibling);
el.nextElementSibling.__x_inserted_me = true;
transitionIn(el.nextElementSibling, () => {}, initialUpdate);
component.initializeElements(el.nextElementSibling);
component.initializeElements(el.nextElementSibling, extraVars);
} else if (!expressionResult && elementHasAlreadyBeenAdded) {
transitionOut(el.nextElementSibling, () => {
el.nextElementSibling.remove();
Expand Down Expand Up @@ -1498,7 +1498,7 @@
// We will let the "x-for" directive handle the "if"ing.
if (attrs.filter(i => i.type === 'for').length > 0) return;
var output = this.evaluateReturnExpression(el, expression, extraVars);
handleIfDirective(this, el, output, initialUpdate);
handleIfDirective(this, el, output, initialUpdate, extraVars);
break;

case 'for':
Expand Down
2 changes: 1 addition & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class Component {

var output = this.evaluateReturnExpression(el, expression, extraVars)

handleIfDirective(this, el, output, initialUpdate)
handleIfDirective(this, el, output, initialUpdate, extraVars)
break;

case 'for':
Expand Down
4 changes: 2 additions & 2 deletions src/directives/if.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transitionIn, transitionOut } from '../utils'

export function handleIfDirective(component, el, expressionResult, initialUpdate) {
export function handleIfDirective(component, el, expressionResult, initialUpdate, extraVars) {
if (el.nodeName.toLowerCase() !== 'template') console.warn(`Alpine: [x-if] directive should only be added to <template> tags. See https://github.com/alpinejs/alpine#x-if`)

const elementHasAlreadyBeenAdded = el.nextElementSibling && el.nextElementSibling.__x_inserted_me === true
Expand All @@ -14,7 +14,7 @@ export function handleIfDirective(component, el, expressionResult, initialUpdate

transitionIn(el.nextElementSibling, () => {}, initialUpdate)

component.initializeElements(el.nextElementSibling)
component.initializeElements(el.nextElementSibling, extraVars)
} else if (! expressionResult && elementHasAlreadyBeenAdded) {
transitionOut(el.nextElementSibling, () => {
el.nextElementSibling.remove()
Expand Down
21 changes: 21 additions & 0 deletions test/if.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,24 @@ test('elements inside x-if are still reactive', async () => {
expect(document.querySelector('span').innerText).toEqual('baz')
})
})

test('x-if works inside a loop', async () => {
document.body.innerHTML = `
<div x-data="{ foos: [{bar: 'baz'}, {bar: 'bop'}]}">
<template x-for="foo in foos">
<div>
<template x-if="foo.bar === 'baz'">
<div>
<span x-text="foo.bar"></span>
</div>
</template>
</div>
</template>
</div>
`

Alpine.start()

expect(document.querySelectorAll('span').length).toEqual(1)
expect(document.querySelector('span').innerText).toEqual('baz')
})

0 comments on commit b16a514

Please sign in to comment.