-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support receiving events from class api templates
- Loading branch information
1 parent
03b5a8d
commit acaf99a
Showing
8 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
...ent-handlers/__snapshots__/misc-native-tag-event-handlers/basic/node.render.expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<button> | ||
emit 1 | ||
</button> | ||
<button> | ||
emit 2 | ||
</button> |
6 changes: 6 additions & 0 deletions
6
...vent-handlers/__snapshots__/misc-native-tag-event-handlers/basic/web.render.expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<button> | ||
emit 1 | ||
</button> | ||
<button> | ||
emit 2 | ||
</button> |
6 changes: 6 additions & 0 deletions
6
...vent-handlers/__snapshots__/misc-native-tag-event-handlers/basic/web.step-0.expected.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<button> | ||
emit 1 | ||
</button> | ||
<button> | ||
emit 2 | ||
</button> |
25 changes: 25 additions & 0 deletions
25
src/__tests__/fixtures/misc/class-api-event-handlers/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { spy, resetHistory } from "sinon"; | ||
import fixture from "../../../fixture"; | ||
|
||
const onValue = spy(); | ||
|
||
describe("misc native tag event handlers", () => { | ||
describe( | ||
"basic", | ||
fixture("./templates/basic.marko", [ | ||
{ onValue }, | ||
async ({ expect, fireEvent, screen }) => { | ||
expect(onValue).has.not.been.called; | ||
|
||
await fireEvent.click(screen.getByText("emit 1")); | ||
|
||
expect(onValue).has.been.calledOnceWith(1); | ||
resetHistory(); | ||
|
||
await fireEvent.click(screen.getByText("emit 2")); | ||
expect(onValue).has.been.calledOnceWith(2); | ||
resetHistory(); | ||
}, | ||
]) | ||
); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/__tests__/fixtures/misc/class-api-event-handlers/templates/basic.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<attrs/{ onValue }/> | ||
<fancy-button onClick=(() => onValue(1))>emit 1</fancy-button> | ||
<fancy-button onClick=(() => onValue(2))>emit 2</fancy-button> |
9 changes: 9 additions & 0 deletions
9
src/__tests__/fixtures/misc/class-api-event-handlers/templates/components/fancy-button.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class { | ||
handleClick() { | ||
this.emit("click"); | ||
} | ||
} | ||
|
||
<button onClick("handleClick")> | ||
<${input.renderBody}/> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { types as t } from "@marko/compiler"; | ||
import { loadFileForTag } from "@marko/babel-utils"; | ||
import isApi from "../util/is-api"; | ||
const eventNameReg = /^on[A-Z]/; | ||
|
||
// Translates all event handlers that are on class api child tags to use the `onEvent(handler)` api. | ||
// This allows tags api templates to consume events from class api templates. | ||
export default { | ||
MarkoTag(tag: t.NodePath<t.MarkoTag>) { | ||
if (isApi(tag, "class")) return; | ||
let childFile: t.BabelFile | undefined; | ||
|
||
for (const attr of tag.get("attributes")) { | ||
if ( | ||
attr.isMarkoAttribute() && | ||
!attr.node.arguments && | ||
eventNameReg.test(attr.node.name) | ||
) { | ||
if ( | ||
!(childFile ||= loadFileForTag(tag)) || | ||
isApi(childFile.path, "tags") | ||
) | ||
return; | ||
attr.node.arguments = [attr.node.value]; | ||
attr.node.value = t.booleanLiteral(true); | ||
} | ||
} | ||
}, | ||
} as t.Visitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { types as t } from "@marko/compiler"; | ||
import classApiCustomTagHandlers from "./class-api-custom-tag-handlers"; | ||
import nativeTagHandlers from "./native-tag-handlers/translate"; | ||
import trackRendering from "./track-rendering/translate"; | ||
import forKeyScope from "./for-key-scope"; | ||
export = [nativeTagHandlers, trackRendering, forKeyScope] as t.Visitor[]; | ||
|
||
export = [ | ||
classApiCustomTagHandlers, | ||
nativeTagHandlers, | ||
trackRendering, | ||
forKeyScope, | ||
] as t.Visitor[]; |