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

ReactRenderer for React.FunctionComponent with ref #1040

Closed
zofiag opened this issue Apr 9, 2021 · 5 comments
Closed

ReactRenderer for React.FunctionComponent with ref #1040

zofiag opened this issue Apr 9, 2021 · 5 comments
Labels
Type: Feature The issue or pullrequest is a new feature

Comments

@zofiag
Copy link

zofiag commented Apr 9, 2021

Is your feature request related to a problem? Please describe.
I couldn't find a way to run a method from the function component, which is wrapped into RectRenderer. From what I can see RectRenderer pass a ref as a prop only for class components

if (isClassComponent(Component)) {
  props.ref = (ref: React.Component) => {
    this.ref = ref
  }
}

Describe the solution you’d like
A way to be able to run methods from the function components wrapped into ReactRenderer.

Example:
For the Mention extension import Mention from "@tiptap/extension-mention"; I'd like to be able to configure it with my own MentionList component. MentionList is a function component with forwardRef and useImperativeHandle hook.

export const MentionList = forwardRef<HTMLDivElement, SuggestionProps>(
  ({ command, items }, ref) => {

    useImperativeHandle(ref, () => ({
      onKeyDown: ({ event }: { event: KeyboardEvent }) => {
        if (event.key === "ArrowUp") {
          upHandler();
          return true;
        }

        if (event.key === "ArrowDown") {
          downHandler();
          return true;
        }

        if (event.key === "Enter") {
          enterHandler();
          return true;
        }

        return false;
      },
    }));

...
});

then I'd expect to able to call reactRenderer.ref?.current.onKeyDown(props) in onKeyDown(props) of Mention.configure under suggestion key.

Please let me know if there's already a way to make it work with function components, that I'm not aware of! If not, would love to hear your thoughts on the suggested approach.

@philippkuehn
Copy link
Contributor

philippkuehn commented Apr 9, 2021

Hey, I have to admit that I have limited experience with React. I haven’t found a way to communicate from the suggestion extension with a function component, so currently it's only possible with a class component.

Maybe it’s an option to use a class component just to communicate with your functional component? SuggestionExtension -> SomeClassComponent -> MentionList

But I also have to admit that I don’t find the current solution optimal and I’m open to improvements!

@hanspagel hanspagel transferred this issue from ueberdosis/tiptap-next Apr 21, 2021
@hanspagel hanspagel added v2 Type: Feature The issue or pullrequest is a new feature labels Apr 21, 2021
@aizaiz
Copy link

aizaiz commented Jun 14, 2021

how do I fix this
create a custom ReactRenderer.tsx and remove this if condition and it should work with function component
if (isClassComponent(Component)) {

or maybe making an extension of ReactRenderer would be easier

@softmarshmallow
Copy link

Need better react support!

@philippkuehn
Copy link
Contributor

always happy for some react contributions!

@philippkuehn
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature The issue or pullrequest is a new feature
Projects
None yet
Development

No branches or pull requests

5 participants