forked from vkbansal/react-contextmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMenu.js
More file actions
195 lines (164 loc) · 7.48 KB
/
Copy pathAbstractMenu.js
File metadata and controls
195 lines (164 loc) · 7.48 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _MenuItem = require('./MenuItem');
var _MenuItem2 = _interopRequireDefault(_MenuItem);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var AbstractMenu = function (_Component) {
_inherits(AbstractMenu, _Component);
function AbstractMenu(props) {
_classCallCheck(this, AbstractMenu);
var _this = _possibleConstructorReturn(this, (AbstractMenu.__proto__ || Object.getPrototypeOf(AbstractMenu)).call(this, props));
_initialiseProps.call(_this);
_this.seletedItemRef = null;
_this.state = {
selectedItem: null,
forceSubMenuOpen: false
};
return _this;
}
return AbstractMenu;
}(_react.Component);
AbstractMenu.propTypes = {
children: _propTypes2.default.node.isRequired
};
var _initialiseProps = function _initialiseProps() {
var _this2 = this;
this.handleKeyNavigation = function (e) {
// check for isVisible strictly here as it might be undefined when this code executes in the context of SubMenu
// but we only need to check when it runs in the ContextMenu context
if (_this2.state.isVisible === false) {
return;
}
switch (e.keyCode) {
case 37: // left arrow
case 27:
// escape
e.preventDefault();
_this2.hideMenu(e);
break;
case 38:
// up arrow
e.preventDefault();
_this2.selectChildren(true);
break;
case 40:
// down arrow
e.preventDefault();
_this2.selectChildren(false);
break;
case 39:
// right arrow
_this2.tryToOpenSubMenu(e);
break;
case 13:
// enter
e.preventDefault();
_this2.tryToOpenSubMenu(e);
{
// determine the selected item is disabled or not
var disabled = _this2.seletedItemRef && _this2.seletedItemRef.props && _this2.seletedItemRef.props.disabled;
if (_this2.seletedItemRef && _this2.seletedItemRef.ref instanceof HTMLElement && !disabled) {
_this2.seletedItemRef.ref.click();
} else {
_this2.hideMenu(e);
}
}
break;
default:
// do nothing
}
};
this.handleForceClose = function () {
_this2.setState({ forceSubMenuOpen: false });
};
this.tryToOpenSubMenu = function (e) {
if (_this2.state.selectedItem && _this2.state.selectedItem.type === _this2.getSubMenuType()) {
e.preventDefault();
_this2.setState({ forceSubMenuOpen: true });
}
};
this.selectChildren = function (forward) {
var selectedItem = _this2.state.selectedItem;
var children = [];
var childCollector = function childCollector(child) {
// child can be empty in case you do conditional rendering of components, in which
// case it should not be accounted for as a real child
if (!child) {
return;
}
if ([_MenuItem2.default, _this2.getSubMenuType()].indexOf(child.type) < 0) {
// Maybe the MenuItem or SubMenu is capsuled in a wrapper div or something else
_react2.default.Children.forEach(child.props.children, childCollector);
} else if (!child.props.divider) {
children.push(child);
}
};
_react2.default.Children.forEach(_this2.props.children, childCollector);
var currentIndex = children.indexOf(selectedItem);
if (currentIndex < 0) {
_this2.setState({
selectedItem: forward ? children[children.length - 1] : children[0],
forceSubMenuOpen: false
});
} else if (forward) {
_this2.setState({
selectedItem: children[currentIndex - 1 < 0 ? children.length - 1 : currentIndex - 1],
forceSubMenuOpen: false
});
} else {
_this2.setState({
selectedItem: children[currentIndex + 1 < children.length ? currentIndex + 1 : 0],
forceSubMenuOpen: false
});
}
};
this.onChildMouseMove = function (child) {
if (_this2.state.selectedItem !== child) {
_this2.setState({ selectedItem: child, forceSubMenuOpen: false });
}
};
this.onChildMouseLeave = function () {
_this2.setState({ selectedItem: null, forceSubMenuOpen: false });
};
this.renderChildren = function (children) {
return _react2.default.Children.map(children, function (child) {
var props = {};
if (!_react2.default.isValidElement(child)) return child;
if ([_MenuItem2.default, _this2.getSubMenuType()].indexOf(child.type) < 0) {
// Maybe the MenuItem or SubMenu is capsuled in a wrapper div or something else
props.children = _this2.renderChildren(child.props.children);
return _react2.default.cloneElement(child, props);
}
props.onMouseLeave = _this2.onChildMouseLeave.bind(_this2);
if (child.type === _this2.getSubMenuType()) {
// special props for SubMenu only
props.forceOpen = _this2.state.forceSubMenuOpen && _this2.state.selectedItem === child;
props.forceClose = _this2.handleForceClose;
props.parentKeyNavigationHandler = _this2.handleKeyNavigation;
}
if (!child.props.divider && _this2.state.selectedItem === child) {
// special props for selected item only
props.selected = true;
props.ref = function (ref) {
_this2.seletedItemRef = ref;
};
return _react2.default.cloneElement(child, props);
}
// onMouseMove is only needed for non selected items
props.onMouseMove = function () {
return _this2.onChildMouseMove(child);
};
return _react2.default.cloneElement(child, props);
});
};
};
exports.default = AbstractMenu;