Skip to content

Commit

Permalink
fix dynamic components inside elements - fixes #993
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 10, 2017
1 parent cf94217 commit 7ca36c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/generators/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ export default class Component extends Node {
`if (${name}) ${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
);

const updateMountNode = this.getUpdateMountNode(anchor);

block.builders.update.addBlock(deindent`
if (${switch_vars.value} !== (${switch_vars.value} = ${snippet})) {
if (${name}) ${name}.destroy();
Expand All @@ -298,7 +300,7 @@ export default class Component extends Node {
${name}._fragment.c();
${this.children.map(child => remount(generator, child, name))}
${name}._mount(${anchor}.parentNode, ${anchor});
${name}._mount(${updateMountNode}, ${anchor});
${eventHandlers.map(handler => deindent`
${name}.on("${handler.name}", ${handler.var});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{x}}, therefore Bar</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{x}}, therefore Foo</p>
19 changes: 19 additions & 0 deletions test/runtime/samples/dynamic-component-inside-element/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
data: {
x: true
},

html: `
<div><p>true, therefore Foo</p></div>
`,

test(assert, component, target) {
component.set({
x: false
});

assert.htmlEqual(target.innerHTML, `
<div><p>false, therefore Bar</p></div>
`);
}
};
14 changes: 14 additions & 0 deletions test/runtime/samples/dynamic-component-inside-element/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div>
<:Component { x ? Foo : Bar } x='{{x}}'/>
</div>

<script>
import Foo from './Foo.html';
import Bar from './Bar.html';

export default {
data() {
return { Foo, Bar };
}
};
</script>

0 comments on commit 7ca36c8

Please sign in to comment.