-
Notifications
You must be signed in to change notification settings - Fork 43
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
The cursor jumps to the end of the content when using React Hook Form #91
Comments
Thanks for using I'm fairly certain what is causing this is the fact that I have the mui-tiptap/src/RichTextEditor.tsx Lines 105 to 125 in 73d26b4
But that above code means it doesn't work well when you're updating What you can do in the meantime before I release an update that fixes this problem: use Also, one separate general point/suggestion (not specific to mui-tiptap per se): I would not recommend calling |
@sjdemartini thanks for your answer and explanation, it really helps!
but we can achieve this behavior in the following way (I'm not sure that this is the best solution) useEffect(() => {
if (!editable) {
editor?.commands.setContent(value || '');
}
}, [value, editor, editable]); I know that serialization/deserialization is an expensive operation, we can use debounce or even better update |
This is correct. It still has to use Tiptap under the hood in order to properly render all of the content in whatever way you desire, which depends on what extensions are configured and how they're configured. (It's not merely rendering the raw serialized HTML directly, but rather is set up to accommodate the full capabilities of Tiptap's renderer, which can add interaction functionality with node views, JS-based logic, etc. even in read-only mode.) I can make the README more explicit about that. It (like
Right, this is what I was aiming to handle with // Only reset the content to the latest `value` if the user isn't focused on
// the editor or currently in editing mode. This tries to avoid losing any
// in-progress the user might be continuing to make. This approach isn't perfect; a better
// solution could involve "collaborative editing" (https://tiptap.dev/guide/collaborative-editing)
if (!editor.isFocused || !editable) {
// Preserve the previous editor cursor state when calling `setContent` (which
// otherwise would reset the selection/cursor)
const currentSelection = editor.state.selection;
editor
.chain()
.setContent(value)
.setTextSelection(currentSelection)
.run();
}
Yeah, this is tricky. Why is it that you say you will lose changes with that approach though? If you're saving the latest serialized content in For what it's worth, there's a mui-tiptap hook that may be helpful for you: mui-tiptap/src/hooks/useDebouncedFocus.ts Lines 12 to 21 in afd49d8
|
See discussion here #91 (comment) These components won't work without some extensions provided, so improve upon the typing we get from Tiptap and specify `extensions` as a required prop. Note that an empty array could still "pass" typing, but at least it's a hint that a real value is required, so shouldn't be as likely to run into a confusing error, like in #91 (comment).
See discussion here #91 (comment) These components won't work without some extensions provided, so improve upon the typing we get from Tiptap and specify `extensions` as a required prop. Note that an empty array could still "pass" typing, but at least it's a hint that a real value is required, so shouldn't be as likely to run into a confusing error, like in #91 (comment).
@sjdemartini you are right, saving changes on This is the final working example |
This is related to the discussion here #91, and should make the component more generally flexible, with the option for users to customize when/how they want to `setContent`.
This is related to the discussion here #91, and should make the component more generally flexible, with the option for users to customize when/how they want to `setContent`.
This is related to the discussion here #91, and should make the component more generally flexible, with the option for users to customize when/how they want to `setContent`.
This is related to the discussion here #91, and should make the component more generally flexible, with the option for users to customize when/how they want to `setContent`.
This is related to the discussion here #91, and should make the component more generally flexible, with the option for users to customize when/how they want to `setContent`.
Just as a heads up, I removed the automatic I've updated the README accordingly, and added some more suggestions to it based on this thread (e.g. about calling Thanks again for filing the issue and discussing! Glad you were able to get your react-hook-form setup working well! |
Describe the bug
When I try to use RichTextEditor with React Hook Form and change the form value via
onUpdate
prop with HTML format, the cursor jumps to the end of the content each time.To Reproduce
Steps to reproduce the behavior:
Expected behavior
The content should be updated, and the cursor should stay in the same position
Screenshots
Link to Reproduce
System (please complete the following information):
The text was updated successfully, but these errors were encountered: