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

Commit 1fae34f

Browse files
Dennis Wilkinsfacebook-github-bot
authored andcommitted
Convert some of draftjs' ReactDOM.findDOMNode to refs (#2051)
Summary: Pull Request resolved: #2051 ReactDOM.findDOMNode is deprecated and need to remove instances of it in draftjs, so React can be in strict mode. Reviewed By: bennyhobart Differential Revision: D14716519 fbshipit-source-id: b1bb4ab73f70a8b4e8429077a0b6b2c180724ee8
1 parent ffd8f59 commit 1fae34f

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

src/component/base/DraftEditor.react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
467467
): void => {
468468
const {editorState} = this.props;
469469
const alreadyHasFocus = editorState.getSelection().getHasFocus();
470-
const editorNode = ReactDOM.findDOMNode(this.editor);
470+
const editorNode = this.editor;
471471

472472
if (!editorNode) {
473473
// once in a while people call 'focus' in a setTimeout, and the node has
@@ -504,7 +504,7 @@ class DraftEditor extends React.Component<DraftEditorProps, State> {
504504
};
505505

506506
blur: () => void = (): void => {
507-
const editorNode = ReactDOM.findDOMNode(this.editor);
507+
const editorNode = this.editor;
508508
invariant(
509509
editorNode instanceof HTMLElement,
510510
'editorNode is not an HTMLElement',

src/component/contents/DraftEditorBlock.react.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ const isBlockOnSelectionEdge = (
7171
* appropriate decorator and inline style components.
7272
*/
7373
class DraftEditorBlock extends React.Component<Props> {
74+
_node: ?HTMLDivElement;
75+
7476
shouldComponentUpdate(nextProps: Props): boolean {
7577
return (
7678
this.props.block !== nextProps.block ||
@@ -100,7 +102,10 @@ class DraftEditorBlock extends React.Component<Props> {
100102
return;
101103
}
102104

103-
const blockNode = ReactDOM.findDOMNode(this);
105+
const blockNode = this._node;
106+
if (blockNode == null) {
107+
return;
108+
}
104109
const scrollParent = Style.getScrollParent(blockNode);
105110
const scrollPosition = getScrollPosition(scrollParent);
106111
let scrollDelta;
@@ -227,7 +232,10 @@ class DraftEditorBlock extends React.Component<Props> {
227232
});
228233

229234
return (
230-
<div data-offset-key={offsetKey} className={className}>
235+
<div
236+
data-offset-key={offsetKey}
237+
className={className}
238+
ref={ref => (this._node = ref)}>
231239
{this._renderChildren()}
232240
</div>
233241
);

src/component/contents/DraftEditorLeaf.react.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import type SelectionState from 'SelectionState';
1717

1818
const DraftEditorTextNode = require('DraftEditorTextNode.react');
1919
const React = require('React');
20-
const ReactDOM = require('ReactDOM');
2120

2221
const invariant = require('invariant');
2322
const setDraftEditorSelection = require('setDraftEditorSelection');
@@ -94,18 +93,15 @@ class DraftEditorLeaf extends React.Component<Props> {
9493
// Determine the appropriate target node for selection. If the child
9594
// is not a text node, it is a <br /> spacer. In this case, use the
9695
// <span> itself as the selection target.
97-
const node = ReactDOM.findDOMNode(this);
96+
const node = this.leaf;
9897
invariant(node, 'Missing node');
9998
const child = node.firstChild;
10099
invariant(child, 'Missing child');
101100
let targetNode;
102101

103102
if (child.nodeType === Node.TEXT_NODE) {
104103
targetNode = child;
105-
/* $FlowFixMe(>=0.79.1 site=www) This comment suppresses an error found
106-
* when Flow v0.79 was deployed. To see the error delete this comment and
107-
* run Flow. */
108-
} else if (child.tagName === 'BR') {
104+
} else if (child instanceof Element && child.tagName === 'BR') {
109105
targetNode = node;
110106
} else {
111107
targetNode = child.firstChild;
@@ -116,7 +112,7 @@ class DraftEditorLeaf extends React.Component<Props> {
116112
}
117113

118114
shouldComponentUpdate(nextProps: Props): boolean {
119-
const leafNode = ReactDOM.findDOMNode(this.leaf);
115+
const leafNode = this.leaf;
120116
invariant(leafNode, 'Missing leafNode');
121117
const shouldUpdate =
122118
leafNode.textContent !== nextProps.text ||

src/component/contents/DraftEditorTextNode.react.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
'use strict';
1313

1414
const React = require('React');
15-
const ReactDOM = require('ReactDOM');
1615
const UserAgent = require('UserAgent');
1716

1817
const invariant = require('invariant');
@@ -39,21 +38,23 @@ function isNewline(node: Element): boolean {
3938
* See http://jsfiddle.net/9khdavod/ for the failure case, and
4039
* http://jsfiddle.net/7pg143f7/ for the fixed case.
4140
*/
42-
const NEWLINE_A = useNewlineChar ? (
43-
<span key="A" data-text="true">
44-
{'\n'}
45-
</span>
46-
) : (
47-
<br key="A" data-text="true" />
48-
);
41+
const NEWLINE_A = ref =>
42+
useNewlineChar ? (
43+
<span key="A" data-text="true" ref={ref}>
44+
{'\n'}
45+
</span>
46+
) : (
47+
<br key="A" data-text="true" ref={ref} />
48+
);
4949

50-
const NEWLINE_B = useNewlineChar ? (
51-
<span key="B" data-text="true">
52-
{'\n'}
53-
</span>
54-
) : (
55-
<br key="B" data-text="true" />
56-
);
50+
const NEWLINE_B = ref =>
51+
useNewlineChar ? (
52+
<span key="B" data-text="true" ref={ref}>
53+
{'\n'}
54+
</span>
55+
) : (
56+
<br key="B" data-text="true" ref={ref} />
57+
);
5758

5859
type Props = {
5960
children: string,
@@ -68,6 +69,7 @@ type Props = {
6869
*/
6970
class DraftEditorTextNode extends React.Component<Props> {
7071
_forceFlag: boolean;
72+
_node: ?(HTMLSpanElement | HTMLBRElement);
7173

7274
constructor(props: Props) {
7375
super(props);
@@ -77,7 +79,7 @@ class DraftEditorTextNode extends React.Component<Props> {
7779
}
7880

7981
shouldComponentUpdate(nextProps: Props): boolean {
80-
const node = ReactDOM.findDOMNode(this);
82+
const node = this._node;
8183
const shouldBeNewline = nextProps.children === '';
8284
invariant(node instanceof Element, 'node is not an Element');
8385
if (shouldBeNewline) {
@@ -96,10 +98,15 @@ class DraftEditorTextNode extends React.Component<Props> {
9698

9799
render(): React.Node {
98100
if (this.props.children === '') {
99-
return this._forceFlag ? NEWLINE_A : NEWLINE_B;
101+
return this._forceFlag
102+
? NEWLINE_A(ref => (this._node = ref))
103+
: NEWLINE_B(ref => (this._node = ref));
100104
}
101105
return (
102-
<span key={this._forceFlag ? 'A' : 'B'} data-text="true">
106+
<span
107+
key={this._forceFlag ? 'A' : 'B'}
108+
data-text="true"
109+
ref={ref => (this._node = ref)}>
103110
{this.props.children}
104111
</span>
105112
);

0 commit comments

Comments
 (0)