Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: support binding in text content (#2756)

Bogdan Chadkined188ef215e0
diff --git a/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx
index 5e050b713d..94deca56bd 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/text-content.tsx
@@ -1,12 +1,10 @@
+import { useId, useMemo } from "react";
import { useStore } from "@nanostores/react";
-import {
- useId,
- TextArea,
- Flex,
- Label,
- theme,
-} from "@webstudio-is/design-system";
+import { computed } from "nanostores";
+import { TextArea } from "@webstudio-is/design-system";
import type { Instance } from "@webstudio-is/sdk";
+import { $instances } from "~/shared/nano-states";
+import { serverSyncStore } from "~/shared/sync";
import {
BindingControl,
BindingPopover,
@@ -17,11 +15,8 @@ import {
useLocalValue,
VerticalLayout,
$selectedInstanceScope,
+ Label,
} from "../shared";
-import { useMemo } from "react";
-import { computed } from "nanostores";
-import { $instances } from "~/shared/nano-states";
-import { serverSyncStore } from "~/shared/sync";
const useInstance = (instanceId: Instance["id"]) => {
const $store = useMemo(() => {
@@ -32,7 +27,7 @@ const useInstance = (instanceId: Instance["id"]) => {
const updateChildren = (
instanceId: Instance["id"],
- type: "text",
+ type: "text" | "expression",
value: string
) => {
serverSyncStore.createTransaction([$instances], (instances) => {
@@ -40,9 +35,7 @@ const updateChildren = (
if (instance === undefined) {
return;
}
- if (type === "text") {
- instance.children = [{ type: "text", value }];
- }
+ instance.children = [{ type, value }];
});
};
@@ -63,16 +56,21 @@ export const TextContent = ({
const { scope, aliases } = useStore($selectedInstanceScope);
let expression: undefined | string;
- if (child?.type === "text") {
+ if (child.type === "text") {
expression = JSON.stringify(child.value);
}
+ if (child.type === "expression") {
+ expression = child.value;
+ }
+
+ const readOnly = child.type === "expression";
return (
<VerticalLayout
label={
- <Flex align="center" css={{ gap: theme.spacing[3] }}>
- <Label truncate>{label}</Label>
- </Flex>
+ <Label htmlFor={id} description={meta.description} readOnly={readOnly}>
+ {label}
+ </Label>
}
deletable={false}
onDelete={() => {}}
@@ -80,6 +78,7 @@ export const TextContent = ({
<BindingControl>
<TextArea
id={id}
+ disabled={readOnly}
autoGrow
value={localValue.value}
rows={1}
@@ -96,9 +95,11 @@ export const TextContent = ({
return `${label} expects a string value`;
}
}}
- removable={false}
+ removable={child.type === "expression"}
value={expression}
- onChange={(_newExpression) => {}}
+ onChange={(newExpression) => {
+ updateChildren(instanceId, "expression", newExpression);
+ }}
onRemove={(evaluatedValue) =>
updateChildren(instanceId, "text", String(evaluatedValue))
}
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
index e356315cb0..a26831f651 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
@@ -5,6 +5,7 @@ import {
showAttribute,
decodeDataSourceVariable,
textContentAttribute,
+ collectionComponent,
} from "@webstudio-is/react-sdk";
import type { PropValue } from "../shared";
import { useStore } from "@nanostores/react";
@@ -196,11 +197,19 @@ export const usePropsLogic = ({
readOnly: isReadOnly(saved),
};
});
- const canHaveTextContent = instanceMeta?.type === "container";
+ const canHaveTextContent =
+ instanceMeta?.type === "container" &&
+ instance.component !== collectionComponent;
const hasNoChildren = instance.children.length === 0;
- const hasOnlyTextChildren =
+ const hasOnlyTextChild =
instance.children.length === 1 && instance.children[0].type === "text";
- if (canHaveTextContent && (hasNoChildren || hasOnlyTextChildren)) {
+ const hasOnlyExpressionChild =
+ instance.children.length === 1 &&
+ instance.children[0].type === "expression";
+ if (
+ canHaveTextContent &&
+ (hasNoChildren || hasOnlyTextChild || hasOnlyExpressionChild)
+ ) {
systemProps.push({
propName: textContentAttribute,
meta: {
diff --git a/apps/builder/app/builder/features/settings-panel/variables-section.tsx b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
index 1fc119aa63..3f012fe4d4 100644
--- a/apps/builder/app/builder/features/settings-panel/variables-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
@@ -23,6 +23,7 @@ import {
} from "@webstudio-is/react-sdk";
import {
$dataSources,
+ $instances,
$props,
$resources,
$selectedInstanceSelector,
@@ -70,52 +71,63 @@ const $instanceVariableValues = computed(
/**
* find variables used in
*
+ * instance children
* expression prop
* action prop
* url resource field
* header resource field
* body resource fiel
*/
-const $usedVariables = computed([$props, $resources], (props, resources) => {
- const usedVariables = new Set<DataSource["id"]>();
- const collectExpressionVariables = (expression: string) => {
- try {
- validateExpression(expression, {
- // parse any expression
- effectful: true,
- transformIdentifier: (identifier) => {
- const id = decodeDataSourceVariable(identifier);
- if (id !== undefined) {
- usedVariables.add(id);
- }
- return identifier;
- },
- });
- } catch {
- // empty block
- }
- };
- for (const resource of resources.values()) {
- collectExpressionVariables(resource.url);
- for (const { value } of resource.headers) {
- collectExpressionVariables(value);
- }
- if (resource.body) {
- collectExpressionVariables(resource.body);
+const $usedVariables = computed(
+ [$instances, $props, $resources],
+ (instances, props, resources) => {
+ const usedVariables = new Set<DataSource["id"]>();
+ const collectExpressionVariables = (expression: string) => {
+ try {
+ validateExpression(expression, {
+ // parse any expression
+ effectful: true,
+ transformIdentifier: (identifier) => {
+ const id = decodeDataSourceVariable(identifier);
+ if (id !== undefined) {
+ usedVariables.add(id);
+ }
+ return identifier;
+ },
+ });
+ } catch {
+ // empty block
+ }
+ };
+ for (const instance of instances.values()) {
+ for (const child of instance.children) {
+ if (child.type === "expression") {
+ collectExpressionVariables(child.value);
+ }
+ }
}
- }
- for (const prop of props.values()) {
- if (prop.type === "expression") {
- collectExpressionVariables(prop.value);
+ for (const resource of resources.values()) {
+ collectExpressionVariables(resource.url);
+ for (const { value } of resource.headers) {
+ collectExpressionVariables(value);
+ }
+ if (resource.body) {
+ collectExpressionVariables(resource.body);
+ }
}
- if (prop.type === "action") {
- for (const value of prop.value) {
- collectExpressionVariables(value.code);
+ for (const prop of props.values()) {
+ if (prop.type === "expression") {
+ collectExpressionVariables(prop.value);
+ }
+ if (prop.type === "action") {
+ for (const value of prop.value) {
+ collectExpressionVariables(value.code);
+ }
}
}
+ return usedVariables;
}
- return usedVariables;
-});
+);
const deleteVariable = (variableId: DataSource["id"]) => {
serverSyncStore.createTransaction(
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index 88865773a0..2edf703a98 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -26,6 +26,7 @@ import {
createInstanceChildrenElements,
collectionComponent,
type AnyComponent,
+ textContentAttribute,
} from "@webstudio-is/react-sdk";
import {
$propValuesByInstanceSelector,
@@ -256,13 +257,15 @@ export const WebstudioComponentCanvas = forwardRef<
const { [showAttribute]: show = true, ...instanceProps } =
useInstanceProps(instanceSelector);
- const children = createInstanceChildrenElements({
- instances,
- instanceSelector,
- children: instance.children,
- Component: WebstudioComponentCanvas,
- components,
- });
+ const children =
+ (instanceProps[textContentAttribute] as undefined | string) ??
+ createInstanceChildrenElements({
+ instances,
+ instanceSelector,
+ children: instance.children,
+ Component: WebstudioComponentCanvas,
+ components,
+ });
/**
* Prevents edited element from having a size of 0 on the first render.
* Directly using `children` in Text Edit
@@ -447,13 +450,14 @@ export const WebstudioComponentPreview = forwardRef<
}
return (
<Component {...props} ref={ref}>
- {createInstanceChildrenElements({
- instances,
- instanceSelector,
- children: instance.children,
- Component: WebstudioComponentPreview,
- components,
- })}
+ {(instanceProps[textContentAttribute] as undefined | string) ??
+ createInstanceChildrenElements({
+ instances,
+ instanceSelector,
+ children: instance.children,
+ Component: WebstudioComponentPreview,
+ components,
+ })}
</Component>
);
});
diff --git a/apps/builder/app/shared/nano-states/props.test.ts b/apps/builder/app/shared/nano-states/props.test.ts
index 28ea13ea3f..6c443c531a 100644
--- a/apps/builder/app/shared/nano-states/props.test.ts
+++ b/apps/builder/app/shared/nano-states/props.test.ts
@@ -483,6 +483,50 @@ test("compute instance text content when plain text", () => {
cleanStores($propValuesByInstanceSelector);
});
+test("compute instance text content bound to expression", () => {
+ $instances.set(
+ toMap([
+ {
+ id: "body",
+ type: "instance",
+ component: "Body",
+ children: [{ type: "id", value: "expressionBox" }],
+ },
+ {
+ id: "expressionBox",
+ type: "instance",
+ component: "Box",
+ children: [
+ { type: "expression", value: `"Hello " + $ws$dataSource$world` },
+ ],
+ },
+ ])
+ );
+ $dataSources.set(
+ toMap([
+ {
+ id: "world",
+ scopeInstanceId: "body",
+ name: "world",
+ type: "variable",
+ value: { type: "string", value: "world" },
+ },
+ ])
+ );
+ selectPageRoot("body");
+ expect($propValuesByInstanceSelector.get()).toEqual(
+ new Map([
+ [JSON.stringify(["body"]), new Map<string, unknown>()],
+ [
+ JSON.stringify(["expressionBox", "body"]),
+ new Map<string, unknown>([[textContentAttribute, "Hello world"]]),
+ ],
+ ])
+ );
+
+ cleanStores($propValuesByInstanceSelector);
+});
+
test("compute variable values for root", () => {
$instances.set(
toMap([{ id: "body", type: "instance", component: "Body", children: [] }])
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index f5a4ef4038..97299d9154 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -155,7 +155,7 @@ export const $propValuesByInstanceSelector = computed(
$assets,
],
(instances, props, page, dataSourcesLogic, params, pages, assets) => {
- const values = new Map<string, unknown>(dataSourcesLogic);
+ const variableValues = new Map<string, unknown>(dataSourcesLogic);
let propsList = Array.from(props.values());
// ignore asset and page props when params is not provided
@@ -197,14 +197,14 @@ export const $propValuesByInstanceSelector = computed(
continue;
}
if (prop.type === "expression") {
- const value = computeExpression(prop.value, values);
+ const value = computeExpression(prop.value, variableValues);
if (value !== undefined) {
propValues.set(prop.name, value);
}
continue;
}
if (prop.type === "action") {
- const action = values.get(prop.id);
+ const action = variableValues.get(prop.id);
if (typeof action === "function") {
propValues.set(prop.name, action);
}
@@ -227,7 +227,7 @@ export const $propValuesByInstanceSelector = computed(
const itemVariableId = parameters.get("item");
if (Array.isArray(data) && itemVariableId !== undefined) {
data.forEach((item, index) => {
- values.set(itemVariableId, item);
+ variableValues.set(itemVariableId, item);
for (const child of instance.children) {
if (child.type === "id") {
const indexId = getIndexedInstanceId(instanceId, index);
@@ -243,6 +243,12 @@ export const $propValuesByInstanceSelector = computed(
if (child.type === "text" && instance.children.length === 1) {
propValues.set(textContentAttribute, child.value);
}
+ if (child.type === "expression") {
+ const value = computeExpression(child.value, variableValues);
+ if (value !== undefined) {
+ propValues.set(textContentAttribute, value);
+ }
+ }
if (child.type === "id") {
traverseInstances([child.value, ...instanceSelector]);
}
diff --git a/packages/design-system/src/components/floating-panel-popover.tsx b/packages/design-system/src/components/floating-panel-popover.tsx
index 342f2de38c..c7a0bf2f1b 100644
--- a/packages/design-system/src/components/floating-panel-popover.tsx
+++ b/packages/design-system/src/components/floating-panel-popover.tsx
@@ -8,6 +8,7 @@ import * as Primitive from "@radix-ui/react-popover";
import { css, theme, type CSS } from "../stitches.config";
import { PanelTitle } from "./panel-title";
import { floatingPanelStyle, CloseButton, TitleSlot } from "./floating-panel";
+import { Flex } from "..";
export const FloatingPanelPopover = Primitive.Root;
@@ -55,12 +56,12 @@ export const FloatingPanelPopoverTitle = ({
<TitleSlot>
<PanelTitle
suffix={
- <>
+ <Flex gap={1}>
{actions}
<Primitive.Close asChild>
<CloseButton aria-label={closeLabel} />
</Primitive.Close>
- </>
+ </Flex>
}
>
{children}
diff --git a/packages/icons/icons/list-view.svg b/packages/icons/icons/list-view.svg
new file mode 100644
index 0000000000..9c62966508
--- /dev/null
+++ b/packages/icons/icons/list-view.svg
@@ -0,0 +1,5 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<g id="icon/list view">
+<path id="vector" fill-rule="evenodd" clip-rule="evenodd" d="M5.17304 1.3501H10.8269C11.3641 1.35009 11.8064 1.35008 12.1665 1.37951C12.5402 1.41004 12.8822 1.47547 13.2031 1.63893C13.7017 1.893 14.1071 2.29839 14.3611 2.79702C14.5246 3.11784 14.59 3.45987 14.6206 3.83357C14.65 4.19368 14.65 4.63601 14.65 5.17316V10.827C14.65 11.3642 14.65 11.8065 14.6206 12.1666C14.59 12.5403 14.5246 12.8824 14.3611 13.2032C14.1071 13.7018 13.7017 14.1072 13.2031 14.3613C12.8822 14.5247 12.5402 14.5902 12.1665 14.6207C11.8064 14.6501 11.3641 14.6501 10.8269 14.6501H5.17304C4.63588 14.6501 4.19356 14.6501 3.83345 14.6207C3.45975 14.5902 3.11772 14.5247 2.7969 14.3613C2.29827 14.1072 1.89287 13.7018 1.63881 13.2032C1.47534 12.8824 1.40992 12.5403 1.37938 12.1666C1.34996 11.8065 1.34997 11.3642 1.34998 10.827V5.17316C1.34997 4.636 1.34996 4.19368 1.37938 3.83357C1.40992 3.45987 1.47534 3.11784 1.63881 2.79702C1.89287 2.29839 2.29827 1.893 2.7969 1.63893C3.11772 1.47547 3.45975 1.41004 3.83345 1.37951C4.19356 1.35008 4.63588 1.35009 5.17304 1.3501ZM2.64998 6.6501V9.3501H13.35V6.6501H2.64998ZM13.35 5.3501H2.64998V5.2001C2.64998 4.62932 2.65048 4.24033 2.67507 3.93943C2.69903 3.64613 2.74259 3.49422 2.79712 3.38721C2.92655 3.13319 3.13307 2.92667 3.38709 2.79724C3.4941 2.74272 3.646 2.69915 3.93931 2.67519C4.24021 2.6506 4.6292 2.6501 5.19998 2.6501H10.8C11.3708 2.6501 11.7597 2.6506 12.0606 2.67519C12.3539 2.69915 12.5059 2.74272 12.6129 2.79724C12.8669 2.92667 13.0734 3.13319 13.2028 3.38721C13.2574 3.49422 13.3009 3.64613 13.3249 3.93943C13.3495 4.24033 13.35 4.62932 13.35 5.2001V5.3501ZM13.35 10.6501H2.64998V10.8001C2.64998 11.3709 2.65048 11.7599 2.67507 12.0608C2.69903 12.3541 2.74259 12.506 2.79712 12.613C2.92655 12.867 3.13307 13.0735 3.38709 13.203C3.4941 13.2575 3.646 13.301 3.93931 13.325C4.24021 13.3496 4.6292 13.3501 5.19998 13.3501H10.8C11.3708 13.3501 11.7597 13.3496 12.0606 13.325C12.3539 13.301 12.5059 13.2575 12.6129 13.203C12.8669 13.0735 13.0734 12.867 13.2028 12.613C13.2574 12.506 13.3009 12.3541 13.3249 12.0608C13.3495 11.7599 13.35 11.3709 13.35 10.8001V10.6501Z" fill="#11181C"/>
+</g>
+</svg>
diff --git a/packages/icons/src/__generated__/index.ts b/packages/icons/src/__generated__/index.ts
index d4eb70a365..719ed56e80 100644
--- a/packages/icons/src/__generated__/index.ts
+++ b/packages/icons/src/__generated__/index.ts
@@ -113,6 +113,7 @@ export * from "./link-2-unlinked";
export * from "./link-2";
export * from "./link";
export * from "./list-item";
+export * from "./list-view";
export * from "./list";
export * from "./loading-dots";
export * from "./menu-ellipses";
diff --git a/packages/icons/src/__generated__/list-view.tsx b/packages/icons/src/__generated__/list-view.tsx
new file mode 100644
index 0000000000..2e5ec05ce3
--- /dev/null
+++ b/packages/icons/src/__generated__/list-view.tsx
@@ -0,0 +1,15 @@
+// Generated from icons/list-view.svg
+
+import { forwardRef } from "react";
+import type { IconProps } from "../types";
+
+// prettier-ignore
+export const ListViewIcon = forwardRef<SVGSVGElement, IconProps>(
+ ({ color = "currentColor", size = 16, ...props }, forwardedRef) => {
+ return (
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width={size} height={size} fill={color} {...props} ref={forwardedRef}><path fill="#11181C" fillRule="evenodd" d="M5.173 1.35h5.654c.537 0 .98 0 1.34.03.373.03.715.095 1.036.259.499.254.904.66 1.158 1.158.164.32.229.663.26 1.037.029.36.029.802.029 1.34v5.653c0 .537 0 .98-.03 1.34-.03.373-.095.715-.259 1.036a2.65 2.65 0 0 1-1.158 1.158c-.32.164-.663.23-1.037.26-.36.03-.802.03-1.34.03H5.174c-.537 0-.98 0-1.34-.03-.373-.03-.715-.096-1.036-.26a2.65 2.65 0 0 1-1.158-1.158c-.164-.32-.23-.663-.26-1.036-.029-.36-.029-.803-.029-1.34V5.173c0-.537 0-.98.03-1.34.03-.373.095-.715.259-1.036a2.65 2.65 0 0 1 1.158-1.158c.32-.164.663-.229 1.036-.26.36-.029.803-.029 1.34-.029ZM2.65 6.65v2.7h10.7v-2.7H2.65Zm10.7-1.3H2.65V5.2c0-.57 0-.96.025-1.26.024-.294.068-.446.122-.553.13-.254.336-.46.59-.59.107-.054.259-.098.552-.122.301-.024.69-.025 1.261-.025h5.6c.57 0 .96 0 1.26.025.294.024.446.068.553.122.254.13.46.336.59.59.054.107.098.26.122.552.025.301.025.69.025 1.261v.15Zm0 5.3H2.65v.15c0 .57 0 .96.025 1.26.024.294.068.446.122.553.13.254.336.46.59.59.107.055.259.098.552.122.301.025.69.025 1.261.025h5.6c.57 0 .96 0 1.26-.025.294-.024.446-.067.553-.122.254-.13.46-.336.59-.59.054-.107.098-.259.122-.552.025-.301.025-.69.025-1.26v-.15Z" clipRule="evenodd" /></svg>
+ );
+ }
+);
+
+ListViewIcon.displayName = "ListViewIcon";
diff --git a/packages/icons/src/__generated__/svg/index.ts b/packages/icons/src/__generated__/svg/index.ts
index 6e8d774b1b..99489105bf 100644
--- a/packages/icons/src/__generated__/svg/index.ts
+++ b/packages/icons/src/__generated__/svg/index.ts
@@ -113,6 +113,7 @@ export { Link2UnlinkedIcon } from "./link-2-unlinked";
export { Link2Icon } from "./link-2";
export { LinkIcon } from "./link";
export { ListItemIcon } from "./list-item";
+export { ListViewIcon } from "./list-view";
export { ListIcon } from "./list";
export { LoadingDotsIcon } from "./loading-dots";
export { MenuEllipsesIcon } from "./menu-ellipses";
diff --git a/packages/icons/src/__generated__/svg/list-view.ts b/packages/icons/src/__generated__/svg/list-view.ts
new file mode 100644
index 0000000000..3a6cb07617
--- /dev/null
+++ b/packages/icons/src/__generated__/svg/list-view.ts
@@ -0,0 +1 @@
+export const ListViewIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path fill="#11181C" fill-rule="evenodd" d="M5.173 1.35h5.654c.537 0 .98 0 1.34.03.373.03.715.095 1.036.259.499.254.904.66 1.158 1.158.164.32.229.663.26 1.037.029.36.029.802.029 1.34v5.653c0 .537 0 .98-.03 1.34-.03.373-.095.715-.259 1.036a2.65 2.65 0 0 1-1.158 1.158c-.32.164-.663.23-1.037.26-.36.03-.802.03-1.34.03H5.174c-.537 0-.98 0-1.34-.03-.373-.03-.715-.096-1.036-.26a2.65 2.65 0 0 1-1.158-1.158c-.164-.32-.23-.663-.26-1.036-.029-.36-.029-.803-.029-1.34V5.173c0-.537 0-.98.03-1.34.03-.373.095-.715.259-1.036a2.65 2.65 0 0 1 1.158-1.158c.32-.164.663-.229 1.036-.26.36-.029.803-.029 1.34-.029ZM2.65 6.65v2.7h10.7v-2.7H2.65Zm10.7-1.3H2.65V5.2c0-.57 0-.96.025-1.26.024-.294.068-.446.122-.553.13-.254.336-.46.59-.59.107-.054.259-.098.552-.122.301-.024.69-.025 1.261-.025h5.6c.57 0 .96 0 1.26.025.294.024.446.068.553.122.254.13.46.336.59.59.054.107.098.26.122.552.025.301.025.69.025 1.261v.15Zm0 5.3H2.65v.15c0 .57 0 .96.025 1.26.024.294.068.446.122.553.13.254.336.46.59.59.107.055.259.098.552.122.301.025.69.025 1.261.025h5.6c.57 0 .96 0 1.26-.025.294-.024.446-.067.553-.122.254-.13.46-.336.59-.59.054-.107.098-.259.122-.552.025-.301.025-.69.025-1.26v-.15Z" clip-rule="evenodd"/></svg>`;
diff --git a/packages/react-sdk/src/component-generator.test.ts b/packages/react-sdk/src/component-generator.test.ts
index a37a76d658..42eb86fc8e 100644
--- a/packages/react-sdk/src/component-generator.test.ts
+++ b/packages/react-sdk/src/component-generator.test.ts
@@ -412,6 +412,33 @@ test("generate jsx children with text", () => {
);
});
+test("generate jsx children with expression", () => {
+ expect(
+ generateJsxChildren({
+ scope: createScope(),
+ children: [
+ { type: "expression", value: "'Hello ' + $ws$dataSource$var" },
+ ],
+ instances: new Map(),
+ props: new Map(),
+ dataSources: new Map([
+ createDataSourcePair({
+ id: "var",
+ scopeInstanceId: "body",
+ name: "my var",
+ type: "variable",
+ value: { type: "string", value: "world" },
+ }),
+ ]),
+ indexesWithinAncestors: new Map(),
+ })
+ ).toEqual(
+ clear(`
+ {'Hello ' + myvar}
+ `)
+ );
+});
+
test("generate jsx children with nested instances", () => {
expect(
generateJsxChildren({
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index 378c6354fc..6d7fe5f04e 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -209,6 +209,15 @@ export const generateJsxChildren = ({
.join(`<br />\n`);
continue;
}
+ if (child.type === "expression") {
+ const expression = generateExpression({
+ expression: child.value,
+ dataSources,
+ scope,
+ });
+ generatedChildren = `{${expression}}\n`;
+ continue;
+ }
if (child.type === "id") {
const instanceId = child.value;
const instance = instances.get(instanceId);
diff --git a/packages/react-sdk/src/core-components.ts b/packages/react-sdk/src/core-components.ts
index b12c08cf4b..03bd1667fa 100644
--- a/packages/react-sdk/src/core-components.ts
+++ b/packages/react-sdk/src/core-components.ts
@@ -1,4 +1,4 @@
-import { ListIcon } from "@webstudio-is/icons/svg";
+import { ListViewIcon } from "@webstudio-is/icons/svg";
import type {
WsComponentMeta,
WsComponentPropsMeta,
@@ -13,7 +13,7 @@ const collectionMeta: WsComponentMeta = {
order: 7,
type: "container",
label: "Collection",
- icon: ListIcon,
+ icon: ListViewIcon,
stylable: false,
template: [
{
diff --git a/packages/react-sdk/src/tree/create-elements-tree.tsx b/packages/react-sdk/src/tree/create-elements-tree.tsx
index 8ad5a630d7..925d2fed1a 100644
--- a/packages/react-sdk/src/tree/create-elements-tree.tsx
+++ b/packages/react-sdk/src/tree/create-elements-tree.tsx
@@ -94,18 +94,25 @@ export const createInstanceChildrenElements = ({
elements.push(renderText(child.value));
continue;
}
- const childInstance = instances.get(child.value);
- if (childInstance === undefined) {
+ if (child.type === "expression") {
continue;
}
- const childInstanceSelector = [child.value, ...instanceSelector];
- const element = createInstanceElement({
- instance: childInstance,
- instanceSelector: childInstanceSelector,
- Component,
- components,
- });
- elements.push(element);
+ if (child.type === "id") {
+ const childInstance = instances.get(child.value);
+ if (childInstance === undefined) {
+ continue;
+ }
+ const childInstanceSelector = [child.value, ...instanceSelector];
+ const element = createInstanceElement({
+ instance: childInstance,
+ instanceSelector: childInstanceSelector,
+ Component,
+ components,
+ });
+ elements.push(element);
+ continue;
+ }
+ child satisfies never;
}
// let empty children be coalesced with fallback
if (elements.length === 0) {
diff --git a/packages/sdk/src/schema/instances.ts b/packages/sdk/src/schema/instances.ts
index 35211842eb..851ba29960 100644
--- a/packages/sdk/src/schema/instances.ts
+++ b/packages/sdk/src/schema/instances.ts
@@ -1,26 +1,32 @@
import { z } from "zod";
-export const Text = z.object({
+export const TextChild = z.object({
type: z.literal("text"),
value: z.string(),
});
-export type Text = z.infer<typeof Text>;
+export type TextChild = z.infer<typeof TextChild>;
const InstanceId = z.string();
-export const Id = z.object({
+export const IdChild = z.object({
type: z.literal("id"),
value: InstanceId,
});
-export type Id = z.infer<typeof Id>;
+export type IdChild = z.infer<typeof IdChild>;
+
+export const ExpressionChild = z.object({
+ type: z.literal("expression"),
+ value: z.string(),
+});
+export type ExpressionChild = z.infer<typeof ExpressionChild>;
export const Instance = z.object({
type: z.literal("instance"),
id: InstanceId,
component: z.string(),
label: z.string().optional(),
- children: z.array(z.union([Id, Text])),
+ children: z.array(z.union([IdChild, TextChild, ExpressionChild])),
});
export type Instance = z.infer<typeof Instance>;