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

Cursor selector jump on start on each update of content if try to make component controlled #1422

Open
misha-kravtsov opened this issue Feb 2, 2024 · 2 comments

Comments

@misha-kravtsov
Copy link

misha-kravtsov commented Feb 2, 2024

`import { memo, useEffect, useState, FC } from 'react';
import { ContentState, convertToRaw, EditorState } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import './WysiwygEditor.scss';

// helper function
const createEditorState = (html: string) => {
const { contentBlocks, entityMap } = htmlToDraft(html);
return EditorState.createWithContent(ContentState.createFromBlockArray(contentBlocks, entityMap));
};

export interface WysiwygEditorProps {
value: string;
onValueChange: (value: string) => void;
fieldTags?: Array;
}

const WysiwygEditor: FC = memo(({ value, onValueChange, fieldTags }) => {
const [editorState, setEditorState] = useState(createEditorState(value));

const onEditorStateChange = (state: EditorState) => {
setEditorState(state);
onValueChange(draftToHtml(convertToRaw(state.getCurrentContent())));
};

useEffect(() => {
if (value === '') {
setEditorState(EditorState.createEmpty());
} else {
setEditorState(createEditorState(value));
}
}, [value]);

return (


<Editor
editorState={editorState}
// contentState={}
toolbarClassName='draft-toolbar'
wrapperClassName='draft-wrapper'
editorClassName='draft-editor'
toolbar={{
options: [
'inline',
'blockType',
'fontSize',
'list',
'textAlign',
'colorPicker',
'link',
'embedded',
'emoji',
'remove',
'history',
],
inline: {
options: ['bold', 'italic', 'underline', 'strikethrough'],
},
}}
onEditorStateChange={onEditorStateChange}
/>

);
});

export default WysiwygEditor;`

@misha-kravtsov
Copy link
Author

Same behaviour as on gif #652 (comment)

@misha-kravtsov
Copy link
Author

misha-kravtsov commented Feb 2, 2024

uncontrolled component works as well because in need to be sync once at the moment when it mounts, but for example if I want to change source (state), that store value? Only one proper solution that I found is change key when value from props change to re-sync component, could you add some interface that allow more easier mutate value in editor, I saw that there is state.getSelection() but it also wasn't work for me when I tried to change editor state with useEffect that reacts to new value from props

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant