Skip to content

Commit 0ba919e

Browse files
committed
refactor dash table refs to createRef
1 parent 577651b commit 0ba919e

File tree

8 files changed

+146
-76
lines changed

8 files changed

+146
-76
lines changed

components/dash-table/src/core/components/IsolatedInput/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export default class IsolatedInput extends PureComponent<IProps, IState> {
9393

9494
return (
9595
<input
96-
ref='input'
9796
type='text'
9897
value={this.state.value || ''}
9998
onChange={this.handleChange}

components/dash-table/src/dash-table/components/Cell/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default class Cell extends Component<ICellProps> {
3131

3232
return (
3333
<td
34-
ref='td'
3534
children={(this as any).props.children}
3635
tabIndex={-1}
3736
className={className}

components/dash-table/src/dash-table/components/CellDropdown/index.tsx

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {ChangeEvent, PureComponent} from 'react';
1+
import React, {ChangeEvent, Component, createRef} from 'react';
22
import Dropdown from 'react-select';
33

44
import DOM from 'core/browser/DOM';
@@ -17,7 +17,12 @@ interface IProps {
1717
disabled?: boolean;
1818
}
1919

20-
export default class CellDropdown extends PureComponent<IProps> {
20+
export default class CellDropdown extends Component<IProps> {
21+
dropdownRef: React.RefObject<any>;
22+
constructor(props: IProps) {
23+
super(props);
24+
this.dropdownRef = createRef();
25+
}
2126
render() {
2227
const {clearable, dropdown, onChange, value, disabled} = this.props;
2328

@@ -37,7 +42,7 @@ export default class CellDropdown extends PureComponent<IProps> {
3742
}
3843
</div>
3944
<Dropdown
40-
ref='dropdown'
45+
ref={this.dropdownRef}
4146
clearable={clearable}
4247
onChange={(newValue: any) => {
4348
onChange(newValue ? newValue.value : newValue);
@@ -71,7 +76,7 @@ export default class CellDropdown extends PureComponent<IProps> {
7176
return;
7277
}
7378

74-
const dropdown = this.refs.dropdown as any;
79+
const dropdown = this.dropdownRef.current;
7580

7681
if (applyFocus && dropdown && document.activeElement !== dropdown) {
7782
// Limitation. If React >= 16 --> Use React.createRef instead to pass parent ref to child
@@ -83,8 +88,8 @@ export default class CellDropdown extends PureComponent<IProps> {
8388
}
8489

8590
private handleOpenDropdown = () => {
86-
const {dropdown}: {[key: string]: any} = this.refs;
87-
88-
dropdownHelper(dropdown.wrapper.querySelector('.Select-menu-outer'));
91+
dropdownHelper(
92+
this.dropdownRef.current.wrapper.querySelector('.Select-menu-outer')
93+
);
8994
};
9095
}

components/dash-table/src/dash-table/components/CellInput/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {
22
ChangeEvent,
33
ClipboardEvent,
4+
createRef,
45
KeyboardEvent,
56
MouseEvent,
67
PureComponent
@@ -25,12 +26,15 @@ interface ICellState {
2526
}
2627

2728
export default class CellInput extends PureComponent<ICellProps, ICellState> {
29+
textInputRef: React.RefObject<any>;
2830
constructor(props: ICellProps) {
2931
super(props);
3032

3133
this.state = {
3234
value: props.value
3335
};
36+
37+
this.textInputRef = createRef();
3438
}
3539

3640
render() {
@@ -46,7 +50,7 @@ export default class CellInput extends PureComponent<ICellProps, ICellState> {
4650
{value}
4751
</div>
4852
<input
49-
ref='textInput'
53+
ref={this.textInputRef}
5054
type='text'
5155
className={className}
5256
onBlur={this.propagateChange}
@@ -116,7 +120,7 @@ export default class CellInput extends PureComponent<ICellProps, ICellState> {
116120
return;
117121
}
118122

119-
const input = this.refs.textInput as HTMLInputElement;
123+
const input = this.textInputRef.current;
120124

121125
if (applyFocus && input && document.activeElement !== input) {
122126
input.focus();

components/dash-table/src/dash-table/components/CellLabel/index.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {PureComponent} from 'react';
1+
import React, {createRef, PureComponent} from 'react';
22

33
interface IProps {
44
active: boolean;
@@ -8,11 +8,16 @@ interface IProps {
88
}
99

1010
export default class CellLabel extends PureComponent<IProps> {
11+
elRef: React.RefObject<any>;
12+
constructor(props: IProps) {
13+
super(props);
14+
this.elRef = createRef();
15+
}
1116
render() {
1217
const {className, value} = this.props;
1318

1419
return (
15-
<div ref='el' className={className} tabIndex={-1}>
20+
<div ref={this.elRef} className={className} tabIndex={-1}>
1621
{typeof value === 'boolean' ? value.toString() : value}
1722
</div>
1823
);
@@ -32,7 +37,7 @@ export default class CellLabel extends PureComponent<IProps> {
3237
return;
3338
}
3439

35-
const el = this.refs.el as HTMLDivElement;
40+
const el = this.elRef.current;
3641

3742
if (applyFocus && el && document.activeElement !== el) {
3843
window.getSelection()?.selectAllChildren(el);

components/dash-table/src/dash-table/components/CellMarkdown/index.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {PureComponent} from 'react';
1+
import React, {createRef, PureComponent} from 'react';
22

33
import DOM from 'core/browser/DOM';
44
import {memoizeOne} from 'core/memoizer';
@@ -19,6 +19,7 @@ export default class CellMarkdown extends PureComponent<IProps, {}> {
1919
__html: md.render(String(value))
2020
}
2121
}));
22+
elRef: React.RefObject<any>;
2223

2324
constructor(props: IProps) {
2425
super(props);
@@ -28,6 +29,7 @@ export default class CellMarkdown extends PureComponent<IProps, {}> {
2829
this.setState({});
2930
});
3031
}
32+
this.elRef = createRef();
3133
}
3234

3335
componentDidUpdate() {
@@ -43,7 +45,7 @@ export default class CellMarkdown extends PureComponent<IProps, {}> {
4345

4446
return (
4547
<div
46-
ref='el'
48+
ref={this.elRef}
4749
className={[className, 'cell-markdown'].join(' ')}
4850
{...this.getMarkdown(value, markdown, Markdown.isReady)}
4951
/>
@@ -56,7 +58,7 @@ export default class CellMarkdown extends PureComponent<IProps, {}> {
5658
return;
5759
}
5860

59-
const el = this.refs.el as any;
61+
const el = this.elRef.current;
6062

6163
if (applyFocus && el && document.activeElement !== el) {
6264
// Limitation. If React >= 16 --> Use React.createRef instead to pass parent ref to child

0 commit comments

Comments
 (0)