MenuItem.js
7.84 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.MenuItem = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _classnames = _interopRequireDefault(require("classnames"));
var _omit = _interopRequireDefault(require("omit.js"));
var _miniStore = require("mini-store");
var _util = require("./util");
var MenuItem = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(MenuItem, _React$Component);
var _super = (0, _createSuper2.default)(MenuItem);
function MenuItem() {
var _this;
(0, _classCallCheck2.default)(this, MenuItem);
_this = _super.apply(this, arguments);
_this.onKeyDown = function (e) {
var keyCode = e.keyCode;
if (keyCode === _KeyCode.default.ENTER) {
_this.onClick(e);
return true;
}
return undefined;
};
_this.onMouseLeave = function (e) {
var _this$props = _this.props,
eventKey = _this$props.eventKey,
onItemHover = _this$props.onItemHover,
onMouseLeave = _this$props.onMouseLeave;
onItemHover({
key: eventKey,
hover: false
});
onMouseLeave({
key: eventKey,
domEvent: e
});
};
_this.onMouseEnter = function (e) {
var _this$props2 = _this.props,
eventKey = _this$props2.eventKey,
onItemHover = _this$props2.onItemHover,
onMouseEnter = _this$props2.onMouseEnter;
onItemHover({
key: eventKey,
hover: true
});
onMouseEnter({
key: eventKey,
domEvent: e
});
};
_this.onClick = function (e) {
var _this$props3 = _this.props,
eventKey = _this$props3.eventKey,
multiple = _this$props3.multiple,
onClick = _this$props3.onClick,
onSelect = _this$props3.onSelect,
onDeselect = _this$props3.onDeselect,
isSelected = _this$props3.isSelected;
var info = {
key: eventKey,
keyPath: [eventKey],
item: (0, _assertThisInitialized2.default)(_this),
domEvent: e
};
onClick(info);
if (multiple) {
if (isSelected) {
onDeselect(info);
} else {
onSelect(info);
}
} else if (!isSelected) {
onSelect(info);
}
};
_this.saveNode = function (node) {
_this.node = node;
};
return _this;
}
(0, _createClass2.default)(MenuItem, [{
key: "componentDidMount",
value: function componentDidMount() {
// invoke customized ref to expose component to mixin
this.callRef();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.callRef();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var props = this.props;
if (props.onDestroy) {
props.onDestroy(props.eventKey);
}
}
}, {
key: "getPrefixCls",
value: function getPrefixCls() {
return "".concat(this.props.rootPrefixCls, "-item");
}
}, {
key: "getActiveClassName",
value: function getActiveClassName() {
return "".concat(this.getPrefixCls(), "-active");
}
}, {
key: "getSelectedClassName",
value: function getSelectedClassName() {
return "".concat(this.getPrefixCls(), "-selected");
}
}, {
key: "getDisabledClassName",
value: function getDisabledClassName() {
return "".concat(this.getPrefixCls(), "-disabled");
}
}, {
key: "callRef",
value: function callRef() {
if (this.props.manualRef) {
this.props.manualRef(this);
}
}
}, {
key: "render",
value: function render() {
var _classNames;
var props = (0, _objectSpread2.default)({}, this.props);
var className = (0, _classnames.default)(this.getPrefixCls(), props.className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, this.getActiveClassName(), !props.disabled && props.active), (0, _defineProperty2.default)(_classNames, this.getSelectedClassName(), props.isSelected), (0, _defineProperty2.default)(_classNames, this.getDisabledClassName(), props.disabled), _classNames));
var attrs = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props.attribute), {}, {
title: typeof props.title === 'string' ? props.title : undefined,
className: className,
// set to menuitem by default
role: props.role || 'menuitem',
'aria-disabled': props.disabled
});
if (props.role === 'option') {
// overwrite to option
attrs = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attrs), {}, {
role: 'option',
'aria-selected': props.isSelected
});
} else if (props.role === null || props.role === 'none') {
// sometimes we want to specify role inside <li/> element
// <li><a role='menuitem'>Link</a></li> would be a good example
// in this case the role on <li/> should be "none" to
// remove the implied listitem role.
// https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html
attrs.role = 'none';
} // In case that onClick/onMouseLeave/onMouseEnter is passed down from owner
var mouseEvent = {
onClick: props.disabled ? null : this.onClick,
onMouseLeave: props.disabled ? null : this.onMouseLeave,
onMouseEnter: props.disabled ? null : this.onMouseEnter
};
var style = (0, _objectSpread2.default)({}, props.style);
if (props.mode === 'inline') {
if (props.direction === 'rtl') {
style.paddingRight = props.inlineIndent * props.level;
} else {
style.paddingLeft = props.inlineIndent * props.level;
}
}
_util.menuAllProps.forEach(function (key) {
return delete props[key];
});
delete props.direction;
var icon = this.props.itemIcon;
if (typeof this.props.itemIcon === 'function') {
// TODO: This is a bug which should fixed after TS refactor
icon = React.createElement(this.props.itemIcon, this.props);
}
return React.createElement("li", Object.assign({}, (0, _omit.default)(props, ['onClick', 'onMouseEnter', 'onMouseLeave', 'onSelect']), attrs, mouseEvent, {
style: style,
ref: this.saveNode
}), props.children, icon);
}
}]);
return MenuItem;
}(React.Component);
exports.MenuItem = MenuItem;
MenuItem.isMenuItem = true;
MenuItem.defaultProps = {
onSelect: _util.noop,
onMouseEnter: _util.noop,
onMouseLeave: _util.noop,
manualRef: _util.noop
};
var connected = (0, _miniStore.connect)(function (_ref, _ref2) {
var activeKey = _ref.activeKey,
selectedKeys = _ref.selectedKeys;
var eventKey = _ref2.eventKey,
subMenuKey = _ref2.subMenuKey;
return {
active: activeKey[subMenuKey] === eventKey,
isSelected: selectedKeys.indexOf(eventKey) !== -1
};
})(MenuItem);
var _default = connected;
exports.default = _default;