add ref to reactrenderer

This commit is contained in:
Philipp Kühn
2021-03-15 00:22:17 +01:00
parent 8505a24995
commit 385ee1d1f4
2 changed files with 50 additions and 4 deletions

View File

@@ -142,6 +142,26 @@ const MentionList = (props) => {
) )
} }
class MentionList2 extends React.Component {
onKeyDown(props) {
console.log('onKeyDown', props)
}
render() {
return (
<div>
mentions
{this.props.items.map((item, index) => (
<div key={index}>
{item}
</div>
))}
</div>
)
}
}
export default () => { export default () => {
const editor = useEditor({ const editor = useEditor({
// onTransaction({ editor }) { // onTransaction({ editor }) {
@@ -186,7 +206,7 @@ export default () => {
return { return {
onStart: props => { onStart: props => {
reactRenderer = new ReactRenderer(MentionList, { reactRenderer = new ReactRenderer(MentionList2, {
props, props,
editor: props.editor, editor: props.editor,
}) })
@@ -209,8 +229,7 @@ export default () => {
}) })
}, },
onKeyDown(props) { onKeyDown(props) {
console.log('keydown', props) return reactRenderer.ref.onKeyDown(props)
// return reactRenderer.ref.onKeyDown(props)
}, },
onExit() { onExit() {
popup[0].destroy() popup[0].destroy()

View File

@@ -7,6 +7,24 @@ export interface ReactRendererOptions {
props?: { [key: string]: any }, props?: { [key: string]: any },
} }
function isFunctionalComponent(Component: any) {
return (
typeof Component === 'function' // can be various things
&& !(
Component.prototype // native arrows don't have prototypes
&& Component.prototype.isReactComponent // special property
)
);
}
function isClassComponent(Component: any) {
return !!(
typeof Component === 'function'
&& Component.prototype
&& Component.prototype.isReactComponent
);
}
export class ReactRenderer { export class ReactRenderer {
id: string id: string
@@ -20,6 +38,8 @@ export class ReactRenderer {
reactElement: React.ReactNode reactElement: React.ReactNode
ref: React.Component | null = null
constructor(component: any, { props = {}, editor }: ReactRendererOptions) { constructor(component: any, { props = {}, editor }: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString() this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component this.component = component
@@ -31,7 +51,14 @@ export class ReactRenderer {
} }
render() { render() {
this.reactElement = React.createElement(this.component, this.props) const Component = this.component
const props = this.props
if (isClassComponent(Component)) {
props.ref = (ref: React.Component) => this.ref = ref
}
this.reactElement = <Component {...props } />
if (this.editor?.contentComponent) { if (this.editor?.contentComponent) {
this.editor.contentComponent.setState({ this.editor.contentComponent.setState({