Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with state inside key of Fragment for React and Angular #1641

Merged
merged 9 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/yellow-coins-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@builder.io/mitosis': patch
---

[React, Angular] fix: issue with ``state`` inside `key` attribute in `Fragment`.

Example:

`<Fragment key={state.xxx + "abc"}...` was generated in React with `state.xxx` and in Angular without `this.`.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: snapshots-updates
path: ./packages/core/src/__tests__/__snapshots__
path: ./packages/core/src/**/__snapshots__
retention-days: 30

site:
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,31 @@ exports[`Alpine.js > jsx > Javascript Test > expressionState 1`] = `
"
`;

exports[`Alpine.js > jsx > Javascript Test > forKeyState 1`] = `
"<div x-data=\\"forKeyState()\\">
<template x-for=\\"option in ['a', 'b', 'c']\\">
<div x-bind:key=\\"\`\${id}-\${option}\`\\">
<div><span x-html=\\"option\\"></span></div>
</div>
</template>
<select>
<template x-for=\\"option in ['d', 'e', 'f']\\">
<option x-bind:key=\\"\`\${id}-\${option}\`\\" x-bind:value=\\"option\\">
<span x-html=\\"option\\"></span>
</option>
</template>
</select>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"forKeyState\\", () => ({
id: \\"xyz\\",
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > getterState 1`] = `
"<div x-data=\\"button()\\">
<p><span x-html=\\"foo2\\"></span></p>
Expand Down Expand Up @@ -4961,6 +4986,31 @@ exports[`Alpine.js > jsx > Typescript Test > expressionState 1`] = `
"
`;

exports[`Alpine.js > jsx > Typescript Test > forKeyState 1`] = `
"<div x-data=\\"forKeyState()\\">
<template x-for=\\"option in ['a', 'b', 'c']\\">
<div x-bind:key=\\"\`\${id}-\${option}\`\\">
<div><span x-html=\\"option\\"></span></div>
</div>
</template>
<select>
<template x-for=\\"option in ['d', 'e', 'f']\\">
<option x-bind:key=\\"\`\${id}-\${option}\`\\" x-bind:value=\\"option\\">
<span x-html=\\"option\\"></span>
</option>
</template>
</select>
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"forKeyState\\", () => ({
id: \\"xyz\\",
}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > getterState 1`] = `
"<div x-data=\\"button()\\">
<p><span x-html=\\"foo2\\"></span></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4463,6 +4463,59 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > forKeyState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"for-key-state\\",
template: \`
<div>
<ng-container
*ngFor=\\"let option of ['a', 'b', 'c']; trackBy: trackByOption0\\"
>
<ng-container>
<div>{{option}}</div>
</ng-container>
</ng-container>
<select>
<ng-container
*ngFor=\\"let option of ['d', 'e', 'f']; trackBy: trackByOption1\\"
>
<option [attr.value]=\\"option\\">{{option}}</option>
</ng-container>
</select>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class ForKeyState {
id = \\"xyz\\";
trackByOption0(_, option) {
return \`\${this.id}-\${option}\`;
}
trackByOption1(_, option) {
return \`\${this.id}-\${option}\`;
}
}

@NgModule({
declarations: [ForKeyState],
imports: [CommonModule],
exports: [ForKeyState],
})
export class ForKeyStateModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > getterState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -12182,6 +12235,59 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > forKeyState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"for-key-state\\",
template: \`
<div>
<ng-container
*ngFor=\\"let option of ['a', 'b', 'c']; trackBy: trackByOption0\\"
>
<ng-container>
<div>{{option}}</div>
</ng-container>
</ng-container>
<select>
<ng-container
*ngFor=\\"let option of ['d', 'e', 'f']; trackBy: trackByOption1\\"
>
<option [attr.value]=\\"option\\">{{option}}</option>
</ng-container>
</select>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class ForKeyState {
id = \\"xyz\\";
trackByOption0(_, option) {
return \`\${this.id}-\${option}\`;
}
trackByOption1(_, option) {
return \`\${this.id}-\${option}\`;
}
}

@NgModule({
declarations: [ForKeyState],
imports: [CommonModule],
exports: [ForKeyState],
})
export class ForKeyStateModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > getterState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4531,6 +4531,60 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > forKeyState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"for-key-state\\",
template: \`
<div>
<ng-container
*ngFor=\\"let option of ['a', 'b', 'c']; trackBy: trackByOption0\\"
>
<ng-container>
<div>{{option}}</div>
</ng-container>
</ng-container>
<select>
<ng-container
*ngFor=\\"let option of ['d', 'e', 'f']; trackBy: trackByOption1\\"
>
<option [attr.value]=\\"option\\">{{option}}</option>
</ng-container>
</select>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class ForKeyState {
id = \\"xyz\\";
trackByOption0(_, option) {
return \`\${this.id}-\${option}\`;
}
trackByOption1(_, option) {
return \`\${this.id}-\${option}\`;
}
}

@NgModule({
declarations: [ForKeyState],
imports: [CommonModule],
exports: [ForKeyState],
bootstrap: [SomeOtherComponent],
})
export class ForKeyStateModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > getterState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -12376,6 +12430,60 @@ export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > forKeyState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component } from \\"@angular/core\\";

@Component({
selector: \\"for-key-state\\",
template: \`
<div>
<ng-container
*ngFor=\\"let option of ['a', 'b', 'c']; trackBy: trackByOption0\\"
>
<ng-container>
<div>{{option}}</div>
</ng-container>
</ng-container>
<select>
<ng-container
*ngFor=\\"let option of ['d', 'e', 'f']; trackBy: trackByOption1\\"
>
<option [attr.value]=\\"option\\">{{option}}</option>
</ng-container>
</select>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class ForKeyState {
id = \\"xyz\\";
trackByOption0(_, option) {
return \`\${this.id}-\${option}\`;
}
trackByOption1(_, option) {
return \`\${this.id}-\${option}\`;
}
}

@NgModule({
declarations: [ForKeyState],
imports: [CommonModule],
exports: [ForKeyState],
bootstrap: [SomeOtherComponent],
})
export class ForKeyStateModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > getterState 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Loading