Skip to content

Commit 3bc8eec

Browse files
refactor: use object refs instead of deprecated string refs (#1866)
1 parent 042e7db commit 3bc8eec

File tree

18 files changed

+118
-78
lines changed

18 files changed

+118
-78
lines changed

src/components/BooleanEditor/BooleanEditor.react.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class BooleanEditor extends React.Component {
2020

2121
this.checkExternalClick = this.checkExternalClick.bind(this);
2222
this.handleKey = this.handleKey.bind(this);
23+
this.inputRef = React.createRef();
2324
}
2425

2526
componentDidMount() {
@@ -33,7 +34,7 @@ export default class BooleanEditor extends React.Component {
3334
}
3435

3536
checkExternalClick(e) {
36-
if (!hasAncestor(e.target, this.refs.input)) {
37+
if (!hasAncestor(e.target, this.inputRef.current)) {
3738
this.props.onCommit(this.state.value);
3839
}
3940
}
@@ -46,7 +47,7 @@ export default class BooleanEditor extends React.Component {
4647

4748
render() {
4849
return (
49-
<div ref='input' style={{ minWidth: this.props.width }} className={styles.editor}>
50+
<div ref={this.inputRef} style={{ minWidth: this.props.width }} className={styles.editor}>
5051
<Toggle
5152
type={Toggle.Types.TRUE_FALSE}
5253
value={this.state.value}

src/components/CategoryList/CategoryList.react.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
*/
88
import PropTypes from 'lib/PropTypes';
99
import React from 'react';
10-
import ReactDOM from 'react-dom';
1110
import styles from 'components/CategoryList/CategoryList.scss';
1211
import { Link } from 'react-router-dom';
1312

1413
export default class CategoryList extends React.Component {
14+
constructor() {
15+
super();
16+
this.listWrapperRef = React.createRef();
17+
}
18+
1519
componentDidMount() {
16-
let listWrapper = ReactDOM.findDOMNode(this.refs.listWrapper);
20+
let listWrapper = this.listWrapperRef.current;
1721
if (listWrapper) {
1822
this.highlight = document.createElement('div');
1923
this.highlight.className = styles.highlight;
@@ -52,7 +56,7 @@ export default class CategoryList extends React.Component {
5256
return null;
5357
}
5458
return (
55-
<div ref='listWrapper' className={styles.class_list}>
59+
<div ref={this.listWrapperRef} className={styles.class_list}>
5660
{this.props.categories.map((c) => {
5761
let id = c.id || c.name;
5862
let count = c.count;

src/components/CodeSnippet/CodeSnippet.react.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import './CodeSnippet.css';
1313
import 'prismjs/plugins/line-numbers/prism-line-numbers';
1414

1515
export default class CodeSnippet extends React.Component {
16+
constructor() {
17+
super();
18+
this.codeRef = React.createRef();
19+
}
20+
1621
componentDidMount() {
1722
this._highlight();
1823
}
@@ -22,7 +27,7 @@ export default class CodeSnippet extends React.Component {
2227
}
2328

2429
_highlight() {
25-
Prism.highlightElement(this.refs.code);
30+
Prism.highlightElement(this.codeRef.current);
2631
}
2732

2833
render() {
@@ -34,7 +39,7 @@ export default class CodeSnippet extends React.Component {
3439
let pageStyle = fullPage ? { minHeight: 'calc(100vh - 96px)'} : {};
3540
return (
3641
<pre style={{ margin: 0, ...pageStyle}} className={classes.join(' ')}>
37-
<code style={pageStyle} ref='code'>{this.props.source}</code>
42+
<code style={pageStyle} ref={this.codeRef}>{this.props.source}</code>
3843
</pre>
3944
);
4045
}

src/components/DateTimeEditor/DateTimeEditor.react.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class DateTimeEditor extends React.Component {
2323

2424
this.checkExternalClick = this.checkExternalClick.bind(this);
2525
this.handleKey = this.handleKey.bind(this);
26+
this.inputRef = React.createRef();
27+
this.editorRef = React.createRef();
2628
}
2729

2830
componentWillReceiveProps(props) {
@@ -31,16 +33,16 @@ export default class DateTimeEditor extends React.Component {
3133

3234
componentDidMount() {
3335
document.body.addEventListener('click', this.checkExternalClick);
34-
this.refs.input.addEventListener('keypress', this.handleKey);
36+
this.inputRef.current.addEventListener('keypress', this.handleKey);
3537
}
3638

3739
componentWillUnmount() {
3840
document.body.removeEventListener('click', this.checkExternalClick);
39-
this.refs.input.removeEventListener('keypress', this.handleKey);
41+
this.inputRef.current.removeEventListener('keypress', this.handleKey);
4042
}
4143

4244
checkExternalClick(e) {
43-
if (!hasAncestor(e.target, this.refs.editor)) {
45+
if (!hasAncestor(e.target, this.editorRef.current)) {
4446
this.props.onCommit(this.state.value);
4547
}
4648
}
@@ -100,11 +102,11 @@ export default class DateTimeEditor extends React.Component {
100102
}
101103

102104
return (
103-
<div ref='editor' style={{ width: this.props.width }} className={styles.editor}>
105+
<div ref={this.editorRef} style={{ width: this.props.width }} className={styles.editor}>
104106
<input
105107
autoFocus
106108
type='text'
107-
ref='input'
109+
ref={this.inputRef}
108110
value={this.state.text}
109111
onFocus={e => e.target.select()}
110112
onClick={this.toggle.bind(this)}

src/components/FileEditor/FileEditor.react.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export default class FileEditor extends React.Component {
2121
this.checkExternalClick = this.checkExternalClick.bind(this);
2222
this.handleKey = this.handleKey.bind(this);
2323
this.removeFile = this.removeFile.bind(this);
24+
this.inputRef = React.createRef();
25+
this.fileInputRef = React.createRef();
2426
}
2527

2628
componentDidMount() {
@@ -31,15 +33,15 @@ export default class FileEditor extends React.Component {
3133
fileInputElement.click();
3234
}
3335
}
34-
36+
3537
componentWillUnmount() {
3638
document.body.removeEventListener('click', this.checkExternalClick);
3739
document.body.removeEventListener('keypress', this.handleKey);
3840
}
3941

4042
checkExternalClick(e) {
4143
const { onCancel } = this.props;
42-
if (!hasAncestor(e.target, this.refs.input) && onCancel) {
44+
if (!hasAncestor(e.target, this.inputRef.current) && onCancel) {
4345
onCancel();
4446
}
4547
}
@@ -61,7 +63,7 @@ export default class FileEditor extends React.Component {
6163
}
6264

6365
removeFile() {
64-
this.refs.fileInput.value = '';
66+
this.fileInputRef.current.value = '';
6567
this.props.onCommit(undefined);
6668
}
6769

@@ -76,9 +78,9 @@ export default class FileEditor extends React.Component {
7678
render() {
7779
const file = this.props.value;
7880
return (
79-
<div ref='input' style={{ minWidth: this.props.width, display: 'none' }} className={styles.editor}>
81+
<div ref={this.inputRef.current} style={{ minWidth: this.props.width, display: 'none' }} className={styles.editor}>
8082
<a className={styles.upload}>
81-
<input ref='fileInput' id='fileInput' type='file' onChange={this.handleChange.bind(this)} />
83+
<input ref={this.fileInputRef} id='fileInput' type='file' onChange={this.handleChange.bind(this)} />
8284
<span>{file ? 'Replace file' : 'Upload file'}</span>
8385
</a>
8486
</div>

src/components/GeoPointEditor/GeoPointEditor.react.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ export default class GeoPointEditor extends React.Component {
2222
this.checkExternalClick = this.checkExternalClick.bind(this);
2323
this.handleKeyLatitude = this.handleKeyLatitude.bind(this);
2424
this.handleKeyLongitude = this.handleKeyLongitude.bind(this);
25+
26+
this.latitudeRef = React.createRef();
27+
this.longitudeRef = React.createRef();
2528
}
2629

2730
componentDidMount() {
2831
if (!this.props.disableAutoFocus) {
29-
this.refs.latitude.focus();
32+
this.latitudeRef.current.focus();
3033
}
31-
this.refs.latitude.setSelectionRange(0, String(this.state.latitude).length);
32-
this.refs.latitude.addEventListener('keypress', this.handleKeyLatitude);
33-
this.refs.longitude.addEventListener('keypress', this.handleKeyLongitude);
34+
this.latitudeRef.current.setSelectionRange(0, String(this.state.latitude).length);
35+
this.latitudeRef.current.addEventListener('keypress', this.handleKeyLatitude);
36+
this.longitudeRef.current.addEventListener('keypress', this.handleKeyLongitude);
3437
}
3538

3639
componentWillReceiveProps(props) {
@@ -45,8 +48,8 @@ export default class GeoPointEditor extends React.Component {
4548
}
4649

4750
componentWillUnmount() {
48-
this.refs.latitude.removeEventListener('keypress', this.handleKeyLatitude);
49-
this.refs.longitude.removeEventListener('keypress', this.handleKeyLongitude);
51+
this.latitudeRef.current.removeEventListener('keypress', this.handleKeyLatitude);
52+
this.longitudeRef.current.removeEventListener('keypress', this.handleKeyLongitude);
5053
}
5154

5255
checkExternalClick() {
@@ -55,8 +58,8 @@ export default class GeoPointEditor extends React.Component {
5558
// check if activeElement is something else from input fields,
5659
// to avoid commiting new value on every switch of focus beetween latitude and longitude fields
5760
if (
58-
document.activeElement !== this.refs.latitude &&
59-
document.activeElement !== this.refs.longitude
61+
document.activeElement !== this.latitudeRef.current &&
62+
document.activeElement !== this.longitudeRef.current
6063
) {
6164
this.commitValue();
6265
}
@@ -65,8 +68,8 @@ export default class GeoPointEditor extends React.Component {
6568

6669
handleKeyLatitude(e) {
6770
if (e.keyCode === 13 || e.keyCode === 44) {
68-
this.refs.longitude.focus();
69-
this.refs.longitude.setSelectionRange(0, String(this.state.longitude).length);
71+
this.longitudeRef.current.focus();
72+
this.longitudeRef.current.setSelectionRange(0, String(this.state.longitude).length);
7073
}
7174
}
7275

@@ -112,15 +115,15 @@ export default class GeoPointEditor extends React.Component {
112115

113116
if (values[1].length <= 0 || !validateNumeric(values[1])) {
114117
this.setState({ latitude: values[0] });
115-
this.refs.longitude.focus();
116-
this.refs.longitude.setSelectionRange(0, String(this.state.longitude).length);
118+
this.longitudeRef.current.focus();
119+
this.longitudeRef.current.setSelectionRange(0, String(this.state.longitude).length);
117120
return;
118121
}
119122

120123
if (validateNumeric(values[1])) {
121124
this.setState({ latitude: values[0] });
122125
this.setState({ longitude: values[1] });
123-
this.refs.longitude.focus();
126+
this.longitudeRef.current.focus();
124127
return;
125128
}
126129
}
@@ -130,14 +133,14 @@ export default class GeoPointEditor extends React.Component {
130133
this.setState({ [target]: validateNumeric(value) ? value : this.state[target] });
131134
};
132135
return (
133-
<div ref='input' style={{ width: this.props.width, ...this.props.style }} className={styles.editor}>
136+
<div style={{ width: this.props.width, ...this.props.style }} className={styles.editor}>
134137
<input
135-
ref='latitude'
138+
ref={this.latitudeRef}
136139
value={this.state.latitude}
137140
onBlur={this.checkExternalClick}
138141
onChange={onChange.bind(this, 'latitude')} />
139142
<input
140-
ref='longitude'
143+
ref={this.longitudeRef}
141144
value={this.state.longitude}
142145
onBlur={this.checkExternalClick}
143146
onChange={onChange.bind(this, 'longitude')} />

src/components/Loader/Loader.react.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ function getPosition(t) {
5252
}
5353

5454
export default class Loader extends React.Component {
55+
constructor() {
56+
super();
57+
this.dot0Ref = React.createRef();
58+
this.dot1Ref = React.createRef();
59+
this.dot2Ref = React.createRef();
60+
}
61+
5562
componentDidMount() {
5663
this.mounted = true;
5764
this.mountTime = new Date().getTime();
@@ -69,21 +76,21 @@ export default class Loader extends React.Component {
6976
let delta = new Date() - this.mountTime;
7077
let t = (delta / DURATION) % 1;
7178
let pos = getPosition(t);
72-
let style = this.refs.dot0.style;
79+
let style = this.dot0Ref.current.style;
7380
style.left = pos.x + 'px';
7481
style.top = pos.y + 'px';
7582
style.width = style.height = getRadius(t) + 'px';
7683

7784
t = (delta / DURATION + 0.4) % 1;
7885
pos = getPosition(t);
79-
style = this.refs.dot1.style;
86+
style = this.dot1Ref.current.style;
8087
style.left = pos.x + 'px';
8188
style.top = pos.y + 'px';
8289
style.width = style.height = getRadius(t) + 'px';
8390

8491
t = (delta / DURATION + 0.8) % 1;
8592
pos = getPosition(t);
86-
style = this.refs.dot2.style;
93+
style = this.dot2Ref.current.style;
8794
style.left = pos.x + 'px';
8895
style.top = pos.y + 'px';
8996
style.width = style.height = getRadius(t) + 'px';
@@ -98,9 +105,9 @@ export default class Loader extends React.Component {
98105
}
99106
return (
100107
<div className={classes}>
101-
<div ref='dot0' />
102-
<div ref='dot1' />
103-
<div ref='dot2' />
108+
<div ref={this.dot0Ref} />
109+
<div ref={this.dot1Ref} />
110+
<div ref={this.dot2Ref} />
104111
</div>
105112
);
106113
}

src/components/LoginForm/LoginForm.react.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ import { verticalCenter } from 'stylesheets/base.scss';
1313

1414
// Class-style component, because we need refs
1515
export default class LoginForm extends React.Component {
16+
constructor() {
17+
super();
18+
this.formRef = React.createRef();
19+
}
1620
render() {
1721
return (
1822
<div className={styles.login} style={{ marginTop: this.props.marginTop || '-220px' }}>
1923
<Icon width={80} height={80} name='infinity' fill='#093A59' />
20-
<form method='post' ref='form' action={this.props.endpoint} className={styles.form}>
24+
<form method='post' ref={this.formRef} action={this.props.endpoint} className={styles.form}>
2125
<CSRFInput />
2226
<div className={styles.header}>{this.props.header}</div>
2327
{this.props.children}
@@ -34,7 +38,7 @@ export default class LoginForm extends React.Component {
3438
return;
3539
}
3640
this.props.formSubmit();
37-
this.refs.form.submit()
41+
this.formRef.current.submit()
3842
}}
3943
className={styles.submit}
4044
value={this.props.action} />

src/components/NumberEditor/NumberEditor.react.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ export default class NumberEditor extends React.Component {
1919

2020
this.checkExternalClick = this.checkExternalClick.bind(this);
2121
this.handleKey = this.handleKey.bind(this);
22+
this.inputRef = React.createRef();
2223
}
2324

2425
componentDidMount() {
25-
this.refs.input.setSelectionRange(0, String(this.state.value).length);
26+
this.inputRef.current.setSelectionRange(0, String(this.state.value).length);
2627
document.body.addEventListener('click', this.checkExternalClick);
2728
document.body.addEventListener('keypress', this.handleKey);
2829
}
@@ -33,7 +34,7 @@ export default class NumberEditor extends React.Component {
3334
}
3435

3536
checkExternalClick(e) {
36-
if (e.target !== this.refs.input) {
37+
if (e.target !== this.inputRef.current) {
3738
this.commitValue()
3839
}
3940
}
@@ -64,7 +65,7 @@ export default class NumberEditor extends React.Component {
6465
return (
6566
<div style={{ width: this.props.width }} className={styles.editor}>
6667
<input
67-
ref='input'
68+
ref={this.inputRef}
6869
value={this.state.value}
6970
onChange={onChange} />
7071
</div>

0 commit comments

Comments
 (0)