forked from adazzle/react-data-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraggable.js
75 lines (60 loc) · 1.5 KB
/
Draggable.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
const React = require('react');
const PropTypes = React.PropTypes;
const Draggable = React.createClass({
propTypes: {
onDragStart: PropTypes.func,
onDragEnd: PropTypes.func,
onDrag: PropTypes.func,
component: PropTypes.oneOfType([PropTypes.func, PropTypes.constructor])
},
getDefaultProps() {
return {
onDragStart: () => true,
onDragEnd: () => {},
onDrag: () => {}
};
},
getInitialState(): {drag: ?any} {
return {
drag: null
};
},
componentWillUnmount() {
this.cleanUp();
},
onMouseDown(e: SyntheticMouseEvent) {
let drag = this.props.onDragStart(e);
if (drag === null && e.button !== 0) {
return;
}
window.addEventListener('mouseup', this.onMouseUp);
window.addEventListener('mousemove', this.onMouseMove);
this.setState({drag});
},
onMouseMove(e: SyntheticEvent) {
if (this.state.drag === null) {
return;
}
if (e.preventDefault) {
e.preventDefault();
}
this.props.onDrag(e);
},
onMouseUp(e: SyntheticEvent) {
this.cleanUp();
this.props.onDragEnd(e, this.state.drag);
this.setState({drag: null});
},
cleanUp() {
window.removeEventListener('mouseup', this.onMouseUp);
window.removeEventListener('mousemove', this.onMouseMove);
},
render(): ?ReactElement {
return (
<div {...this.props}
onMouseDown={this.onMouseDown}
className="react-grid-HeaderCell__draggable" />
);
}
});
module.exports = Draggable;