MenuItem.js
6.96 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
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import * as React from 'react';
import KeyCode from "rc-util/es/KeyCode";
import classNames from 'classnames';
import omit from 'omit.js';
import { connect } from 'mini-store';
import { noop, menuAllProps } from './util';
export var MenuItem = /*#__PURE__*/function (_React$Component) {
_inherits(MenuItem, _React$Component);
var _super = _createSuper(MenuItem);
function MenuItem() {
var _this;
_classCallCheck(this, MenuItem);
_this = _super.apply(this, arguments);
_this.onKeyDown = function (e) {
var keyCode = e.keyCode;
if (keyCode === KeyCode.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: _assertThisInitialized(_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;
}
_createClass(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 = _objectSpread({}, this.props);
var className = classNames(this.getPrefixCls(), props.className, (_classNames = {}, _defineProperty(_classNames, this.getActiveClassName(), !props.disabled && props.active), _defineProperty(_classNames, this.getSelectedClassName(), props.isSelected), _defineProperty(_classNames, this.getDisabledClassName(), props.disabled), _classNames));
var attrs = _objectSpread(_objectSpread({}, 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 = _objectSpread(_objectSpread({}, 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 = _objectSpread({}, props.style);
if (props.mode === 'inline') {
if (props.direction === 'rtl') {
style.paddingRight = props.inlineIndent * props.level;
} else {
style.paddingLeft = props.inlineIndent * props.level;
}
}
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({}, omit(props, ['onClick', 'onMouseEnter', 'onMouseLeave', 'onSelect']), attrs, mouseEvent, {
style: style,
ref: this.saveNode
}), props.children, icon);
}
}]);
return MenuItem;
}(React.Component);
MenuItem.isMenuItem = true;
MenuItem.defaultProps = {
onSelect: noop,
onMouseEnter: noop,
onMouseLeave: noop,
manualRef: noop
};
var connected = 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);
export default connected;