-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragSort.js
More file actions
104 lines (84 loc) · 2.58 KB
/
Copy pathdragSort.js
File metadata and controls
104 lines (84 loc) · 2.58 KB
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import {
DragSource as dragSource,
DropTarget as dropTarget
} from 'react-dnd';
import classnames from 'classnames';
const itemSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index,
};
},
};
const itemTarget = {
hover(props, monitor, component) {
const moveFromIndex = monitor.getItem().index;
const moveToIndex = props.index;
if (moveFromIndex === moveToIndex) {
return;
}
const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
const clientOffset = monitor.getClientOffset();
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
if (moveFromIndex < moveToIndex && hoverClientY < hoverMiddleY) {
return;
}
if (moveFromIndex > moveToIndex && hoverClientY > hoverMiddleY) {
return;
}
props.moveListItem(moveFromIndex, moveToIndex);
monitor.getItem().index = moveToIndex;
},
};
const collectDrop = (connect) => ({
connectDropTarget: connect.dropTarget(),
});
const collectDrag = (connect, monitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging(),
connectDragPreview: connect.dragPreview(),
});
class DragSort extends Component {
static displayName = 'DragSort';
static propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDragPreview: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
isDragging: PropTypes.bool,
index: PropTypes.number.isRequired,
id: PropTypes.any.isRequired,
moveListItem: PropTypes.func.isRequired,
children: PropTypes.node,
isDragEnabled: PropTypes.bool,
};
render() {
if (!this.props.isDragEnabled) {
return <React.Fragment>{this.props.children}</React.Fragment>;
}
const {
isDragging,
connectDragSource,
connectDropTarget,
connectDragPreview,
} = this.props;
const opacity = isDragging ? 0 : 1;
return connectDragPreview(
connectDropTarget(
<div
className={ classnames('intellyo-drag-sort', { 'intellyo-drag-sort--is-dragging': this.props.isDragging }) }
style={ { opacity } }
>
{connectDragSource(<div className="drag-and-drop-handle" />)}
{ this.props.children }
</div>)
);
}
}
export default dropTarget('card', itemTarget, collectDrop)(
dragSource('card', itemSource, collectDrag)(DragSort)
);