Skip to content

Replace findDOMNode calls with refs #835

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

Merged
merged 5 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 6 additions & 7 deletions src/components/RangeSelector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const React = require('react');
const ReactDOM = require('react-dom');
const PropTypes = require('prop-types');
const Radium = require('radium');
const _throttle = require('lodash/throttle');
Expand Down Expand Up @@ -56,6 +55,8 @@ class RangeSelector extends React.Component {
upperValue,
trackClicked: false
};

this.rangeSelectorRef = React.createRef();
}

componentDidMount () {
Expand All @@ -81,7 +82,7 @@ class RangeSelector extends React.Component {
};

_setDefaultRangeValues = () => {
const component = ReactDOM.findDOMNode(this.rangeSelectorRef);
const component = this.rangeSelectorRef.current;
const width = component ? component.offsetWidth : 0;

//convert our values to a 0-based scale
Expand Down Expand Up @@ -129,7 +130,7 @@ class RangeSelector extends React.Component {

_handleTrackMouseDown = (e) => {
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const newPixels = clientX - ReactDOM.findDOMNode(this.rangeSelectorRef).getBoundingClientRect().left;
const newPixels = clientX - this.rangeSelectorRef.current.getBoundingClientRect().left;
const updatedState = {
trackClicked: true
};
Expand Down Expand Up @@ -158,7 +159,7 @@ class RangeSelector extends React.Component {
selectedLabel: null
};

let newPixels = clientX - ReactDOM.findDOMNode(this.rangeSelectorRef).getBoundingClientRect().left;
let newPixels = clientX - this.rangeSelectorRef.current.getBoundingClientRect().left;

//make sure we don't go past the end of the track
newPixels = Math.min(newPixels, this.state.width);
Expand Down Expand Up @@ -257,9 +258,7 @@ class RangeSelector extends React.Component {
onMouseUp={this._handleDragEnd}
onTouchEnd={this._handleDragEnd}
onTouchMove={this._handleDragging}
ref={(ref) => {
this.rangeSelectorRef = ref;
}}
ref={this.rangeSelectorRef}
style={styles.range}
>
{this.props.presets.length ? (
Expand Down
9 changes: 5 additions & 4 deletions src/components/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const keycode = require('keycode');
const PropTypes = require('prop-types');
const Radium = require('radium');
const React = require('react');
const ReactDOM = require('react-dom');

import { css } from 'glamor'
import { withTheme } from './Theme';
Expand Down Expand Up @@ -63,6 +62,8 @@ class Select extends React.Component {
selected: props.selected,
searchTerm: "",
};

this.optionListRef = React.createRef();
}

componentWillReceiveProps (newProps) {
Expand Down Expand Up @@ -105,7 +106,7 @@ class Select extends React.Component {
};

_scrollListDown = (nextIndex) => {
const ul = ReactDOM.findDOMNode(this.optionList);
const ul = this.optionList.current;
const activeLi = ul.children[nextIndex];
const heightFromTop = nextIndex * activeLi.clientHeight;

Expand All @@ -115,7 +116,7 @@ class Select extends React.Component {
};

_scrollListUp = (prevIndex) => {
const ul = ReactDOM.findDOMNode(this.optionList);
const ul = this.optionList.current;
const activeLi = ul.children[prevIndex];
const heightFromBottom = (this.props.options.length - prevIndex) * activeLi.clientHeight;

Expand Down Expand Up @@ -155,7 +156,7 @@ class Select extends React.Component {
<Listbox
aria-label={this.props.placeholderText}
className='mx-select-options'
ref={(ref) => this.optionList = ref}
ref={this.optionListRef}
style={styles.options}
withSearch={this.props.withSearch}
>
Expand Down
14 changes: 8 additions & 6 deletions src/components/SimpleSlider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const React = require('react');
const ReactDOM = require('react-dom');
const PropTypes = require('prop-types');
const Radium = require('radium');
const _merge = require('lodash/merge');
Expand All @@ -24,14 +23,19 @@ class SimpleSlider extends React.Component {
disabled: false
};

constructor(props) {
super(props);
this.rangeSelectorRef = React.createRef();
}

state = {
dragging: false,
leftPixels: 0,
width: 0
};

componentDidMount () {
const component = ReactDOM.findDOMNode(this.rangeSelectorRef);
const component = this.rangeSelectorRef.current;
const width = component.clientWidth;
const leftPixels = this.props.percent * width;

Expand All @@ -58,7 +62,7 @@ class SimpleSlider extends React.Component {

_handleMouseEvents = (e) => {
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const leftSpace = ReactDOM.findDOMNode(this.rangeSelectorRef).getBoundingClientRect().left;
const leftSpace = this.rangeSelectorRef.current.getBoundingClientRect().left;
let currentPercent = (clientX - leftSpace) / this.state.width;

if (currentPercent < 0) {
Expand Down Expand Up @@ -101,9 +105,7 @@ class SimpleSlider extends React.Component {
onMouseUp={disabled ? null : this._handleDragEnd}
onTouchEnd={disabled ? null : this._handleDragEnd}
onTouchMove={disabled ? null : this._handleDragging}
ref={(ref) => {
this.rangeSelectorRef = ref;
}}
ref={this.rangeSelectorRef}
style={styles.range}
>
<div
Expand Down
10 changes: 7 additions & 3 deletions src/components/Spin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const PropTypes = require('prop-types');
const React = require('react');
const ReactDOM = require('react-dom');

class Spin extends React.Component {
static propTypes = {
Expand All @@ -14,8 +13,13 @@ class Spin extends React.Component {
speed: 1000
};

constructor(props) {
super(props);
this.spinRef = React.createRef();
}

componentDidMount() {
const el = ReactDOM.findDOMNode(this);
const el = this.spinRef.current
const speed = this.props.speed;
const spinDirection = this.props.direction === 'clockwise' ? 1 : -1;
let rotation = 0;
Expand All @@ -37,7 +41,7 @@ class Spin extends React.Component {

render() {
return (
<div className='mx-spin' style={{ display: 'inline-block' }}>
<div className='mx-spin' ref={this.spinRef} style={{ display: 'inline-block' }}>
{this.props.children}
</div>
);
Expand Down
21 changes: 13 additions & 8 deletions src/components/TypeAhead.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const React = require('react');
const ReactDOM = require('react-dom');
const PropTypes = require('prop-types');
const Radium = require('radium');

Expand Down Expand Up @@ -29,6 +28,12 @@ class TypeAhead extends React.Component {
preSelectedItems: []
};

constructor(props) {
super(props);
this.inputRef = React.createRef();
this.optionListRef = React.createRef();
}

state = {
highlightedValue: null,
isOpen: false,
Expand Down Expand Up @@ -56,7 +61,7 @@ class TypeAhead extends React.Component {
isOpen: true
});

ReactDOM.findDOMNode(this.input).focus();
this.inputRef.current.focus();
};

_handleItemMouseOver = () => {
Expand Down Expand Up @@ -99,7 +104,7 @@ class TypeAhead extends React.Component {
selectedItems
});

ReactDOM.findDOMNode(this.input).focus();
this.inputRef.current.focus();
};

_handleItemRemove = (item) => {
Expand All @@ -113,7 +118,7 @@ class TypeAhead extends React.Component {
selectedItems
});

ReactDOM.findDOMNode(this.input).focus();
this.inputRef.current.focus();
};

_handleInputKeyDown = (e) => {
Expand Down Expand Up @@ -188,13 +193,13 @@ class TypeAhead extends React.Component {
highlightedValue: null
});

ReactDOM.findDOMNode(this.input).blur();
this.inputRef.current.blur();
}
};

_scrollList = (nextIndex, scrollDirection) => {
const filteredItems = this._getFilteredItems();
const ul = ReactDOM.findDOMNode(this.optionList);
const ul = this.optionListRef.current;
const skipClearSelectAll = 2;
const activeLi = ul.children[nextIndex + skipClearSelectAll];

Expand Down Expand Up @@ -244,7 +249,7 @@ class TypeAhead extends React.Component {
return (
<div
className='mx-typeahead-option-list'
ref={(ref) => this.optionList = ref}
ref={this.optionListRef}
style={styles.itemList}
>
{this.state.selectedItems.length !== this.props.items.length ? (
Expand Down Expand Up @@ -313,7 +318,7 @@ class TypeAhead extends React.Component {
onChange={this._handleInputChange}
onKeyDown={this._handleInputKeyDown}
placeholder={!this.state.selectedItems.length ? this.props.placeholderText : null}
ref={(ref) => this.input = ref}
ref={this.inputRef}
style={styles.input}
type='text'
value={this.state.searchString}
Expand Down