Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/early-needles-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@coldwired/actions': patch
'@coldwired/react': patch
---

fix nested fragment render bug
9 changes: 7 additions & 2 deletions packages/actions/src/morph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function morphToDocumentFragment(

function morphToElement(fromElement: Element, toElement: Element, options?: MorphOptions): void {
const forceAttribute = options?.forceAttribute;
const added = new WeakSet<Element>();

morphdom(fromElement, toElement, {
childrenOnly: options?.childrenOnly,
Expand Down Expand Up @@ -162,9 +163,13 @@ function morphToElement(fromElement: Element, toElement: Element, options?: Morp
return true;
},
onNodeAdded(node) {
if (isElement(node)) {
options?.plugins?.forEach((plugin) => plugin.onCreateElement?.(node));
if (isElement(node) && node.parentElement) {
if (!added.has(node.parentElement)) {
options?.plugins?.forEach((plugin) => plugin.onCreateElement?.(node));
}
added.add(node);
}

return node;
},
});
Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/actions-react-plugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ describe('@coldwired/react', () => {
);
});

actions.update({
targets: '#main',
fragment: `<${DEFAULT_TAG_NAME} id="frag-1"><${REACT_COMPONENT_TAG} ${NAME_ATTRIBUTE}="Counter"></${REACT_COMPONENT_TAG}></${DEFAULT_TAG_NAME}><${DEFAULT_TAG_NAME} id="frag-2">Test</${DEFAULT_TAG_NAME}><div><${DEFAULT_TAG_NAME} id="frag-3">Encore un</${DEFAULT_TAG_NAME}></div>`,
});
await actions.ready();
expect(root.getCache().size).toEqual(3);
await waitFor(() => {
expect(document.body.innerHTML).toEqual(
layout(
`<${DEFAULT_TAG_NAME} id="frag-1"><div><p>Count: 2</p><button>Increment</button></div></${DEFAULT_TAG_NAME}><${DEFAULT_TAG_NAME} id="frag-2">Test</${DEFAULT_TAG_NAME}><div><${DEFAULT_TAG_NAME} id="frag-3">Encore un</${DEFAULT_TAG_NAME}></div>`,
),
);
});

actions.update({
targets: '#main',
fragment: `<${DEFAULT_TAG_NAME} id="frag-1"><${REACT_COMPONENT_TAG} ${NAME_ATTRIBUTE}="Counter"></${REACT_COMPONENT_TAG}></${DEFAULT_TAG_NAME}><${DEFAULT_TAG_NAME} id="frag-2">Test 23</${DEFAULT_TAG_NAME}>`,
Expand Down