feat: add support for forwardRef component in reactRenderer (#1690, fix #1040)

This commit is contained in:
castroCrea 2021-09-07 23:13:39 +02:00 committed by GitHub
parent e1b1905d6d
commit d1f2ad293f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
@ -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
@ -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
}