Skip to content

Commit 24f92fb

Browse files
committed
fix: replace findDOMNode with refs (react 17)
also upg date-arithmetic dep & some minor cleanup
1 parent 6def209 commit 24f92fb

File tree

4 files changed

+125
-136
lines changed

4 files changed

+125
-136
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
"dependencies": {
131131
"@babel/runtime": "^7.1.5",
132132
"clsx": "^1.0.4",
133-
"date-arithmetic": "^4.0.1",
133+
"date-arithmetic": "^4.1.0",
134134
"dom-helpers": "^5.1.0",
135135
"invariant": "^2.2.4",
136136
"lodash": "^4.17.11",

src/addons/dragAndDrop/EventContainerWrapper.js

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import PropTypes from 'prop-types'
22
import React from 'react'
33
import * as dates from '../../utils/dates'
4-
import { findDOMNode } from 'react-dom'
54
import { DnDContext } from './DnDContext'
65

76
import Selection, {
87
getBoundsForNode,
98
getEventNodeFromPoint,
109
} from '../../Selection'
1110
import TimeGridEvent from '../../TimeGridEvent'
12-
import { dragAccessors } from './common'
11+
import { dragAccessors, eventTimes, pointInColumn } from './common'
1312
import NoopWrapper from '../../NoopWrapper'
1413

15-
const pointInColumn = (bounds, { x, y }) => {
16-
const { left, right, top } = bounds
17-
return x < right + 10 && x > left && y > top
18-
}
19-
const propTypes = {}
20-
2114
class EventContainerWrapper extends React.Component {
2215
static propTypes = {
2316
accessors: PropTypes.object.isRequired,
@@ -33,6 +26,7 @@ class EventContainerWrapper extends React.Component {
3326
constructor(...args) {
3427
super(...args)
3528
this.state = {}
29+
this.ref = React.createRef()
3630
}
3731

3832
componentDidMount() {
@@ -65,43 +59,31 @@ class EventContainerWrapper extends React.Component {
6559
})
6660
}
6761

68-
handleMove = (point, boundaryBox) => {
62+
handleMove = (point, bounds) => {
63+
if (!pointInColumn(bounds, point)) return this.reset()
6964
const { event } = this.context.draggable.dragAndDropAction
7065
const { accessors, slotMetrics } = this.props
7166

72-
if (!pointInColumn(boundaryBox, point)) {
73-
this.reset()
74-
return
75-
}
76-
77-
let currentSlot = slotMetrics.closestSlotFromPoint(
67+
const newSlot = slotMetrics.closestSlotFromPoint(
7868
{ y: point.y - this.eventOffsetTop, x: point.x },
79-
boundaryBox
69+
bounds
8070
)
8171

82-
let eventStart = accessors.start(event)
83-
let eventEnd = accessors.end(event)
84-
let end = dates.add(
85-
currentSlot,
86-
dates.diff(eventStart, eventEnd, 'minutes'),
87-
'minutes'
88-
)
89-
90-
this.update(event, slotMetrics.getRange(currentSlot, end, false, true))
72+
const { duration } = eventTimes(event, accessors)
73+
let newEnd = dates.add(newSlot, duration, 'milliseconds')
74+
this.update(event, slotMetrics.getRange(newSlot, newEnd, false, true))
9175
}
9276

93-
handleResize(point, boundaryBox) {
94-
let start, end
77+
handleResize(point, bounds) {
9578
const { accessors, slotMetrics } = this.props
9679
const { event, direction } = this.context.draggable.dragAndDropAction
80+
const newTime = slotMetrics.closestSlotFromPoint(point, bounds)
9781

98-
let currentSlot = slotMetrics.closestSlotFromPoint(point, boundaryBox)
82+
let { start, end } = eventTimes(event, accessors)
9983
if (direction === 'UP') {
100-
end = accessors.end(event)
101-
start = dates.min(currentSlot, slotMetrics.closestSlotFromDate(end, -1))
84+
start = dates.min(newTime, slotMetrics.closestSlotFromDate(end, -1))
10285
} else if (direction === 'DOWN') {
103-
start = accessors.start(event)
104-
end = dates.max(currentSlot, slotMetrics.closestSlotFromDate(start))
86+
end = dates.max(newTime, slotMetrics.closestSlotFromDate(start))
10587
}
10688

10789
this.update(event, slotMetrics.getRange(start, end))
@@ -124,10 +106,11 @@ class EventContainerWrapper extends React.Component {
124106
}
125107

126108
_selectable = () => {
127-
let node = findDOMNode(this)
109+
let wrapper = this.ref.current
110+
let node = wrapper.children[0]
128111
let isBeingDragged = false
129112
let selector = (this._selector = new Selection(() =>
130-
node.closest('.rbc-time-view')
113+
wrapper.closest('.rbc-time-view')
131114
))
132115

133116
selector.on('beforeSelect', point => {
@@ -141,6 +124,12 @@ class EventContainerWrapper extends React.Component {
141124
const eventNode = getEventNodeFromPoint(node, point)
142125
if (!eventNode) return false
143126

127+
// eventOffsetTop is distance from the top of the event to the initial
128+
// mouseDown position. We need this later to compute the new top of the
129+
// event during move operations, since the final location is really a
130+
// delta from this point. note: if we want to DRY this with WeekWrapper,
131+
// probably better just to capture the mouseDown point here and do the
132+
// placement computation in handleMove()...
144133
this.eventOffsetTop = point.y - getBoundsForNode(eventNode).top
145134
})
146135

@@ -154,19 +143,14 @@ class EventContainerWrapper extends React.Component {
154143

155144
selector.on('dropFromOutside', point => {
156145
if (!this.context.draggable.onDropFromOutside) return
157-
158146
const bounds = getBoundsForNode(node)
159-
160147
if (!pointInColumn(bounds, point)) return
161-
162148
this.handleDropFromOutside(point, bounds)
163149
})
164150

165151
selector.on('dragOver', point => {
166152
if (!this.context.draggable.dragFromOutsideItem) return
167-
168153
const bounds = getBoundsForNode(node)
169-
170154
this.handleDropFromOutside(point, bounds)
171155
})
172156

@@ -212,7 +196,7 @@ class EventContainerWrapper extends React.Component {
212196
this._selector = null
213197
}
214198

215-
render() {
199+
renderContent() {
216200
const {
217201
children,
218202
accessors,
@@ -223,7 +207,6 @@ class EventContainerWrapper extends React.Component {
223207
} = this.props
224208

225209
let { event, top, height } = this.state
226-
227210
if (!event) return children
228211

229212
const events = children.props.children
@@ -263,8 +246,10 @@ class EventContainerWrapper extends React.Component {
263246
),
264247
})
265248
}
266-
}
267249

268-
EventContainerWrapper.propTypes = propTypes
250+
render() {
251+
return <div ref={this.ref}>{this.renderContent()}</div>
252+
}
253+
}
269254

270255
export default EventContainerWrapper

0 commit comments

Comments
 (0)