Skip to content

Commit 0ad843e

Browse files
committed
fix: support adding multiple nested components in the component browser
1 parent 7b3ab9b commit 0ad843e

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

javascript-modules/browser/lib/app/svelte/lib/helpers-key.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,35 @@ test("preview data overrides component array references", t => {
131131
});
132132
});
133133

134+
test("preview data can reference nested components", t => {
135+
const components = {
136+
"components/button/button.bookshop.json": JSON.stringify({
137+
blueprint: {
138+
text: "Hello World"
139+
}
140+
}),
141+
"components/hero/hero.bookshop.json": JSON.stringify({
142+
blueprint: {
143+
title: "Hello World",
144+
buttons: ["bookshop:button"]
145+
},
146+
preview: {
147+
buttons: ["bookshop:button", "bookshop:button"]
148+
}
149+
})
150+
};
151+
const hydrated = hydrateComponents(components, engineMock);
152+
t.deepEqual(hydrated.hero.props, {
153+
_bookshop_name: "hero",
154+
title: "Hello World",
155+
buttons: [{
156+
_bookshop_name: "button",
157+
text: "Hello World"
158+
}, {
159+
_bookshop_name: "button",
160+
text: "Hello World"
161+
}]
162+
});
163+
});
164+
134165
// TODO: Re-unit test the browser for 3.0 conventions.

javascript-modules/browser/lib/app/svelte/lib/helpers.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,26 @@ export const hydrateComponents = (components, engines, exclude = []) => {
137137
const findAndReplaceNested = (obj, structures, limitRecursion) => {
138138
if (!obj) return obj;
139139
if (Array.isArray(obj)) {
140-
if (typeof obj[0] === "string" && /^bookshop:structure:./.test(obj[0])) {
141-
const structureKey = obj[0].replace(/^bookshop:structure:/, '');
142-
return [randFrom(structures[structureKey], limitRecursion)];
143-
}
144-
if (typeof obj[0] === "string" && /^bookshop:./.test(obj[0])) {
145-
const structureKey = `component:${obj[0].replace(/^bookshop:/, '')}`;
146-
return [randFrom(structures[structureKey], limitRecursion)];
140+
for (let i = 0; i < obj.length; i++) {
141+
if (typeof obj[i] === "string" && /^bookshop:structure:./.test(obj[i])) {
142+
const structureKey = obj[i].replace(/^bookshop:structure:|!$/g, '');
143+
obj[i] = randFrom(structures[structureKey], limitRecursion);
144+
} else if (typeof obj[i] === "string" && /^bookshop:./.test(obj[i])) {
145+
const structureKey = `component:${obj[i].replace(/^bookshop:|!$/g, '')}`;
146+
obj[i] = randFrom(structures[structureKey], limitRecursion);
147+
} else {
148+
obj[i] = findAndReplaceNested(obj[i], structures, limitRecursion);
149+
}
147150
}
148-
return obj.map(o => findAndReplaceNested(o, structures, limitRecursion));
151+
return obj;
149152
}
150153
if (typeof obj === "object") {
151154
Object.entries(obj).forEach(([key, val]) => {
152155
if (typeof val === "string" && /^bookshop:structure:./.test(val)) {
153-
const structureKey = val.replace(/^bookshop:structure:/, '');
156+
const structureKey = val.replace(/^bookshop:structure:|!$/g, '');
154157
obj[key] = randFrom(structures[structureKey], limitRecursion);
155158
} else if (typeof val === "string" && /^bookshop:./.test(val)) {
156-
const structureKey = `component:${val.replace(/^bookshop:/, '')}`;
159+
const structureKey = `component:${val.replace(/^bookshop:|!$/g, '')}`;
157160
obj[key] = randFrom(structures[structureKey], limitRecursion);
158161
} else {
159162
obj[key] = findAndReplaceNested(obj[key], structures, limitRecursion);

0 commit comments

Comments
 (0)