Skip to content

Commit 1b92f5f

Browse files
committed
event propagation shorthand for components (#638)
1 parent 51af8c2 commit 1b92f5f

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

src/generators/dom/visitors/Component/EventHandler.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@ export default function visitEventHandler(
1313
local
1414
) {
1515
// TODO verify that it's a valid callee (i.e. built-in or declared method)
16-
generator.addSourcemapLocations(attribute.expression);
17-
generator.code.prependRight(
18-
attribute.expression.start,
19-
`${block.alias('component')}.`
20-
);
21-
2216
const usedContexts: string[] = [];
23-
attribute.expression.arguments.forEach((arg: Node) => {
24-
const { contexts } = block.contextualise(arg, null, true);
2517

26-
contexts.forEach(context => {
27-
if (!~usedContexts.indexOf(context)) usedContexts.push(context);
28-
if (!~local.allUsedContexts.indexOf(context))
29-
local.allUsedContexts.push(context);
18+
if (attribute.expression) {
19+
generator.addSourcemapLocations(attribute.expression);
20+
generator.code.prependRight(
21+
attribute.expression.start,
22+
`${block.alias('component')}.`
23+
);
24+
25+
attribute.expression.arguments.forEach((arg: Node) => {
26+
const { contexts } = block.contextualise(arg, null, true);
27+
28+
contexts.forEach(context => {
29+
if (!~usedContexts.indexOf(context)) usedContexts.push(context);
30+
if (!~local.allUsedContexts.indexOf(context))
31+
local.allUsedContexts.push(context);
32+
});
3033
});
31-
});
34+
}
3235

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

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

4752
local.create.addBlock(deindent`
4853
${local.name}.on( '${attribute.name}', function ( event ) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button on:click='fire("foo", { answer: 42 })'>click me</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
html: `
3+
<button>click me</button>
4+
`,
5+
6+
test (assert, component, target, window) {
7+
const button = target.querySelector('button');
8+
const event = new window.MouseEvent('click');
9+
10+
let answer;
11+
component.on('foo', event => {
12+
answer = event.answer;
13+
});
14+
15+
button.dispatchEvent(event);
16+
assert.equal(answer, 42);
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Widget on:foo/>
2+
3+
<script>
4+
import Widget from './Widget.html';
5+
6+
export default {
7+
components: {
8+
Widget
9+
}
10+
};
11+
</script>

0 commit comments

Comments
 (0)