Skip to content

Commit a902d20

Browse files
authored
fix: replace findDOMNode with refs
jquense#2193
1 parent ffff7c0 commit a902d20

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

src/BackgroundCells.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import React, { createRef } from 'react'
12
import PropTypes from 'prop-types'
2-
import React from 'react'
3-
import { findDOMNode } from 'react-dom'
43
import clsx from 'clsx'
54

65
import { notify } from './utils/helpers'
@@ -14,6 +13,7 @@ class BackgroundCells extends React.Component {
1413
this.state = {
1514
selecting: false,
1615
}
16+
this.containerRef = createRef()
1717
}
1818

1919
componentDidMount() {
@@ -44,7 +44,7 @@ class BackgroundCells extends React.Component {
4444
let current = getNow()
4545

4646
return (
47-
<div className="rbc-row-bg">
47+
<div className="rbc-row-bg" ref={this.containerRef}>
4848
{range.map((date, index) => {
4949
let selected = selecting && index >= startIdx && index <= endIdx
5050
const { className, style } = getters.dayProp(date)
@@ -71,13 +71,13 @@ class BackgroundCells extends React.Component {
7171
}
7272

7373
_selectable() {
74-
let node = findDOMNode(this)
74+
let node = this.containerRef.current
7575
let selector = (this._selector = new Selection(this.props.container, {
7676
longPressThreshold: this.props.longPressThreshold,
7777
}))
7878

7979
let selectorClicksHandler = (point, actionType) => {
80-
if (!isEvent(findDOMNode(this), point)) {
80+
if (!isEvent(node, point)) {
8181
let rowBox = getBoundsForNode(node)
8282
let { range, rtl } = this.props
8383

@@ -128,7 +128,7 @@ class BackgroundCells extends React.Component {
128128
selector.on('beforeSelect', (box) => {
129129
if (this.props.selectable !== 'ignoreEvents') return
130130

131-
return !isEvent(findDOMNode(this), box)
131+
return !isEvent(this.containerRef.current, box)
132132
})
133133

134134
selector.on('click', (point) => selectorClicksHandler(point, 'click'))

src/DateContentRow.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import React, { createRef } from 'react'
12
import clsx from 'clsx'
23
import getHeight from 'dom-helpers/height'
34
import qsa from 'dom-helpers/querySelectorAll'
45
import PropTypes from 'prop-types'
5-
import React from 'react'
6-
import { findDOMNode } from 'react-dom'
76

87
import BackgroundCells from './BackgroundCells'
98
import EventRow from './EventRow'
@@ -16,6 +15,10 @@ class DateContentRow extends React.Component {
1615
constructor(...args) {
1716
super(...args)
1817

18+
this.containerRef = createRef()
19+
this.headingRowRef = createRef()
20+
this.eventRowRef = createRef()
21+
1922
this.slotMetrics = DateSlotMetrics.getSlotMetrics()
2023
}
2124

@@ -28,7 +31,7 @@ class DateContentRow extends React.Component {
2831
handleShowMore = (slot, target) => {
2932
const { range, onShowMore } = this.props
3033
let metrics = this.slotMetrics(this.props)
31-
let row = qsa(findDOMNode(this), '.rbc-row-bg')[0]
34+
let row = qsa(this.containerRef.current, '.rbc-row-bg')[0]
3235

3336
let cell
3437
if (row) cell = row.children[slot - 1]
@@ -37,23 +40,19 @@ class DateContentRow extends React.Component {
3740
onShowMore(events, range[slot - 1], cell, slot, target)
3841
}
3942

40-
createHeadingRef = (r) => {
41-
this.headingRow = r
42-
}
43-
44-
createEventRef = (r) => {
45-
this.eventRow = r
46-
}
47-
4843
getContainer = () => {
4944
const { container } = this.props
50-
return container ? container() : findDOMNode(this)
45+
return container ? container() : this.containerRef.current
5146
}
5247

5348
getRowLimit() {
54-
let eventHeight = getHeight(this.eventRow)
55-
let headingHeight = this.headingRow ? getHeight(this.headingRow) : 0
56-
let eventSpace = getHeight(findDOMNode(this)) - headingHeight
49+
/* Guessing this only gets called on the dummyRow */
50+
const eventHeight = getHeight(this.eventRowRef.current)
51+
const headingHeight =
52+
this.headingRowRef && this.headingRowRef.current
53+
? getHeight(this.headingRowRef.current)
54+
: 0
55+
const eventSpace = getHeight(this.containerRef.current) - headingHeight
5756

5857
return Math.max(Math.floor(eventSpace / eventHeight), 1)
5958
}
@@ -74,19 +73,19 @@ class DateContentRow extends React.Component {
7473
renderDummy = () => {
7574
let { className, range, renderHeader, showAllEvents } = this.props
7675
return (
77-
<div className={className}>
76+
<div className={className} ref={this.containerRef}>
7877
<div
7978
className={clsx(
8079
'rbc-row-content',
8180
showAllEvents && 'rbc-row-content-scrollable'
8281
)}
8382
>
8483
{renderHeader && (
85-
<div className="rbc-row" ref={this.createHeadingRef}>
84+
<div className="rbc-row" ref={this.headingRowRef}>
8685
{range.map(this.renderHeadingCell)}
8786
</div>
8887
)}
89-
<div className="rbc-row" ref={this.createEventRef}>
88+
<div className="rbc-row" ref={this.eventRowRef}>
9089
<div className="rbc-row-segment">
9190
<div className="rbc-event">
9291
<div className="rbc-event-content">&nbsp;</div>
@@ -152,7 +151,7 @@ class DateContentRow extends React.Component {
152151
}
153152

154153
return (
155-
<div className={className} role="rowgroup">
154+
<div className={className} role="rowgroup" ref={this.containerRef}>
156155
<BackgroundCells
157156
localizer={localizer}
158157
date={date}
@@ -178,7 +177,7 @@ class DateContentRow extends React.Component {
178177
role="row"
179178
>
180179
{renderHeader && (
181-
<div className="rbc-row " ref={this.createHeadingRef}>
180+
<div className="rbc-row " ref={this.headingRowRef}>
182181
{range.map(this.renderHeadingCell)}
183182
</div>
184183
)}

src/DayColumn.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import React, { createRef } from 'react'
12
import PropTypes from 'prop-types'
2-
import React from 'react'
3-
import { findDOMNode } from 'react-dom'
43
import clsx from 'clsx'
54

65
import Selection, { getBoundsForNode, isEvent } from './Selection'
@@ -23,6 +22,7 @@ class DayColumn extends React.Component {
2322
super(...args)
2423

2524
this.slotMetrics = TimeSlotUtils.getSlotMetrics(this.props)
25+
this.containerRef = createRef()
2626
}
2727

2828
componentDidMount() {
@@ -129,6 +129,7 @@ class DayColumn extends React.Component {
129129

130130
return (
131131
<DayColumnWrapperComponent
132+
ref={this.containerRef}
132133
date={date}
133134
style={style}
134135
className={clsx(
@@ -249,9 +250,9 @@ class DayColumn extends React.Component {
249250
}
250251

251252
_selectable = () => {
252-
let node = findDOMNode(this)
253+
let node = this.containerRef.current
253254
const { longPressThreshold, localizer } = this.props
254-
let selector = (this._selector = new Selection(() => findDOMNode(this), {
255+
let selector = (this._selector = new Selection(() => node, {
255256
longPressThreshold: longPressThreshold,
256257
}))
257258

@@ -311,7 +312,7 @@ class DayColumn extends React.Component {
311312
}
312313

313314
let selectorClicksHandler = (box, actionType) => {
314-
if (!isEvent(findDOMNode(this), box)) {
315+
if (!isEvent(this.containerRef.current, box)) {
315316
const { startDate, endDate } = selectionState(box)
316317
this._selectSlot({
317318
startDate,
@@ -329,7 +330,7 @@ class DayColumn extends React.Component {
329330
selector.on('beforeSelect', (box) => {
330331
if (this.props.selectable !== 'ignoreEvents') return
331332

332-
return !isEvent(findDOMNode(this), box)
333+
return !isEvent(this.containerRef.current, box)
333334
})
334335

335336
selector.on('click', (box) => selectorClicksHandler(box, 'click'))

src/DayColumnWrapper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React from 'react'
22

3-
const DayColumnWrapper = ({ children, className, style }) => {
3+
const DayColumnWrapper = ({ children, className, style, innerRef }) => {
44
return (
5-
<div className={className} style={style}>
5+
<div className={className} style={style} ref={innerRef}>
66
{children}
77
</div>
88
)
99
}
1010

11-
export default DayColumnWrapper
11+
export default React.forwardRef((props, ref) => (
12+
<DayColumnWrapper {...props} innerRef={ref} />
13+
))

src/Month.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import React, { createRef } from 'react'
12
import PropTypes from 'prop-types'
2-
import React from 'react'
3-
import { findDOMNode } from 'react-dom'
43
import clsx from 'clsx'
54

65
import chunk from 'lodash/chunk'
@@ -25,13 +24,15 @@ class MonthView extends React.Component {
2524
constructor(...args) {
2625
super(...args)
2726

28-
this._bgRows = []
29-
this._pendingSelection = []
30-
this.slotRowRef = React.createRef()
3127
this.state = {
3228
rowLimit: 5,
3329
needLimitMeasure: true,
3430
}
31+
this.containerRef = createRef()
32+
this.slotRowRef = createRef()
33+
34+
this._bgRows = []
35+
this._pendingSelection = []
3536
}
3637

3738
UNSAFE_componentWillReceiveProps({ date }) {
@@ -69,7 +70,7 @@ class MonthView extends React.Component {
6970
}
7071

7172
getContainer = () => {
72-
return findDOMNode(this)
73+
return this.containerRef.current
7374
}
7475

7576
render() {
@@ -84,6 +85,7 @@ class MonthView extends React.Component {
8485
className={clsx('rbc-month-view', className)}
8586
role="table"
8687
aria-label="Month View"
88+
ref={this.containerRef}
8789
>
8890
<div className="rbc-row rbc-month-header" role="row">
8991
{this.renderHeaders(weeks[0])}
@@ -284,7 +286,7 @@ class MonthView extends React.Component {
284286
this.clearSelection()
285287

286288
if (popup) {
287-
let position = getPosition(cell, findDOMNode(this))
289+
let position = getPosition(cell, this.containerRef.current)
288290

289291
this.setState({
290292
overlay: { date, events, position, target },

src/TimeGrid.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import React, { Component, createRef } from 'react'
12
import PropTypes from 'prop-types'
23
import clsx from 'clsx'
34
import * as animationFrame from 'dom-helpers/animationFrame'
4-
import React, { Component } from 'react'
5-
import { findDOMNode } from 'react-dom'
65
import memoize from 'memoize-one'
76

87
import DayColumn from './DayColumn'
@@ -24,6 +23,7 @@ export default class TimeGrid extends Component {
2423
this.scrollRef = React.createRef()
2524
this.contentRef = React.createRef()
2625
this._scrollRatio = null
26+
this.gutterRef = createRef()
2727
}
2828

2929
UNSAFE_componentWillMount() {
@@ -83,10 +83,6 @@ export default class TimeGrid extends Component {
8383
}
8484
}
8585

86-
gutterRef = (ref) => {
87-
this.gutter = ref && findDOMNode(ref)
88-
}
89-
9086
handleSelectAlldayEvent = (...args) => {
9187
//cancel any pending selections so only the event click goes through.
9288
this.clearSelection()

0 commit comments

Comments
 (0)