allow event handlers to come from destructured var declarations - fixes #2149

pull/2150/head
Richard Harris 6 years ago
parent 5459b71276
commit 104818057f

@ -32,7 +32,7 @@ export default class EventHandler extends Node {
if (node && node.type === 'VariableDeclaration') {
// for `const handleClick = () => {...}`, we want the [arrow] function expression node
const declarator = node.declarations.find(d => d.id.name === info.expression.name);
node = declarator.init;
node = declarator && declarator.init;
}
if (node && /Function/.test(node.type) && node.params.length === 0) {

@ -0,0 +1,15 @@
export default {
html: `
<button>clicked: false</button>
`,
async test({ assert, component, target, window }) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');
await button.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, `
<button>clicked: true</button>
`);
}
};

@ -0,0 +1,16 @@
<script>
function get_handlers() {
return {
handle_click: () => {
clicked = true;
}
};
}
let clicked = false;
const { handle_click } = get_handlers();
</script>
<button on:click={handle_click}>
clicked: {clicked}
</button>
Loading…
Cancel
Save