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

Solid: fix Fragment w/ key prop #1634

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/warm-lobsters-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Solid] Fix: handle Fragment with `key` prop
124 changes: 92 additions & 32 deletions packages/core/src/__tests__/__snapshots__/solid.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3743,25 +3743,55 @@ export default MyComponent;
`;

exports[`Solid > jsx > Javascript Test > basicForFragment 1`] = `
"Property or signature expected. (26:17)
24 | {(option, _index) => {
25 | const index = _index();
> 26 | return < key={\`key-\${option}\`} ><div >{option}</div></>}}
| ^
27 |
28 | </For></div>
29 |"
"import { For } from \\"solid-js\\";

function BasicForFragment(props) {
return (
<>
<div>
<For each={[\\"a\\", \\"b\\", \\"c\\"]}>
{(option, _index) => {
const index = _index();
return (
<Fragment key={\`key-\${option}\`}>
<div>{option}</div>
</Fragment>
);
}}
</For>
</div>
</>
);
}

export default BasicForFragment;
"
`;

exports[`Solid > jsx > Javascript Test > basicForFragment 2`] = `
"Property or signature expected. (26:17)
24 | {(option, _index) => {
25 | const index = _index();
> 26 | return < key={\`key-\${option}\`} ><div >{option}</div></>}}
| ^
27 |
28 | </For></div>
29 |"
"import { For } from \\"solid-js\\";

function BasicForFragment(props) {
return (
<>
<div>
<For each={[\\"a\\", \\"b\\", \\"c\\"]}>
{(option, _index) => {
const index = _index();
return (
<Fragment key={\`key-\${option}\`}>
<div>{option}</div>
</Fragment>
);
}}
</For>
</div>
</>
);
}

export default BasicForFragment;
"
`;

exports[`Solid > jsx > Javascript Test > basicForNoTagReference 1`] = `
Expand Down Expand Up @@ -11957,25 +11987,55 @@ export default MyComponent;
`;

exports[`Solid > jsx > Typescript Test > basicForFragment 1`] = `
"Property or signature expected. (26:17)
24 | {(option, _index) => {
25 | const index = _index();
> 26 | return < key={\`key-\${option}\`} ><div >{option}</div></>}}
| ^
27 |
28 | </For></div>
29 |"
"import { For } from \\"solid-js\\";

function BasicForFragment(props: any) {
return (
<>
<div>
<For each={[\\"a\\", \\"b\\", \\"c\\"]}>
{(option, _index) => {
const index = _index();
return (
<Fragment key={\`key-\${option}\`}>
<div>{option}</div>
</Fragment>
);
}}
</For>
</div>
</>
);
}

export default BasicForFragment;
"
`;

exports[`Solid > jsx > Typescript Test > basicForFragment 2`] = `
"Property or signature expected. (26:17)
24 | {(option, _index) => {
25 | const index = _index();
> 26 | return < key={\`key-\${option}\`} ><div >{option}</div></>}}
| ^
27 |
28 | </For></div>
29 |"
"import { For } from \\"solid-js\\";

function BasicForFragment(props: any) {
return (
<>
<div>
<For each={[\\"a\\", \\"b\\", \\"c\\"]}>
{(option, _index) => {
const index = _index();
return (
<Fragment key={\`key-\${option}\`}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samijaber is Fragment a global component in solid? Found out here that solid fragments are already keyed: solidjs/solid#366 do we need to handle it? or just <>...</> should be sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch!

  • Fragment is not a global component in solid, or actually a component at all! I don't know why I thought it was
  • good find on the issue, can keep <>

fixed in #1639 to ignore all props in Fragments

<div>{option}</div>
</Fragment>
);
}}
</For>
</div>
</>
);
}

export default BasicForFragment;
"
`;

exports[`Solid > jsx > Typescript Test > basicForNoTagReference 1`] = `
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/generators/solid/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export const blockToSolid = (

let str = '';

if (json.name === 'Fragment') {
const isFragmentWithoutKey = json.name === 'Fragment' && !json.bindings.key;

if (isFragmentWithoutKey) {
str += '<';
} else {
str += `<${json.name} `;
Expand Down Expand Up @@ -128,7 +130,7 @@ export const blockToSolid = (
.join('\n');
}

if (json.name === 'Fragment') {
if (isFragmentWithoutKey) {
str += '</>';
} else {
str += `</${json.name}>`;
Expand Down