Skip to content

Commit 6eae04d

Browse files
authored
fix: generate hidden body correctly (#4179)
Here fixed rendering body with bound show prop.
1 parent d6e2177 commit 6eae04d

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/react-sdk/src/component-generator.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,42 @@ return <Body>
788788
`);
789789
});
790790

791+
test("generate conditional body", () => {
792+
expect(
793+
generateWebstudioComponent({
794+
classesMap: new Map(),
795+
scope: createScope(),
796+
name: "Page",
797+
rootInstanceId: "body",
798+
parameters: [],
799+
dataSources: toMap<DataSource>([
800+
{
801+
id: "conditionId",
802+
scopeInstanceId: "list",
803+
name: "conditionName",
804+
type: "variable",
805+
value: { type: "boolean", value: false },
806+
},
807+
]),
808+
indexesWithinAncestors: new Map(),
809+
...renderJsx(
810+
<$.Body
811+
ws:id="body"
812+
data-ws-show={new ExpressionValue("$ws$dataSource$conditionId")}
813+
></$.Body>
814+
),
815+
})
816+
).toMatchInlineSnapshot(`
817+
"const Page = () => {
818+
let [conditionName, set$conditionName] = useVariableState<any>(false)
819+
return (conditionName) &&
820+
<Body />
821+
822+
}
823+
"
824+
`);
825+
});
826+
791827
test("generate resource prop", () => {
792828
expect(
793829
generateWebstudioComponent({

packages/react-sdk/src/component-generator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const generatePropValue = ({
133133
};
134134

135135
export const generateJsxElement = ({
136+
context = "jsx",
136137
scope,
137138
instance,
138139
props,
@@ -142,6 +143,7 @@ export const generateJsxElement = ({
142143
children,
143144
classesMap,
144145
}: {
146+
context?: "expression" | "jsx";
145147
scope: Scope;
146148
instance: Instance;
147149
props: Props;
@@ -250,7 +252,13 @@ export const generateJsxElement = ({
250252
// {dataSourceVariable && <Instance>}
251253
if (conditionValue) {
252254
let conditionalElement = "";
253-
conditionalElement += `{(${conditionValue}) &&\n`;
255+
let before = "";
256+
let after = "";
257+
if (context === "jsx") {
258+
before = "{";
259+
after = "}";
260+
}
261+
conditionalElement += `${before}(${conditionValue}) &&\n`;
254262
// wrap collection with fragment when rendered inside condition
255263
// {dataSourceVariable &&
256264
// <>
@@ -264,7 +272,7 @@ export const generateJsxElement = ({
264272
} else {
265273
conditionalElement += generatedElement;
266274
}
267-
conditionalElement += `}\n`;
275+
conditionalElement += `${after}\n`;
268276
return conditionalElement;
269277
}
270278

@@ -323,6 +331,7 @@ export const generateJsxChildren = ({
323331
continue;
324332
}
325333
generatedChildren += generateJsxElement({
334+
context: "jsx",
326335
scope,
327336
instance,
328337
props,
@@ -377,6 +386,7 @@ export const generateWebstudioComponent = ({
377386

378387
const usedDataSources: DataSources = new Map();
379388
const generatedJsx = generateJsxElement({
389+
context: "expression",
380390
scope,
381391
instance,
382392
props,

0 commit comments

Comments
 (0)