Skip to content

Commit

Permalink
feat: add support for forwardRef component in reactRenderer (#1690, fix
Browse files Browse the repository at this point in the history
  • Loading branch information
castroCrea authored Sep 7, 2021
1 parent e1b1905 commit d1f2ad2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/react/src/ReactRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ function isClassComponent(Component: any) {
)
}

function isForwardRefComponent(Component: any) {
return !!(typeof Component === 'object' && Component.$$typeof?.toString() === 'Symbol(react.forward_ref)')
}

export interface ReactRendererOptions {
editor: Editor,
props?: Record<string, any>,
as?: string,
}

type ComponentType = React.Component | React.FunctionComponent | React.ForwardRefExoticComponent<{
items: any[];
command: any;
} & React.RefAttributes<unknown>>

export class ReactRenderer {
id: string

Expand All @@ -30,7 +39,7 @@ export class ReactRenderer {

ref: React.Component | null = null

constructor(component: React.Component | React.FunctionComponent, { editor, props = {}, as = 'div' }: ReactRendererOptions) {
constructor(component: ComponentType, { editor, props = {}, as = 'div' }: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component
this.editor = editor
Expand All @@ -44,7 +53,7 @@ export class ReactRenderer {
const Component = this.component
const props = this.props

if (isClassComponent(Component)) {
if (isClassComponent(Component) || isForwardRefComponent(Component)) {
props.ref = (ref: React.Component) => {
this.ref = ref
}
Expand Down

0 comments on commit d1f2ad2

Please sign in to comment.