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 dynamic event handler expression #3934

Merged
Show file tree
Hide file tree
Changes from all commits
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
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>