fix: Improve ReactRenderer types (#2011)

This commit is contained in:
Ricardo Amaral 2021-10-11 19:37:35 +01:00 committed by GitHub
parent 503a3f2cf5
commit 31d8ab3593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,12 +23,12 @@ export interface ReactRendererOptions {
as?: string,
}
type ComponentType =
type ComponentType<R> =
| React.ComponentClass
| React.FunctionComponent
| React.ForwardRefExoticComponent<{ items: any[], command: any } & React.RefAttributes<unknown>>
| React.ForwardRefExoticComponent<{ items: any[], command: any } & React.RefAttributes<R>>
export class ReactRenderer {
export class ReactRenderer<R = unknown> {
id: string
editor: ExtendedEditor
@ -41,9 +41,9 @@ export class ReactRenderer {
reactElement: React.ReactNode
ref: React.Component | null = null
ref: R | null = null
constructor(component: ComponentType, { editor, props = {}, as = 'div' }: ReactRendererOptions) {
constructor(component: ComponentType<R>, { editor, props = {}, as = 'div' }: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component
this.editor = editor as ExtendedEditor
@ -58,7 +58,7 @@ export class ReactRenderer {
const props = this.props
if (isClassComponent(Component) || isForwardRefComponent(Component)) {
props.ref = (ref: React.Component) => {
props.ref = (ref: R) => {
this.ref = ref
}
}