Skip to content

Commit

Permalink
Forward React.forwardRef react nodes (markdoc#263)
Browse files Browse the repository at this point in the history
* Forward React.forwardRef react nodes

Closes markdoc#255

* duplicate tagName function
  • Loading branch information
mfix-stripe authored Oct 24, 2022
1 parent dc081c2 commit ea15b97
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
19 changes: 17 additions & 2 deletions src/renderers/react/react.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { tagName } from './shared';
import Tag from '../../tag';
import { RenderableTreeNodes, Scalar } from '../../types';
import type { createElement, Fragment, ReactNode } from 'react';

import type { createElement, ComponentType, Fragment, ReactNode } from 'react';

type ReactShape = Readonly<{
createElement: typeof createElement;
Fragment: typeof Fragment;
}>;

type Component = ComponentType<unknown>;

function tagName(
name: string,
components: Record<string, Component> | ((string: string) => Component)
): string | Component {
return typeof name !== 'string'
? name // This can be an object, e.g. when React.forwardRef is used
: name[0] !== name[0].toUpperCase()
? name
: components instanceof Function
? components(name)
: components[name];
}

export default function dynamic(
node: RenderableTreeNodes,
React: ReactShape,
Expand Down
16 changes: 0 additions & 16 deletions src/renderers/react/shared.ts

This file was deleted.

18 changes: 17 additions & 1 deletion src/renderers/react/static.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { tagName } from './shared';
import Tag from '../../tag';
import { RenderableTreeNode, RenderableTreeNodes } from '../../types';

import type { ComponentType } from 'react';

type Component = ComponentType<unknown>;

function tagName(
name: string,
components: Record<string, Component> | ((string: string) => Component)
): string | Component {
return typeof name !== 'string'
? 'Fragment'
: name[0] !== name[0].toUpperCase()
? name
: components instanceof Function
? components(name)
: components[name];
}

function renderArray(children: RenderableTreeNode[]): string {
return children.map(render).join(', ');
}
Expand Down

0 comments on commit ea15b97

Please sign in to comment.