Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Follow Ref on ForwardRef component in reactRenderer #1690

Merged
Merged
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
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