Merge pull request #2150 from sveltejs/gh-2149

allow event handlers to come from destructured var declarations
pull/2153/head
Rich Harris 6 years ago committed by GitHub
commit ac38a286e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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