Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use Fragment VNodes for if-elseif-else children #3047

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement } from 'lwc';
import XComplex from 'x/complex';
import XTest from 'x/test';

describe('lwc:if, lwc:elseif, lwc:else directives', () => {
Expand All @@ -25,7 +26,7 @@ describe('lwc:if, lwc:elseif, lwc:else directives', () => {
expect(elm.shadowRoot.querySelector('.else')).not.toBeNull();
});

it('should update if the value changes', () => {
it('should update which branch is rendered if the value changes', () => {
const elm = createElement('x-test', { is: XTest });
elm.showIf = true;
document.body.appendChild(elm);
Expand All @@ -46,4 +47,61 @@ describe('lwc:if, lwc:elseif, lwc:else directives', () => {
expect(elm.shadowRoot.querySelector('.if')).not.toBeNull();
});
});

it('should render content when nested inside another if branch', () => {
const element = createElement('x-complex', { is: XComplex });
element.showNestedContent = true;
document.body.appendChild(element);

expect(element.shadowRoot.querySelector('.nestedContent')).not.toBeNull();
});

it('should rerender content when nested inside another if branch', () => {
const element = createElement('x-complex', { is: XComplex });
document.body.appendChild(element);

expect(element.shadowRoot.querySelector('.nestedElse')).not.toBeNull();

element.showNestedContent = true;
return Promise.resolve().then(() => {
expect(element.shadowRoot.querySelector('.nestedContent')).not.toBeNull();
});
});

it('should render list content properly', () => {
const element = createElement('x-complex', { is: XComplex });
element.showList = true;
document.body.appendChild(element);

expect(element.shadowRoot.querySelector('.if').textContent).toBe('123');
});

it('should rerender list content when updated', () => {
const element = createElement('x-complex', { is: XComplex });
document.body.appendChild(element);

expect(element.shadowRoot.querySelector('.else')).not.toBeNull();

element.showList = true;
return Promise.resolve()
.then(() => {
expect(element.shadowRoot.querySelector('.if').textContent).toBe('123');

element.refreshList();
})
.then(() => {
expect(element.shadowRoot.querySelector('.if').textContent).toBe('1234');

element.showList = false;
element.refreshList();
})
.then(() => {
expect(element.shadowRoot.querySelector('.else')).not.toBeNull();

element.showList = true;
})
.then(() => {
expect(element.shadowRoot.querySelector('.if').textContent).toBe('12345');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<div><h1>Header</h1></div>
<div lwc:if={showList} class="if">
<template for:each={items} for:item="item">
<div key={item}>{item}</div>
</template>
</div>
<div lwc:else class="else">
<template lwc:if={showNestedContent}>
<div class="nestedContent">Nested Content</div>
</template>
<template lwc:else>
<div class="nestedElse">Nothing for you</div>
</template>
<div>Nested Footer</div>
</div>
<div><h1>Footer</h1></div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LightningElement, api, track } from 'lwc';

export default class Complex extends LightningElement {
@api showNestedContent = false;
@api showList = false;
@track items = [1, 2, 3];

@api
refreshList() {
this.items.push(this.items.length + 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<h1${3}>if condition</h1>`;
const stc0 = {
key: 2,
key: 3,
};
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment, dc: api_dynamic_component } = $api;
return $cmp.visible.if
? [api_static_fragment($fragment1(), 1)]
: [api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)];
const {
st: api_static_fragment,
fr: api_fragment,
dc: api_dynamic_component,
} = $api;
return [
$cmp.visible.if
? api_fragment(0, [api_static_fragment($fragment1(), 2)], 0)
: api_fragment(
0,
[api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)],
0
),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,95 @@ const $fragment4 = parseFragment`<h1${3}>inner elseif</h1>`;
const $fragment5 = parseFragment`<h1${3}>inner else</h1>`;
const $fragment6 = parseFragment`<h1${3}>outer else</h1>`;
const stc0 = {
key: 8,
key: 11,
};
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment, dc: api_dynamic_component } = $api;
return $cmp.outer.if
? [api_static_fragment($fragment1(), 1)]
: $cmp.outer.elseif1
? [api_static_fragment($fragment2(), 3)]
: $cmp.outer.elseif2
? $cmp.inner.if
? [api_static_fragment($fragment3(), 5)]
: $cmp.inner.elseif
? [api_static_fragment($fragment4(), 7)]
: $cmp.inner.elseif2
? [api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)]
: [api_static_fragment($fragment5(), 10)]
: [api_static_fragment($fragment6(), 12)];
const {
st: api_static_fragment,
fr: api_fragment,
dc: api_dynamic_component,
} = $api;
return [
$cmp.outer.if
? api_fragment(0, [api_static_fragment($fragment1(), 2)], 0)
: api_fragment(
0,
[
$cmp.outer.elseif1
? api_fragment(3, [api_static_fragment($fragment2(), 5)], 0)
: api_fragment(
3,
[
$cmp.outer.elseif2
? api_fragment(
3,
[
$cmp.inner.if
? api_fragment(
6,
[api_static_fragment($fragment3(), 8)],
0
)
: api_fragment(
6,
[
$cmp.inner.elseif
? api_fragment(
6,
[
api_static_fragment(
$fragment4(),
10
),
],
0
)
: api_fragment(
6,
[
$cmp.inner.elseif2
? api_fragment(
6,
[
api_dynamic_component(
"x-foo",
$cmp.trackedProp.foo,
stc0
),
],
0
)
: api_fragment(
6,
[
api_static_fragment(
$fragment5(),
13
),
],
0
),
],
0
),
],
0
),
],
0
)
: api_fragment(
3,
[api_static_fragment($fragment6(), 15)],
0
),
],
0
),
],
0
),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@ import { parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<h1${3}>if condition</h1>`;
const $fragment2 = parseFragment`<h1${3}>elseif condition</h1>`;
const stc0 = {
key: 4,
key: 6,
};
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment, dc: api_dynamic_component } = $api;
return $cmp.visible.if
? [api_static_fragment($fragment1(), 1)]
: $cmp.visible.elseif
? [api_static_fragment($fragment2(), 3)]
: [api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)];
const {
st: api_static_fragment,
fr: api_fragment,
dc: api_dynamic_component,
} = $api;
return [
$cmp.visible.if
? api_fragment(0, [api_static_fragment($fragment1(), 2)], 0)
: api_fragment(
0,
[
$cmp.visible.elseif
? api_fragment(3, [api_static_fragment($fragment2(), 5)], 0)
: api_fragment(
3,
[api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)],
0
),
],
0
),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,49 @@ const $fragment1 = parseFragment`<h1${3}>if condition</h1>`;
const $fragment2 = parseFragment`<h1${3}>elseif condition</h1>`;
const $fragment3 = parseFragment`<h1${3}>else condition</h1>`;
const stc0 = {
key: 4,
key: 6,
};
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment, dc: api_dynamic_component } = $api;
return $cmp.visible.if
? [api_static_fragment($fragment1(), 1)]
: $cmp.visible.elseif
? [api_static_fragment($fragment2(), 3)]
: $cmp.visible.elseif2
? [api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)]
: [api_static_fragment($fragment3(), 6)];
const {
st: api_static_fragment,
fr: api_fragment,
dc: api_dynamic_component,
} = $api;
return [
$cmp.visible.if
? api_fragment(0, [api_static_fragment($fragment1(), 2)], 0)
: api_fragment(
0,
[
$cmp.visible.elseif
? api_fragment(3, [api_static_fragment($fragment2(), 5)], 0)
: api_fragment(
3,
[
$cmp.visible.elseif2
? api_fragment(
3,
[
api_dynamic_component(
"x-foo",
$cmp.trackedProp.foo,
stc0
),
],
0
)
: api_fragment(
3,
[api_static_fragment($fragment3(), 8)],
0
),
],
0
),
],
0
),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { parseFragment, registerTemplate } from "lwc";
const $fragment1 = parseFragment`<h1${3}>if condition</h1>`;
const stc0 = {
key: 2,
key: 4,
};
const stc1 = [];
function tmpl($api, $cmp, $slotset, $ctx) {
const { st: api_static_fragment, dc: api_dynamic_component } = $api;
return $cmp.visible.if
? [api_static_fragment($fragment1(), 1)]
: $cmp.visible.elseif
? [api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)]
: stc1;
const {
st: api_static_fragment,
fr: api_fragment,
dc: api_dynamic_component,
} = $api;
return [
$cmp.visible.if
? api_fragment(0, [api_static_fragment($fragment1(), 2)], 0)
: api_fragment(
0,
[
$cmp.visible.elseif
? api_fragment(
3,
[api_dynamic_component("x-foo", $cmp.trackedProp.foo, stc0)],
0
)
: null,
],
0
),
];
/*LWC compiler vX.X.X*/
}
export default registerTemplate(tmpl);
Expand Down
Loading