UNPKG

8.01 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4import _extends from "@babel/runtime/helpers/esm/extends";
5import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
6import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
7import _createClass from "@babel/runtime/helpers/esm/createClass";
8import _inherits from "@babel/runtime/helpers/esm/inherits";
9import _createSuper from "@babel/runtime/helpers/esm/createSuper";
10var _excluded = ["title", "attribute", "elementRef"],
11 _excluded2 = ["style", "className", "eventKey", "warnKey", "disabled", "itemIcon", "children", "role", "onMouseEnter", "onMouseLeave", "onClick", "onKeyDown", "onFocus"],
12 _excluded3 = ["active"];
13import * as React from 'react';
14import classNames from 'classnames';
15import Overflow from 'rc-overflow';
16import warning from "rc-util/es/warning";
17import KeyCode from "rc-util/es/KeyCode";
18import omit from "rc-util/es/omit";
19import { MenuContext } from './context/MenuContext';
20import useActive from './hooks/useActive';
21import { warnItemProp } from './utils/warnUtil';
22import Icon from './Icon';
23import useDirectionStyle from './hooks/useDirectionStyle';
24import { useFullPath, useMeasure } from './context/PathContext';
25import { useMenuId } from './context/IdContext'; // Since Menu event provide the `info.item` which point to the MenuItem node instance.
26// We have to use class component here.
27// This should be removed from doc & api in future.
28
29var LegacyMenuItem = /*#__PURE__*/function (_React$Component) {
30 _inherits(LegacyMenuItem, _React$Component);
31
32 var _super = _createSuper(LegacyMenuItem);
33
34 function LegacyMenuItem() {
35 _classCallCheck(this, LegacyMenuItem);
36
37 return _super.apply(this, arguments);
38 }
39
40 _createClass(LegacyMenuItem, [{
41 key: "render",
42 value: function render() {
43 var _this$props = this.props,
44 title = _this$props.title,
45 attribute = _this$props.attribute,
46 elementRef = _this$props.elementRef,
47 restProps = _objectWithoutProperties(_this$props, _excluded);
48
49 var passedProps = omit(restProps, ['eventKey']);
50 warning(!attribute, '`attribute` of Menu.Item is deprecated. Please pass attribute directly.');
51 return /*#__PURE__*/React.createElement(Overflow.Item, _extends({}, attribute, {
52 title: typeof title === 'string' ? title : undefined
53 }, passedProps, {
54 ref: elementRef
55 }));
56 }
57 }]);
58
59 return LegacyMenuItem;
60}(React.Component);
61/**
62 * Real Menu Item component
63 */
64
65
66var InternalMenuItem = function InternalMenuItem(props) {
67 var _classNames;
68
69 var style = props.style,
70 className = props.className,
71 eventKey = props.eventKey,
72 warnKey = props.warnKey,
73 disabled = props.disabled,
74 itemIcon = props.itemIcon,
75 children = props.children,
76 role = props.role,
77 onMouseEnter = props.onMouseEnter,
78 onMouseLeave = props.onMouseLeave,
79 onClick = props.onClick,
80 onKeyDown = props.onKeyDown,
81 onFocus = props.onFocus,
82 restProps = _objectWithoutProperties(props, _excluded2);
83
84 var domDataId = useMenuId(eventKey);
85
86 var _React$useContext = React.useContext(MenuContext),
87 prefixCls = _React$useContext.prefixCls,
88 onItemClick = _React$useContext.onItemClick,
89 contextDisabled = _React$useContext.disabled,
90 overflowDisabled = _React$useContext.overflowDisabled,
91 contextItemIcon = _React$useContext.itemIcon,
92 selectedKeys = _React$useContext.selectedKeys,
93 onActive = _React$useContext.onActive;
94
95 var itemCls = "".concat(prefixCls, "-item");
96 var legacyMenuItemRef = React.useRef();
97 var elementRef = React.useRef();
98 var mergedDisabled = contextDisabled || disabled;
99 var connectedKeys = useFullPath(eventKey); // ================================ Warn ================================
100
101 if (process.env.NODE_ENV !== 'production' && warnKey) {
102 warning(false, 'MenuItem should not leave undefined `key`.');
103 } // ============================= Info =============================
104
105
106 var getEventInfo = function getEventInfo(e) {
107 return {
108 key: eventKey,
109 // Note: For legacy code is reversed which not like other antd component
110 keyPath: _toConsumableArray(connectedKeys).reverse(),
111 item: legacyMenuItemRef.current,
112 domEvent: e
113 };
114 }; // ============================= Icon =============================
115
116
117 var mergedItemIcon = itemIcon || contextItemIcon; // ============================ Active ============================
118
119 var _useActive = useActive(eventKey, mergedDisabled, onMouseEnter, onMouseLeave),
120 active = _useActive.active,
121 activeProps = _objectWithoutProperties(_useActive, _excluded3); // ============================ Select ============================
122
123
124 var selected = selectedKeys.includes(eventKey); // ======================== DirectionStyle ========================
125
126 var directionStyle = useDirectionStyle(connectedKeys.length); // ============================ Events ============================
127
128 var onInternalClick = function onInternalClick(e) {
129 if (mergedDisabled) {
130 return;
131 }
132
133 var info = getEventInfo(e);
134 onClick === null || onClick === void 0 ? void 0 : onClick(warnItemProp(info));
135 onItemClick(info);
136 };
137
138 var onInternalKeyDown = function onInternalKeyDown(e) {
139 onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e);
140
141 if (e.which === KeyCode.ENTER) {
142 var info = getEventInfo(e); // Legacy. Key will also trigger click event
143
144 onClick === null || onClick === void 0 ? void 0 : onClick(warnItemProp(info));
145 onItemClick(info);
146 }
147 };
148 /**
149 * Used for accessibility. Helper will focus element without key board.
150 * We should manually trigger an active
151 */
152
153
154 var onInternalFocus = function onInternalFocus(e) {
155 onActive(eventKey);
156 onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
157 }; // ============================ Render ============================
158
159
160 var optionRoleProps = {};
161
162 if (props.role === 'option') {
163 optionRoleProps['aria-selected'] = selected;
164 }
165
166 return /*#__PURE__*/React.createElement(LegacyMenuItem, _extends({
167 ref: legacyMenuItemRef,
168 elementRef: elementRef,
169 role: role === null ? 'none' : role || 'menuitem',
170 tabIndex: disabled ? null : -1,
171 "data-menu-id": overflowDisabled && domDataId ? null : domDataId
172 }, restProps, activeProps, optionRoleProps, {
173 component: "li",
174 "aria-disabled": disabled,
175 style: _objectSpread(_objectSpread({}, directionStyle), style),
176 className: classNames(itemCls, (_classNames = {}, _defineProperty(_classNames, "".concat(itemCls, "-active"), active), _defineProperty(_classNames, "".concat(itemCls, "-selected"), selected), _defineProperty(_classNames, "".concat(itemCls, "-disabled"), mergedDisabled), _classNames), className),
177 onClick: onInternalClick,
178 onKeyDown: onInternalKeyDown,
179 onFocus: onInternalFocus
180 }), children, /*#__PURE__*/React.createElement(Icon, {
181 props: _objectSpread(_objectSpread({}, props), {}, {
182 isSelected: selected
183 }),
184 icon: mergedItemIcon
185 }));
186};
187
188function MenuItem(props) {
189 var eventKey = props.eventKey; // ==================== Record KeyPath ====================
190
191 var measure = useMeasure();
192 var connectedKeyPath = useFullPath(eventKey); // eslint-disable-next-line consistent-return
193
194 React.useEffect(function () {
195 if (measure) {
196 measure.registerPath(eventKey, connectedKeyPath);
197 return function () {
198 measure.unregisterPath(eventKey, connectedKeyPath);
199 };
200 }
201 }, [connectedKeyPath]);
202
203 if (measure) {
204 return null;
205 } // ======================== Render ========================
206
207
208 return /*#__PURE__*/React.createElement(InternalMenuItem, props);
209}
210
211export default MenuItem;
\No newline at end of file