-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
withDragAndDrop.js
134 lines (110 loc) · 3.72 KB
/
withDragAndDrop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import PropTypes from 'prop-types'
import React from 'react'
import clsx from 'clsx'
import { accessor } from '../../utils/propTypes'
import EventWrapper from './EventWrapper'
import EventContainerWrapper from './EventContainerWrapper'
import WeekWrapper from './WeekWrapper'
import { mergeComponents } from './common'
import { DnDContext } from './DnDContext'
export default function withDragAndDrop(Calendar) {
class DragAndDropCalendar extends React.Component {
static propTypes = {
...Calendar.propTypes,
onEventDrop: PropTypes.func,
onEventResize: PropTypes.func,
onDragStart: PropTypes.func,
onDragOver: PropTypes.func,
onDropFromOutside: PropTypes.func,
dragFromOutsideItem: PropTypes.func,
draggableAccessor: accessor,
resizableAccessor: accessor,
selectable: PropTypes.oneOf([true, false, 'ignoreEvents']),
resizable: PropTypes.bool,
}
static defaultProps = {
...Calendar.defaultProps,
draggableAccessor: null,
resizableAccessor: null,
resizable: true,
}
constructor(...args) {
super(...args)
this.state = { interacting: false }
}
getDnDContextValue() {
return {
draggable: {
onStart: this.handleInteractionStart,
onEnd: this.handleInteractionEnd,
onBeginAction: this.handleBeginAction,
onDropFromOutside: this.props.onDropFromOutside,
dragFromOutsideItem: this.props.dragFromOutsideItem,
draggableAccessor: this.props.draggableAccessor,
resizableAccessor: this.props.resizableAccessor,
dragAndDropAction: this.state,
},
}
}
defaultOnDragOver = (event) => {
event.preventDefault()
}
handleBeginAction = (event, action, direction) => {
this.setState({ event, action, direction })
const { onDragStart } = this.props
if (onDragStart) onDragStart({ event, action, direction })
}
handleInteractionStart = () => {
if (this.state.interacting === false) this.setState({ interacting: true })
}
handleInteractionEnd = (interactionInfo) => {
const { action, event } = this.state
if (!action) return
this.setState({
action: null,
event: null,
interacting: false,
direction: null,
})
if (interactionInfo == null) return
interactionInfo.event = event
const { onEventDrop, onEventResize } = this.props
if (action === 'move' && onEventDrop) onEventDrop(interactionInfo)
if (action === 'resize' && onEventResize) onEventResize(interactionInfo)
}
render() {
const { selectable, elementProps, components, ...props } = this.props
const { interacting } = this.state
delete props.onEventDrop
delete props.onEventResize
props.selectable = selectable ? 'ignoreEvents' : false
this.components = mergeComponents(components, {
eventWrapper: EventWrapper,
eventContainerWrapper: EventContainerWrapper,
weekWrapper: WeekWrapper,
})
const elementPropsWithDropFromOutside = this.props.onDropFromOutside
? {
...elementProps,
onDragOver: this.props.onDragOver || this.defaultOnDragOver,
}
: elementProps
props.className = clsx(
props.className,
'rbc-addons-dnd',
!!interacting && 'rbc-addons-dnd-is-dragging'
)
const context = this.getDnDContextValue()
return (
<DnDContext.Provider value={context}>
<Calendar
{...props}
elementProps={elementPropsWithDropFromOutside}
components={this.components}
/>
</DnDContext.Provider>
)
}
}
return DragAndDropCalendar
}