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.

fix: polish collection component (#2767)

Bogdan Chadkinc0702ba10a8c
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 94deca56bd..e202b6198e 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
@@ -2,8 +2,8 @@ import { useId, useMemo } from "react";
import { useStore } from "@nanostores/react";
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 type { DataSources, Instance } from "@webstudio-is/sdk";
+import { $dataSources, $instances } from "~/shared/nano-states";
import { serverSyncStore } from "~/shared/sync";
import {
BindingControl,
@@ -16,7 +16,9 @@ import {
VerticalLayout,
$selectedInstanceScope,
Label,
+ updateExpressionValue,
} from "../shared";
+import { decodeDataSourceVariable } from "@webstudio-is/react-sdk";
const useInstance = (instanceId: Instance["id"]) => {
const $store = useMemo(() => {
@@ -39,6 +41,14 @@ const updateChildren = (
});
};
+const isVariableIdentifier = (expression: string, dataSources: DataSources) => {
+ const potentialVariableId = decodeDataSourceVariable(expression);
+ return (
+ potentialVariableId !== undefined &&
+ dataSources.get(potentialVariableId)?.type === "variable"
+ );
+};
+
export const TextContent = ({
instanceId,
meta,
@@ -48,9 +58,13 @@ export const TextContent = ({
const instance = useInstance(instanceId);
// text content control is rendered only when empty or single child are present
const child = instance?.children?.[0] ?? { type: "text", value: "" };
- const localValue = useLocalValue(String(computedValue ?? ""), (value) =>
- updateChildren(instanceId, "text", value)
- );
+ const localValue = useLocalValue(String(computedValue ?? ""), (value) => {
+ if (child.type === "expression") {
+ updateExpressionValue(child.value, value);
+ } else {
+ updateChildren(instanceId, "text", value);
+ }
+ });
const id = useId();
const label = getLabel(meta, propName);
@@ -63,7 +77,11 @@ export const TextContent = ({
expression = child.value;
}
- const readOnly = child.type === "expression";
+ const dataSources = useStore($dataSources);
+ // allow editiing single variable expression
+ const readOnly =
+ child.type === "expression" &&
+ isVariableIdentifier(child.value, dataSources) === false;
return (
<VerticalLayout
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 3f012fe4d4..9dec646880 100644
--- a/apps/builder/app/builder/features/settings-panel/variables-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/variables-section.tsx
@@ -177,9 +177,6 @@ const VariablesList = () => {
value === undefined
? variable.name
: `${variable.name}: ${formatValuePreview(value)}`;
- // user should be able to create and delete
- const deletable =
- variable.type === "variable" || variable.type === "resource";
return (
<VariablePopoverTrigger key={variable.id} variable={variable}>
<CssValueListItem
@@ -187,12 +184,22 @@ const VariablesList = () => {
index={index}
label={<Label truncate>{label}</Label>}
buttons={
- <Tooltip content="Delete variable" side="bottom">
+ <Tooltip
+ content={
+ variable.type === "parameter"
+ ? "Variable is managed by the component and cannot be deleted"
+ : usedVariables.has(variable.id)
+ ? "Variable is used in bindings and cannot be deleted"
+ : "Delete variable"
+ }
+ side="bottom"
+ >
<SmallIconButton
tabIndex={-1}
// allow to delete only unused variables
disabled={
- deletable === false || usedVariables.has(variable.id)
+ variable.type === "parameter" ||
+ usedVariables.has(variable.id)
}
aria-label="Delete variable"
variant="destructive"
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 2edf703a98..d981d9626e 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -241,6 +241,15 @@ const mergeProps = (
return props;
};
+const getTextContent = (instanceProps: Record<string, unknown>) => {
+ const value = instanceProps[textContentAttribute];
+ // serialize objects and let react render literal types
+ if (typeof value === "object" && value !== null) {
+ return String(value);
+ }
+ return value as ReactNode;
+};
+
// eslint-disable-next-line react/display-name
export const WebstudioComponentCanvas = forwardRef<
HTMLElement,
@@ -258,7 +267,7 @@ export const WebstudioComponentCanvas = forwardRef<
useInstanceProps(instanceSelector);
const children =
- (instanceProps[textContentAttribute] as undefined | string) ??
+ getTextContent(instanceProps) ??
createInstanceChildrenElements({
instances,
instanceSelector,
@@ -450,7 +459,7 @@ export const WebstudioComponentPreview = forwardRef<
}
return (
<Component {...props} ref={ref}>
- {(instanceProps[textContentAttribute] as undefined | string) ??
+ {getTextContent(instanceProps) ??
createInstanceChildrenElements({
instances,
instanceSelector,
diff --git a/packages/icons/icons/list-view.svg b/packages/icons/icons/list-view.svg
index 9c62966508..d72ebd68f7 100644
--- a/packages/icons/icons/list-view.svg
+++ b/packages/icons/icons/list-view.svg
@@ -1,5 +1,5 @@
-<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<svg width="16" height="16" viewBox="0 0 16 16" 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"/>
+<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" />
</g>
</svg>
diff --git a/packages/icons/src/__generated__/list-view.tsx b/packages/icons/src/__generated__/list-view.tsx
index 2e5ec05ce3..64f8209ebd 100644
--- a/packages/icons/src/__generated__/list-view.tsx
+++ b/packages/icons/src/__generated__/list-view.tsx
@@ -7,7 +7,7 @@ import type { IconProps } from "../types";
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>
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width={size} height={size} fill={color} {...props} ref={forwardedRef}><path 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>
);
}
);
diff --git a/packages/icons/src/__generated__/svg/list-view.ts b/packages/icons/src/__generated__/svg/list-view.ts
index 3a6cb07617..41ed9ff374 100644
--- a/packages/icons/src/__generated__/svg/list-view.ts
+++ b/packages/icons/src/__generated__/svg/list-view.ts
@@ -1 +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>`;
+export const ListViewIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%" style="display: block;"><path 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/components/component-meta.ts b/packages/react-sdk/src/components/component-meta.ts
index 124465770e..e049d087bf 100644
--- a/packages/react-sdk/src/components/component-meta.ts
+++ b/packages/react-sdk/src/components/component-meta.ts
@@ -20,6 +20,7 @@ export type WsComponentPropsMeta = z.infer<typeof WsComponentPropsMeta>;
export const componentCategories = [
"general",
"text",
+ "data",
"media",
"forms",
"radix",
diff --git a/packages/react-sdk/src/core-components.ts b/packages/react-sdk/src/core-components.ts
index 03bd1667fa..b34b29b998 100644
--- a/packages/react-sdk/src/core-components.ts
+++ b/packages/react-sdk/src/core-components.ts
@@ -9,7 +9,7 @@ export const portalComponent = "Slot";
export const collectionComponent = "ws:collection";
const collectionMeta: WsComponentMeta = {
- category: "general",
+ category: "data",
order: 7,
type: "container",
label: "Collection",
@@ -23,24 +23,24 @@ const collectionMeta: WsComponentMeta = {
{
name: "data",
type: "json",
- value: ["apple", "orange", "banana"],
+ value: [
+ "Collection Item 1",
+ "Collection Item 2",
+ "Collection Item 3",
+ ],
+ },
+ {
+ name: "item",
+ type: "parameter",
+ variableName: "collectionItem",
+ variableAlias: "Collection Item",
},
- { name: "item", type: "parameter", variableName: "collectionItem" },
],
children: [
{
type: "instance",
component: "Box",
- children: [
- {
- type: "instance",
- component: "HtmlEmbed",
- props: [
- { name: "code", type: "expression", code: "collectionItem" },
- ],
- children: [],
- },
- ],
+ children: [{ type: "expression", value: "collectionItem" }],
},
],
},
diff --git a/packages/react-sdk/src/embed-template.test.ts b/packages/react-sdk/src/embed-template.test.ts
index f32ebae3c8..63953a0e66 100644
--- a/packages/react-sdk/src/embed-template.test.ts
+++ b/packages/react-sdk/src/embed-template.test.ts
@@ -295,6 +295,46 @@ test("generate data for embedding from props bound to data source variables", ()
});
});
+test("generate variables with aliases instead of reference name", () => {
+ expect(
+ generateDataFromEmbedTemplate(
+ [
+ {
+ type: "instance",
+ component: "Box",
+ variables: {
+ myVar: { alias: "My Variable", initialValue: false },
+ },
+ children: [],
+ },
+ ],
+ new Map(),
+ defaultBreakpointId
+ )
+ ).toEqual({
+ children: [{ type: "id", value: expectString }],
+ instances: [
+ { type: "instance", id: expectString, component: "Box", children: [] },
+ ],
+ props: [],
+ dataSources: [
+ {
+ type: "variable",
+ id: expectString,
+ scopeInstanceId: expectString,
+ name: "My Variable",
+ value: {
+ type: "boolean",
+ value: false,
+ },
+ },
+ ],
+ styleSourceSelections: [],
+ styleSources: [],
+ styles: [],
+ });
+});
+
test("generate data for embedding from props with complex expressions", () => {
expect(
generateDataFromEmbedTemplate(
@@ -496,6 +536,12 @@ test("generate data for embedding from parameter props", () => {
name: "myParameter",
variableName: "parameterName",
},
+ {
+ type: "parameter",
+ name: "anotherParameter",
+ variableName: "anotherParameterName",
+ variableAlias: "Another Parameter",
+ },
],
children: [],
},
@@ -505,6 +551,7 @@ test("generate data for embedding from parameter props", () => {
);
const instanceId = data.instances[0].id;
const variableId = data.dataSources[0].id;
+ const anotherVariableId = data.dataSources[1].id;
expect(data).toEqual({
children: [{ type: "id", value: instanceId }],
instances: [
@@ -523,6 +570,13 @@ test("generate data for embedding from parameter props", () => {
type: "parameter",
value: variableId,
},
+ {
+ id: expectString,
+ instanceId,
+ name: "anotherParameter",
+ type: "parameter",
+ value: anotherVariableId,
+ },
],
dataSources: [
{
@@ -531,7 +585,60 @@ test("generate data for embedding from parameter props", () => {
scopeInstanceId: instanceId,
name: "parameterName",
},
+ {
+ type: "parameter",
+ id: anotherVariableId,
+ scopeInstanceId: instanceId,
+ name: "Another Parameter",
+ },
+ ],
+ styleSourceSelections: [],
+ styleSources: [],
+ styles: [],
+ });
+});
+
+test("generate data for embedding from instance child bound to variables", () => {
+ expect(
+ generateDataFromEmbedTemplate(
+ [
+ {
+ type: "instance",
+ component: "Box",
+ variables: {
+ myValue: { initialValue: "text" },
+ },
+ children: [{ type: "expression", value: "myValue" }],
+ },
+ ],
+ new Map(),
+ defaultBreakpointId
+ )
+ ).toEqual({
+ children: [{ type: "id", value: expectString }],
+ instances: [
+ {
+ type: "instance",
+ id: expectString,
+ component: "Box",
+ children: [
+ {
+ type: "expression",
+ value: expect.stringMatching(/\$ws\$dataSource\$\w+/),
+ },
+ ],
+ },
],
+ dataSources: [
+ {
+ type: "variable",
+ id: expectString,
+ scopeInstanceId: expectString,
+ name: "myValue",
+ value: { type: "string", value: "text" },
+ },
+ ],
+ props: [],
styleSourceSelections: [],
styleSources: [],
styles: [],
diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.ts
index c1c8c1a65c..d5d47ac1d1 100644
--- a/packages/react-sdk/src/embed-template.ts
+++ b/packages/react-sdk/src/embed-template.ts
@@ -23,7 +23,15 @@ const EmbedTemplateText = z.object({
type EmbedTemplateText = z.infer<typeof EmbedTemplateText>;
+const EmbedTemplateExpression = z.object({
+ type: z.literal("expression"),
+ value: z.string(),
+});
+
+type EmbedTemplateExpression = z.infer<typeof EmbedTemplateExpression>;
+
const EmbedTemplateVariable = z.object({
+ alias: z.optional(z.string()),
initialValue: z.unknown(),
});
@@ -64,6 +72,7 @@ export const EmbedTemplateProp = z.union([
type: z.literal("parameter"),
name: z.string(),
variableName: z.string(),
+ variableAlias: z.optional(z.string()),
}),
z.object({
type: z.literal("action"),
@@ -104,7 +113,9 @@ export type EmbedTemplateInstance = {
props?: EmbedTemplateProp[];
tokens?: string[];
styles?: EmbedTemplateStyleDecl[];
- children: Array<EmbedTemplateInstance | EmbedTemplateText>;
+ children: Array<
+ EmbedTemplateInstance | EmbedTemplateText | EmbedTemplateExpression
+ >;
};
export const EmbedTemplateInstance: z.ZodType<EmbedTemplateInstance> = z.lazy(
@@ -122,7 +133,9 @@ export const EmbedTemplateInstance: z.ZodType<EmbedTemplateInstance> = z.lazy(
);
export const WsEmbedTemplate = z.lazy(() =>
- z.array(z.union([EmbedTemplateInstance, EmbedTemplateText]))
+ z.array(
+ z.union([EmbedTemplateInstance, EmbedTemplateText, EmbedTemplateExpression])
+ )
);
export type WsEmbedTemplate = z.infer<typeof WsEmbedTemplate>;
@@ -171,7 +184,7 @@ const createInstancesFromTemplate = (
type: "variable",
id: generateId(),
scopeInstanceId: instanceId,
- name,
+ name: variable.alias ?? name,
value: getVariablValue(variable.initialValue),
});
}
@@ -236,7 +249,7 @@ const createInstancesFromTemplate = (
type: "parameter",
id: dataSourceId,
scopeInstanceId: instanceId,
- name: prop.variableName,
+ name: prop.variableAlias ?? prop.variableName,
});
props.push({
id: propId,
@@ -344,6 +357,19 @@ const createInstancesFromTemplate = (
value: item.value,
});
}
+
+ if (item.type === "expression") {
+ parentChildren.push({
+ type: "expression",
+ // replace all references with variable names
+ value: validateExpression(item.value, {
+ transformIdentifier: (ref) => {
+ const id = dataSourceByRef.get(ref)?.id ?? ref;
+ return encodeDataSourceVariable(id);
+ },
+ }),
+ });
+ }
}
return parentChildren;
};
@@ -398,6 +424,9 @@ const namespaceEmbedTemplateComponents = (
if (item.type === "text") {
return item;
}
+ if (item.type === "expression") {
+ return item;
+ }
if (item.type === "instance") {
const prefix = components.has(item.component) ? `${namespace}:` : "";
return {