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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Fix nested collection data variable (#5835)
Oleg Isonen65d8a1670f78
diff --git a/apps/builder/app/builder/shared/binding-popover.tsx b/apps/builder/app/builder/shared/binding-popover.tsxindex 53f0e37f78..714f06a777 100644--- a/apps/builder/app/builder/shared/binding-popover.tsx+++ b/apps/builder/app/builder/shared/binding-popover.tsx@@ -26,6 +26,7 @@ import {FloatingPanel,Label,ScrollArea,+ ScrollAreaNative,SmallIconButton,Text,Tooltip,@@ -163,38 +164,40 @@ const BindingPanel = ({</Text></Flex>)}- <CssValueListArrowFocus>- {scopeEntries.map(([identifier, value], index) => {- const name = aliases.get(identifier);- const label =- value === undefined- ? name- : `${name}: ${formatValuePreview(value)}`;- return (- <CssValueListItem- key={identifier}- id={identifier}- index={index}- label={<Label truncate>{label}</Label>}- // mark all variables used in expression as selected- active={usedIdentifiers.has(identifier)}- // convert variable to expression- onClick={() => {- if (name) {- const nameIdentifier = encodeDataVariableName(name);- editorApiRef.current?.replaceSelection(nameIdentifier);- }- }}- // expression editor blur is fired after pointer down even- // preventing it allows to not trigger validation- // and flickering error tooltip- onPointerDown={(event) => {- event.preventDefault();- }}- />- );- })}- </CssValueListArrowFocus>+ <ScrollAreaNative css={{ maxHeight: theme.spacing[25] }}>+ <CssValueListArrowFocus>+ {scopeEntries.map(([identifier, value], index) => {+ const name = aliases.get(identifier);+ const label =+ value === undefined+ ? name+ : `${name}: ${formatValuePreview(value)}`;+ return (+ <CssValueListItem+ key={identifier}+ id={identifier}+ index={index}+ label={<Label truncate>{label}</Label>}+ // mark all variables used in expression as selected+ active={usedIdentifiers.has(identifier)}+ // convert variable to expression+ onClick={() => {+ if (name) {+ const nameIdentifier = encodeDataVariableName(name);+ editorApiRef.current?.replaceSelection(nameIdentifier);+ }+ }}+ // expression editor blur is fired after pointer down even+ // preventing it allows to not trigger validation+ // and flickering error tooltip+ onPointerDown={(event) => {+ event.preventDefault();+ }}+ />+ );+ })}+ </CssValueListArrowFocus>+ </ScrollAreaNative></Box><Flex gap="1" css={{ padding: theme.panel.padding }}><Text variant="labels">Expression editor</Text>diff --git a/apps/builder/app/shared/nano-states/props.test.tsx b/apps/builder/app/shared/nano-states/props.test.tsxindex 5d8f177eaf..e07f830884 100644--- a/apps/builder/app/shared/nano-states/props.test.tsx+++ b/apps/builder/app/shared/nano-states/props.test.tsx@@ -447,6 +447,63 @@ test("compute expression from object collection items", () => {cleanStores($propValuesByInstanceSelector);});+test("compute prop values inside collection without item parameter", () => {+ $instances.set(+ toMap([+ {+ id: "list",+ type: "instance",+ component: collectionComponent,+ children: [{ type: "id", value: "item" }],+ },+ {+ id: "item",+ type: "instance",+ component: "Box",+ children: [],+ },+ ])+ );+ selectPageRoot("list");+ $dataSources.set(new Map());+ $props.set(+ toMap([+ {+ id: "prop1",+ name: "data",+ instanceId: "list",+ type: "json",+ value: ["orange", "apple"],+ },+ {+ id: "prop2",+ name: "ariaLabel",+ instanceId: "item",+ type: "string",+ value: "collection item",+ },+ ])+ );+ expect($propValuesByInstanceSelector.get()).toEqual(+ new Map([+ [+ getInstanceKey(["list"]),+ new Map<string, unknown>([["data", ["orange", "apple"]]]),+ ],+ [+ getInstanceKey(["item", "list[0]", "list"]),+ new Map<string, unknown>([["ariaLabel", "collection item"]]),+ ],+ [+ getInstanceKey(["item", "list[1]", "list"]),+ new Map<string, unknown>([["ariaLabel", "collection item"]]),+ ],+ ])+ );++ cleanStores($propValuesByInstanceSelector);+});+test("access parameter value from variables values", () => {$instances.set(toMap([@@ -984,6 +1041,55 @@ test("compute item values for collection with nested object data", () => {).toEqual({ name: "Bob", age: 25 });});+test("compute inherited item values inside collection without item parameter", () => {+ const dataVariable = new Variable("dataVariable", [+ { items: ["apple", "banana"] },+ ]);+ const outerItem = new Parameter("Outer Item");+ const data = renderData(+ <$.Body ws:id="bodyId">+ <ws.collection+ ws:id="outerCollectionId"+ data={expression`${dataVariable}`}+ item={outerItem}+ >+ <ws.collection+ ws:id="innerCollectionId"+ data={expression`${outerItem}.items`}+ >+ <$.Box ws:id="boxId"></$.Box>+ </ws.collection>+ </ws.collection>+ </$.Body>+ );+ $instances.set(data.instances);+ $dataSources.set(data.dataSources);+ $props.set(data.props);+ const outerItemParameterId = Array.from(data.dataSources.values()).find(+ (dataSource) => dataSource.name === "Outer Item"+ )?.id;+ selectPageRoot("bodyId");+ $dataSourceVariables.set(new Map([]));+ const values = $variableValuesByInstanceSelector.get();+ expect(+ values+ .get(+ getInstanceKey([+ "boxId",+ "innerCollectionId[0]",+ "innerCollectionId",+ "outerCollectionId[0]",+ "outerCollectionId",+ "bodyId",+ ROOT_INSTANCE_ID,+ ])+ )+ ?.get(outerItemParameterId ?? "")+ ).toEqual({+ items: ["apple", "banana"],+ });+});+test("compute resource variable values", () => {const resourceVariable = new ResourceValue("resourceVariable", {url: expression`""`,diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.tsindex 8af1e5a129..65880775bc 100644--- a/apps/builder/app/shared/nano-states/props.ts+++ b/apps/builder/app/shared/nano-states/props.ts@@ -316,9 +316,11 @@ export const $propValuesByInstanceSelector = computed(const originalData = propValues.get("data");const itemVariableId = parameters.get("item");const itemKeyVariableId = parameters.get("itemKey");- if (itemVariableId !== undefined && originalData) {+ if (originalData) {for (const [key, value] of getCollectionEntries(originalData)) {- variableValues.set(itemVariableId, value);+ if (itemVariableId !== undefined) {+ variableValues.set(itemVariableId, value);+ }if (itemKeyVariableId !== undefined) {variableValues.set(itemKeyVariableId, key);}@@ -520,18 +522,19 @@ export const $variableValuesByInstanceSelector = computed(const originalData = propValues.get("data");const itemVariableId = parameters.get("item");const itemKeyVariableId = parameters.get("itemKey");- if (itemVariableId === undefined) {- return;- }// prevent accessing item from collection- variableValues.delete(itemVariableId);+ if (itemVariableId !== undefined) {+ variableValues.delete(itemVariableId);+ }if (itemKeyVariableId !== undefined) {variableValues.delete(itemKeyVariableId);}if (originalData) {for (const [key, value] of getCollectionEntries(originalData)) {const itemVariableValues = new Map(variableValues);- itemVariableValues.set(itemVariableId, value);+ if (itemVariableId !== undefined) {+ itemVariableValues.set(itemVariableId, value);+ }if (itemKeyVariableId !== undefined) {itemVariableValues.set(itemKeyVariableId, key);}diff --git a/packages/cli/src/prebuild.test.ts b/packages/cli/src/prebuild.test.tsindex 3ca58b3ca1..672d6cee46 100644--- a/packages/cli/src/prebuild.test.ts+++ b/packages/cli/src/prebuild.test.ts@@ -18,6 +18,7 @@ let tempDir: string;let consoleInfo: ReturnType<typeof vi.spyOn>;const rootFolderId = "root";const elementComponent = "ws:element";+const slowPrebuildTestTimeout = 15_000;type Redirects = Array<{ old: string; new: string; status?: "301" | "302" }>;const getFilePaths = async (dir: string): Promise<string[]> => {@@ -501,35 +502,41 @@ describe("prebuild", () => {).resolves.not.toContain("CustomCode");});- test("downloads assets only when requested by prebuild", async () => {- const fetch = vi.fn(async () => ({- ok: false,- statusText: "Not Found",- }));- globalThis.fetch = fetch as unknown as typeof globalThis.fetch;- const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});- const consoleError = vi- .spyOn(console, "error")- .mockImplementation(() => {});-- await prebuild({- assets: false,- template: ["defaults"],- });- expect(fetch).not.toHaveBeenCalled();+ test(+ "downloads assets only when requested by prebuild",+ async () => {+ const fetch = vi.fn(async () => ({+ ok: false,+ statusText: "Not Found",+ }));+ globalThis.fetch = fetch as unknown as typeof globalThis.fetch;+ const consoleWarn = vi+ .spyOn(console, "warn")+ .mockImplementation(() => {});+ const consoleError = vi+ .spyOn(console, "error")+ .mockImplementation(() => {});++ await prebuild({+ assets: false,+ template: ["defaults"],+ });+ expect(fetch).not.toHaveBeenCalled();- await prebuild({- assets: true,- template: ["defaults"],- });- expect(fetch).toHaveBeenCalledWith(- "https://assets.example/cgi/image/image.png?format=raw"- );- expect(consoleWarn).not.toHaveBeenCalled();- expect(consoleError).toHaveBeenCalledWith(- expect.stringContaining("Error materializing file image.png")- );- });+ await prebuild({+ assets: true,+ template: ["defaults"],+ });+ expect(fetch).toHaveBeenCalledWith(+ "https://assets.example/cgi/image/image.png?format=raw"+ );+ expect(consoleWarn).not.toHaveBeenCalled();+ expect(consoleError).toHaveBeenCalledWith(+ expect.stringContaining("Error materializing file image.png")+ );+ },+ slowPrebuildTestTimeout+ );test("uses synced asset files before downloading during prebuild", async () => {await mkdir(".webstudio/assets", { recursive: true });diff --git a/packages/design-system/src/components/scroll-area.tsx b/packages/design-system/src/components/scroll-area.tsxindex aabf74fa9f..9a1b1b696d 100644--- a/packages/design-system/src/components/scroll-area.tsx+++ b/packages/design-system/src/components/scroll-area.tsx@@ -71,6 +71,10 @@ type ScrollAreaProps = {className?: string;} & Pick<ComponentProps<"div">, "onScroll" | "children">;+/**+ * @deprecated Use ScrollAreaNative instead. Native scrolling avoids interaction+ * bugs from the custom Radix scroll area implementation.+ */export const ScrollArea = forwardRef(({