Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Support React v16 when editor is focused after removed from DOM #1409

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,19 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
const alreadyHasFocus = editorState.getSelection().getHasFocus();
const editorNode = ReactDOM.findDOMNode(this.refs.editor);

if (!(editorNode instanceof HTMLElement)) {
if (!editorNode) {
// once in a while people call 'focus' in a setTimeout, and the node has
// been deleted, so it can be null in that case.
return;
}

const scrollParent = Style.getScrollParent(editorNode);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct me if I'm wrong, but Style.getScrollParent has 1 param & that is node, so we don't want to call it unless we have an HTMLElement, right? It doesn't matter either way (code below), but if we were writing a unit test for this, then it'd be REALLY hard to reach that invariant...

getScrollParent: function getScrollParent(node) {
	    if (!node) {
	      return null;
	    }
            ...

const {x, y} = scrollPosition || getScrollPosition(scrollParent);

invariant(
editorNode instanceof HTMLElement,
'editorNode is not an HTMLElement',
);
editorNode.focus();

// Restore scroll position
Expand Down