forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextMenuTrigger.js
More file actions
93 lines (76 loc) · 2.54 KB
/
Copy pathContextMenuTrigger.js
File metadata and controls
93 lines (76 loc) · 2.54 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
import React, { Component, PropTypes } from 'react';
import cx from 'classnames';
import assign from 'object-assign';
import { showMenu, hideMenu } from './actions';
import { callIfExists, cssClasses } from './helpers';
export default class ContextMenuTrigger extends Component {
static propTypes = {
id: PropTypes.string.isRequired,
attributes: PropTypes.object,
collect: PropTypes.func,
holdToDisplay: PropTypes.number,
renderTag: PropTypes.node
};
static defaultProps = {
attributes: {},
holdToDisplay: 1000,
renderTag: 'div'
};
handleMouseDown = (event) => {
if (this.props.holdToDisplay >= 0 && event.button === 0) {
event.persist();
this.mouseDownTimeoutId = setTimeout(
() => this.handleContextClick(event),
this.props.holdToDisplay
);
}
}
handleMouseUp = (event) => {
if (event.button === 0) {
clearTimeout(this.mouseDownTimeoutId);
}
}
handleTouchstart = (event) => {
if (this.props.holdToDisplay >= 0) {
event.persist();
this.touchstartTimeoutId = setTimeout(
() => this.handleContextClick(event),
this.props.holdToDisplay
);
}
}
handleTouchEnd = (event) => {
event.preventDefault();
clearTimeout(this.touchstartTimeoutId);
}
handleContextClick = (event) => {
event.preventDefault();
event.stopPropagation();
const x = event.clientX || (event.touches && event.touches[0].pageX);
const y = event.clientY || (event.touches && event.touches[0].pageY);
hideMenu();
showMenu({
position: {x, y},
target: this.elem,
id: this.props.id,
data: callIfExists(this.props.collect, this.props)
});
}
elemRef = (c) => {
this.elem = c;
}
render() {
const { renderTag, attributes, children } = this.props;
const newAttrs = assign({}, attributes, {
className: cx(cssClasses.menuWrapper, attributes.className),
onContextMenu: this.handleContextClick,
onMouseDown: this.handleMouseDown,
onMouseUp: this.handleMouseUp,
onTouchStart: this.handleTouchstart,
onTouchEnd: this.handleTouchEnd,
onMouseOut: this.handleMouseUp,
ref: this.elemRef
});
return React.createElement(renderTag, newAttrs, children);
}
}