Skip to content

Commit

Permalink
fix dynamic event handler expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Nov 16, 2019
1 parent feafc34 commit 58b7b5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compiler/compile/nodes/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default class EventHandler extends Node {

this.reassigned = component.var_lookup.get(info.expression.name).reassigned;
}
} else if (this.expression.dynamic_dependencies().length > 0) {
this.reassigned = true;
}
} else {
this.handler_name = component.get_unique_name(`${sanitize(this.name)}_handler`);
Expand Down
20 changes: 20 additions & 0 deletions test/runtime/samples/event-handler-dynamic-expression/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
html: `<button>bar</button>`,

async test({ assert, component, target, window }) {
const [button] = target.querySelectorAll(
'button'
);

const event = new window.MouseEvent('click');

await button.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `<button>foo</button>`);

await button.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `<button>bar</button>`);

await button.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `<button>foo</button>`);
},
};
12 changes: 12 additions & 0 deletions test/runtime/samples/event-handler-dynamic-expression/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let name = 'bar';
function foo() {
name = 'foo';
}
function bar() {
name = 'bar';
}
</script>

<button on:click={name === 'bar' ? foo : bar}>{name}</button>

0 comments on commit 58b7b5b

Please sign in to comment.