Skip to content

Commit ea15b97

Browse files
authored
Forward React.forwardRef react nodes (markdoc#263)
* Forward React.forwardRef react nodes Closes markdoc#255 * duplicate tagName function
1 parent dc081c2 commit ea15b97

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/renderers/react/react.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import { tagName } from './shared';
21
import Tag from '../../tag';
32
import { RenderableTreeNodes, Scalar } from '../../types';
4-
import type { createElement, Fragment, ReactNode } from 'react';
3+
4+
import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
55

66
type ReactShape = Readonly<{
77
createElement: typeof createElement;
88
Fragment: typeof Fragment;
99
}>;
1010

11+
type Component = ComponentType<unknown>;
12+
13+
function tagName(
14+
name: string,
15+
components: Record<string, Component> | ((string: string) => Component)
16+
): string | Component {
17+
return typeof name !== 'string'
18+
? name // This can be an object, e.g. when React.forwardRef is used
19+
: name[0] !== name[0].toUpperCase()
20+
? name
21+
: components instanceof Function
22+
? components(name)
23+
: components[name];
24+
}
25+
1126
export default function dynamic(
1227
node: RenderableTreeNodes,
1328
React: ReactShape,

src/renderers/react/shared.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/renderers/react/static.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
import { tagName } from './shared';
21
import Tag from '../../tag';
32
import { RenderableTreeNode, RenderableTreeNodes } from '../../types';
43

4+
import type { ComponentType } from 'react';
5+
6+
type Component = ComponentType<unknown>;
7+
8+
function tagName(
9+
name: string,
10+
components: Record<string, Component> | ((string: string) => Component)
11+
): string | Component {
12+
return typeof name !== 'string'
13+
? 'Fragment'
14+
: name[0] !== name[0].toUpperCase()
15+
? name
16+
: components instanceof Function
17+
? components(name)
18+
: components[name];
19+
}
20+
521
function renderArray(children: RenderableTreeNode[]): string {
622
return children.map(render).join(', ');
723
}

0 commit comments

Comments
 (0)