Skip to content

Commit 938a40a

Browse files
committed
fix: hasSetRoot not work
1 parent 18758c8 commit 938a40a

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

packages/runtime/plugin-runtime/src/document/Html.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { type ReactElement } from 'react';
33
import { Body } from './Body';
44
import { DocumentStructureContext } from './DocumentStructureContext';
55
import { Head } from './Head';
6+
import { Root } from './Root';
67

78
/**
89
* get the directly son element by name
@@ -21,6 +22,31 @@ function findTargetChildByComponent(
2122
return children.find(item => item?.type === component);
2223
}
2324

25+
/**
26+
* get the children(grandChild included) with target component reference
27+
* @param component the component reference
28+
* @param children son element
29+
* @returns target element
30+
*/
31+
function findTargetElementByComponent(
32+
component: unknown,
33+
children: ReactElement<any>[],
34+
): ReactElement<{ children?: ReactElement<any> }> | null {
35+
if (children.length === 0) {
36+
return null;
37+
}
38+
let nextChildren: ReactElement<{ children: ReactElement<any> }>[] = [];
39+
for (const item of children) {
40+
if (item?.type === component) {
41+
return item;
42+
}
43+
if (item?.props?.children) {
44+
nextChildren = nextChildren.concat(item.props.children);
45+
}
46+
}
47+
return findTargetElementByComponent(component, nextChildren);
48+
}
49+
2450
/**
2551
* get the type of react element
2652
*/
@@ -70,7 +96,10 @@ export function Html(
7096
findTargetChildByComponent(Body, children) ||
7197
findTargetChildByName('Body', children),
7298
);
73-
const hasSetRoot = Boolean(findTargetElement('Root', children));
99+
const hasSetRoot = Boolean(
100+
findTargetElementByComponent(Root, children) ||
101+
findTargetChildByName('Root', children),
102+
);
74103
const hasSetTitle = Boolean(findTargetElement('title', children));
75104
const notMissMustChild = [
76105
hasSetHead,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import ReactDomServer from 'react-dom/server';
3+
import { Body, Head, Html, Root } from '../../src/document';
4+
5+
describe('debug Root component type', () => {
6+
it('should have correct type name', () => {
7+
const rootElement = <Root rootId="custom" />;
8+
9+
console.log('=== Root Element Type Info ===');
10+
console.log('type:', (rootElement as any).type);
11+
console.log('type.name:', (rootElement as any).type?.name);
12+
console.log('typeof type:', typeof (rootElement as any).type);
13+
});
14+
15+
it('should render document with Root', () => {
16+
const document = (
17+
<Html>
18+
<Head />
19+
<Body>
20+
<Root rootId="custom" />
21+
</Body>
22+
</Html>
23+
);
24+
25+
const docHtml = ReactDomServer.renderToString(document);
26+
27+
console.log('=== HTML OUTPUT ===');
28+
console.log(docHtml);
29+
30+
const hasCustomId = docHtml.includes('id="custom"');
31+
const hasRootId = docHtml.includes('id="root"');
32+
33+
console.log('Has custom id:', hasCustomId);
34+
console.log('Has root id:', hasRootId);
35+
36+
// If both are true, it means there are 2 Root divs (bug!)
37+
expect(hasCustomId && hasRootId).toBe(false);
38+
});
39+
});

0 commit comments

Comments
 (0)