1 | import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
3 | import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
4 | import _extends from "@babel/runtime/helpers/esm/extends";
|
5 | import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
6 | import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
7 | import _createClass from "@babel/runtime/helpers/esm/createClass";
|
8 | import _inherits from "@babel/runtime/helpers/esm/inherits";
|
9 | import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
10 | var _excluded = ["title", "attribute", "elementRef"],
|
11 | _excluded2 = ["style", "className", "eventKey", "warnKey", "disabled", "itemIcon", "children", "role", "onMouseEnter", "onMouseLeave", "onClick", "onKeyDown", "onFocus"],
|
12 | _excluded3 = ["active"];
|
13 | import * as React from 'react';
|
14 | import classNames from 'classnames';
|
15 | import Overflow from 'rc-overflow';
|
16 | import warning from "rc-util/es/warning";
|
17 | import KeyCode from "rc-util/es/KeyCode";
|
18 | import omit from "rc-util/es/omit";
|
19 | import { MenuContext } from './context/MenuContext';
|
20 | import useActive from './hooks/useActive';
|
21 | import { warnItemProp } from './utils/warnUtil';
|
22 | import Icon from './Icon';
|
23 | import useDirectionStyle from './hooks/useDirectionStyle';
|
24 | import { useFullPath, useMeasure } from './context/PathContext';
|
25 | import { useMenuId } from './context/IdContext';
|
26 |
|
27 |
|
28 |
|
29 | var LegacyMenuItem = 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 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 |
|
63 |
|
64 |
|
65 |
|
66 | var 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);
|
100 |
|
101 | if (process.env.NODE_ENV !== 'production' && warnKey) {
|
102 | warning(false, 'MenuItem should not leave undefined `key`.');
|
103 | }
|
104 |
|
105 |
|
106 | var getEventInfo = function getEventInfo(e) {
|
107 | return {
|
108 | key: eventKey,
|
109 |
|
110 | keyPath: _toConsumableArray(connectedKeys).reverse(),
|
111 | item: legacyMenuItemRef.current,
|
112 | domEvent: e
|
113 | };
|
114 | };
|
115 |
|
116 |
|
117 | var mergedItemIcon = itemIcon || contextItemIcon;
|
118 |
|
119 | var _useActive = useActive(eventKey, mergedDisabled, onMouseEnter, onMouseLeave),
|
120 | active = _useActive.active,
|
121 | activeProps = _objectWithoutProperties(_useActive, _excluded3);
|
122 |
|
123 |
|
124 | var selected = selectedKeys.includes(eventKey);
|
125 |
|
126 | var directionStyle = useDirectionStyle(connectedKeys.length);
|
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);
|
143 |
|
144 | onClick === null || onClick === void 0 ? void 0 : onClick(warnItemProp(info));
|
145 | onItemClick(info);
|
146 | }
|
147 | };
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | var onInternalFocus = function onInternalFocus(e) {
|
155 | onActive(eventKey);
|
156 | onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
|
157 | };
|
158 |
|
159 |
|
160 | var optionRoleProps = {};
|
161 |
|
162 | if (props.role === 'option') {
|
163 | optionRoleProps['aria-selected'] = selected;
|
164 | }
|
165 |
|
166 | return 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, React.createElement(Icon, {
|
181 | props: _objectSpread(_objectSpread({}, props), {}, {
|
182 | isSelected: selected
|
183 | }),
|
184 | icon: mergedItemIcon
|
185 | }));
|
186 | };
|
187 |
|
188 | function MenuItem(props) {
|
189 | var eventKey = props.eventKey;
|
190 |
|
191 | var measure = useMeasure();
|
192 | var connectedKeyPath = useFullPath(eventKey);
|
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 | }
|
206 |
|
207 |
|
208 | return React.createElement(InternalMenuItem, props);
|
209 | }
|
210 |
|
211 | export default MenuItem; |
\ | No newline at end of file |