Skip to content

Commit

Permalink
fix: async event handlers (#1614)
Browse files Browse the repository at this point in the history
* fix: support async actions

* 🌎

* 🐺

* 🍲
  • Loading branch information
manucorporat authored Oct 29, 2024
1 parent 7dfd585 commit c7d2f8c
Show file tree
Hide file tree
Showing 46 changed files with 870 additions and 592 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-penguins-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[All] fix: support async event handlers
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ exports[`Alpine.js > jsx > Javascript Test > BasicRef 1`] = `
exports[`Alpine.js > jsx > Javascript Test > BasicRefAssignment 1`] = `
"<div x-data=\\"myBasicRefAssignmentComponent()\\">
<button x-on:click=\\"handlerClick(evt)\\">Click</button>
<button x-on:click=\\"await handlerClick(evt)\\">Click</button>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Expand Down Expand Up @@ -1312,7 +1312,7 @@ exports[`Alpine.js > jsx > Javascript Test > Stamped.io 1`] = `
></textarea>
<button
class=\\"button\\"
x-on:click=\\"$event.preventDefault();
x-on:click=\\"ev.preventDefault();
showReviewPrompt = false\\"
>
Submit
Expand Down Expand Up @@ -3309,7 +3309,7 @@ exports[`Alpine.js > jsx > Typescript Test > BasicRef 1`] = `
exports[`Alpine.js > jsx > Typescript Test > BasicRefAssignment 1`] = `
"<div x-data=\\"myBasicRefAssignmentComponent()\\">
<button x-on:click=\\"handlerClick(evt)\\">Click</button>
<button x-on:click=\\"await handlerClick(evt)\\">Click</button>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Expand Down Expand Up @@ -4217,7 +4217,7 @@ exports[`Alpine.js > jsx > Typescript Test > Stamped.io 1`] = `
></textarea>
<button
class=\\"button\\"
x-on:click=\\"$event.preventDefault();
x-on:click=\\"ev.preventDefault();
showReviewPrompt = false\\"
>
Submit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ import { Component } from \\"@angular/core\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -7799,7 +7799,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ import { Component } from \\"@angular/core\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -7930,7 +7930,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ import { Component } from \\"@angular/core\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -2724,8 +2724,8 @@ export default class SmileReviews {
snakeCaseValue() {
return snakeCase(\\"testThis\\");
}
node_0_button = (event) => {
event.preventDefault();
node_0_button = (ev) => {
ev.preventDefault();
this.showReviewPrompt = false;
};
trackByReview0(index, review) {
Expand Down Expand Up @@ -8067,7 +8067,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -10269,8 +10269,8 @@ export default class SmileReviews {
snakeCaseValue() {
return snakeCase(\\"testThis\\");
}
node_0_button = (event) => {
event.preventDefault();
node_0_button = (ev) => {
ev.preventDefault();
this.showReviewPrompt = false;
};
trackByReview0(index, review) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ import { Component } from \\"@angular/core\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
})
export default class MyBasicRefAssignmentComponent {
Expand Down Expand Up @@ -6966,7 +6966,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
})
export default class MyBasicRefAssignmentComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ import { Component } from \\"@angular/core\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -1312,7 +1312,7 @@ import { CommonModule } from \\"@angular/common\\";
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -14632,7 +14632,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down Expand Up @@ -14672,7 +14672,7 @@ export interface Props {
@Component({
selector: \\"my-basic-ref-assignment-component\\",
template: \`
<div><button (click)=\\"handlerClick($event)\\">Click</button></div>
<div><button (click)=\\"await handlerClick($event)\\">Click</button></div>
\`,
styles: [
\`
Expand Down
101 changes: 93 additions & 8 deletions packages/core/src/__tests__/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3011,10 +3011,8 @@ exports[`Builder > Stamped 1`] = `
{
"@type": "@builder.io/sdk:Element",
"actions": {
"click": "
event.preventDefault();
state.showReviewPrompt = false;
",
"click": "event.preventDefault();
state.showReviewPrompt = false",
},
"bindings": {},
"children": [
Expand All @@ -3034,10 +3032,8 @@ exports[`Builder > Stamped 1`] = `
],
"code": {
"actions": {
"click": "
event.preventDefault();
state.showReviewPrompt = false;
",
"click": "event.preventDefault();
state.showReviewPrompt = false",
},
"bindings": {},
},
Expand Down Expand Up @@ -3469,6 +3465,95 @@ exports[`Builder > Text with bindings 3`] = `
}
`;

exports[`Builder > async bindings 1`] = `
{
"data": {
"blocks": [
{
"@type": "@builder.io/sdk:Element",
"actions": {},
"bindings": {},
"children": [
{
"@type": "@builder.io/sdk:Element",
"actions": {
"click": "(async () => await state.handlerClick(event))()",
},
"bindings": {},
"children": [
{
"@type": "@builder.io/sdk:Element",
"bindings": {},
"component": {
"name": "Text",
"options": {
"text": "Click",
},
},
"tagName": "span",
},
],
"code": {
"actions": {
"click": "(async () => await state.handlerClick(event))()",
},
"bindings": {},
},
"properties": {},
"tagName": "button",
},
],
"code": {
"actions": {},
"bindings": {},
},
"properties": {},
"tagName": "div",
},
],
"jsCode": "Object.assign(state, {
handlerClick: function handlerClick(event) {
event.preventDefault();
console.log(\\"current value\\", holdValueRef);
holdValueRef = holdValueRef + \\"JS\\";
},
});
",
"tsCode": "useStore({
handlerClick: function handlerClick(event) {
event.preventDefault();
console.log(\\"current value\\", holdValueRef);
holdValueRef = holdValueRef + \\"JS\\";
},
});
",
},
}
`;

exports[`Builder > async bindings 2`] = `
"import { useStore } from \\"@builder.io/mitosis\\";

export default function MyComponent(props) {
const state = useStore({
handlerClick: function handlerClick(event) {
event.preventDefault();
console.log(\\"current value\\", holdValueRef);
holdValueRef = holdValueRef + \\"JS\\";
},
});

return (
<div>
<button onClick={async (event) => await state.handlerClick(event)}>
Click
</button>
</div>
);
}
"
`;

exports[`Builder > bindings 1`] = `
{
"@type": "@builder.io/mitosis/component",
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/__tests__/__snapshots__/html.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1382,8 +1382,8 @@ exports[`Html > jsx > Javascript Test > BasicRefAssignment 1`] = `
}

// Event handler for 'click' event on button-1
function onButton1Click(evt) {
state.handlerClick(evt);
async function onButton1Click(evt) {
await state.handlerClick(evt);
}

// Update with initial state on first load
Expand Down Expand Up @@ -3155,8 +3155,8 @@ exports[`Html > jsx > Javascript Test > Stamped.io 1`] = `
}

// Event handler for 'click' event on button-2
function onButton2Click(event) {
event.preventDefault();
function onButton2Click(ev) {
ev.preventDefault();
state.showReviewPrompt = false;
update();
}
Expand Down Expand Up @@ -8631,8 +8631,8 @@ exports[`Html > jsx > Typescript Test > BasicRefAssignment 1`] = `
}

// Event handler for 'click' event on button-1
function onButton1Click(evt) {
state.handlerClick(evt);
async function onButton1Click(evt) {
await state.handlerClick(evt);
}

// Update with initial state on first load
Expand Down Expand Up @@ -10404,8 +10404,8 @@ exports[`Html > jsx > Typescript Test > Stamped.io 1`] = `
}

// Event handler for 'click' event on button-2
function onButton2Click(event) {
event.preventDefault();
function onButton2Click(ev) {
ev.preventDefault();
state.showReviewPrompt = false;
update();
}
Expand Down
Loading

0 comments on commit c7d2f8c

Please sign in to comment.