Skip to content

Commit

Permalink
event propagation shorthand for components (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 29, 2017
1 parent 51af8c2 commit 1b92f5f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/generators/dom/visitors/Component/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ export default function visitEventHandler(
local
) {
// TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.addSourcemapLocations(attribute.expression);
generator.code.prependRight(
attribute.expression.start,
`${block.alias('component')}.`
);

const usedContexts: string[] = [];
attribute.expression.arguments.forEach((arg: Node) => {
const { contexts } = block.contextualise(arg, null, true);

contexts.forEach(context => {
if (!~usedContexts.indexOf(context)) usedContexts.push(context);
if (!~local.allUsedContexts.indexOf(context))
local.allUsedContexts.push(context);
if (attribute.expression) {
generator.addSourcemapLocations(attribute.expression);
generator.code.prependRight(
attribute.expression.start,
`${block.alias('component')}.`
);

attribute.expression.arguments.forEach((arg: Node) => {
const { contexts } = block.contextualise(arg, null, true);

contexts.forEach(context => {
if (!~usedContexts.indexOf(context)) usedContexts.push(context);
if (!~local.allUsedContexts.indexOf(context))
local.allUsedContexts.push(context);
});
});
});
}

// TODO hoist event handlers? can do `this.__component.method(...)`
const declarations = usedContexts.map(name => {
Expand All @@ -42,7 +45,9 @@ export default function visitEventHandler(

const handlerBody =
(declarations.length ? declarations.join('\n') + '\n\n' : '') +
`[✂${attribute.expression.start}-${attribute.expression.end}✂];`;
(attribute.expression ?
`[✂${attribute.expression.start}-${attribute.expression.end}✂];` :
`${block.alias('component')}.fire('${attribute.name}', event);`);

local.create.addBlock(deindent`
${local.name}.on( '${attribute.name}', function ( event ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button on:click='fire("foo", { answer: 42 })'>click me</button>
18 changes: 18 additions & 0 deletions test/runtime/samples/event-handler-shorthand-component/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
html: `
<button>click me</button>
`,

test (assert, component, target, window) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');

let answer;
component.on('foo', event => {
answer = event.answer;
});

button.dispatchEvent(event);
assert.equal(answer, 42);
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/event-handler-shorthand-component/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Widget on:foo/>

<script>
import Widget from './Widget.html';

export default {
components: {
Widget
}
};
</script>

0 comments on commit 1b92f5f

Please sign in to comment.