acud
Version:
acg react 组件库
189 lines • 8.34 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _extends from "@babel/runtime/helpers/esm/extends";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import classNames from 'classnames';
import Overflow from 'rc-overflow';
import warning from "rc-util/es/warning";
import KeyCode from "rc-util/es/KeyCode";
import omit from "rc-util/es/omit";
import { MenuContext } from './context/MenuContext';
import useActive from './hooks/useActive';
import { warnItemProp } from './utils/warnUtil';
import Icon from './Icon';
import useDirectionStyle from './hooks/useDirectionStyle';
import { useFullPath, useMeasure } from './context/PathContext';
import { useMenuId } from './context/IdContext';
// Since Menu event provide the `info.item` which point to the MenuItem node instance.
// We have to use class component here.
// This should be removed from doc & api in future.
var LegacyMenuItem = /*#__PURE__*/function (_React$Component) {
function LegacyMenuItem() {
_classCallCheck(this, LegacyMenuItem);
return _callSuper(this, LegacyMenuItem, arguments);
}
_inherits(LegacyMenuItem, _React$Component);
return _createClass(LegacyMenuItem, [{
key: "render",
value: function render() {
var _a = this.props,
title = _a.title,
attribute = _a.attribute,
elementRef = _a.elementRef,
restProps = __rest(_a, ["title", "attribute", "elementRef"]);
var passedProps = omit(restProps, ['eventKey']);
warning(!attribute, '`attribute` of Menu.Item is deprecated. Please pass attribute directly.');
return /*#__PURE__*/React.createElement(Overflow.Item, _extends({}, attribute, {
title: typeof title === 'string' ? title : undefined
}, passedProps, {
ref: elementRef
}));
}
}]);
}(React.Component);
/**
* Real Menu Item component
*/
var InternalMenuItem = function InternalMenuItem(props) {
var style = props.style,
className = props.className,
eventKey = props.eventKey,
sub = props.sub,
warnKey = props.warnKey,
disabled = props.disabled,
itemIcon = props.itemIcon,
children = props.children,
role = props.role,
onMouseEnter = props.onMouseEnter,
onMouseLeave = props.onMouseLeave,
onClick = props.onClick,
onKeyDown = props.onKeyDown,
onFocus = props.onFocus,
restProps = __rest(props, ["style", "className", "eventKey", "sub", "warnKey", "disabled", "itemIcon", "children", "role", "onMouseEnter", "onMouseLeave", "onClick", "onKeyDown", "onFocus"]);
var domDataId = useMenuId(eventKey);
var _React$useContext = React.useContext(MenuContext),
prefixCls = _React$useContext.prefixCls,
onItemClick = _React$useContext.onItemClick,
contextDisabled = _React$useContext.disabled,
overflowDisabled = _React$useContext.overflowDisabled,
contextItemIcon = _React$useContext.itemIcon,
selectedKeys = _React$useContext.selectedKeys,
onActive = _React$useContext.onActive;
var itemCls = "".concat(prefixCls, "-item");
var legacyMenuItemRef = React.useRef();
var elementRef = React.useRef();
var mergedDisabled = contextDisabled || disabled;
var connectedKeys = useFullPath(eventKey);
// ================================ Warn ================================
if (process.env.NODE_ENV !== 'production' && warnKey) {
warning(false, 'MenuItem should not leave undefined `key`.');
}
// ============================= Info =============================
var getEventInfo = function getEventInfo(e) {
return {
key: eventKey,
// Note: For legacy code is reversed which not like other antd component
keyPath: _toConsumableArray(connectedKeys).reverse(),
item: legacyMenuItemRef.current,
domEvent: e
};
};
// ============================= Icon =============================
var mergedItemIcon = itemIcon || contextItemIcon;
// ============================ Active ============================
var _a = useActive(eventKey, mergedDisabled, onMouseEnter, onMouseLeave),
active = _a.active,
activeProps = __rest(_a, ["active"]);
// ============================ Select ============================
var selected = selectedKeys.includes(eventKey);
// ======================== DirectionStyle ========================
var directionStyle = useDirectionStyle(className, sub);
// ============================ Events ============================
var onInternalClick = function onInternalClick(e) {
if (mergedDisabled) {
return;
}
var info = getEventInfo(e);
onClick === null || onClick === void 0 ? void 0 : onClick(warnItemProp(info));
onItemClick(info);
};
var onInternalKeyDown = function onInternalKeyDown(e) {
onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(e);
if (e.which === KeyCode.ENTER) {
var info = getEventInfo(e);
// Legacy. Key will also trigger click event
onClick === null || onClick === void 0 ? void 0 : onClick(warnItemProp(info));
onItemClick(info);
}
};
/**
* Used for accessibility. Helper will focus element without key board.
* We should manually trigger an active
*/
var onInternalFocus = function onInternalFocus(e) {
onActive(eventKey);
onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
};
// ============================ Render ============================
var optionRoleProps = {};
if (props.role === 'option') {
optionRoleProps['aria-selected'] = selected;
}
return /*#__PURE__*/React.createElement(LegacyMenuItem, _extends({
ref: legacyMenuItemRef,
elementRef: elementRef,
role: role === null ? 'none' : role || 'menuitem',
tabIndex: disabled ? null : -1,
"data-menu-id": overflowDisabled && domDataId ? null : domDataId
}, restProps, activeProps, optionRoleProps, {
component: "li",
"aria-disabled": disabled,
style: _extends(_extends({}, directionStyle), style),
className: classNames(itemCls, _defineProperty(_defineProperty(_defineProperty({}, "".concat(itemCls, "-active"), active), "".concat(itemCls, "-selected"), selected), "".concat(itemCls, "-disabled"), mergedDisabled), className),
onClick: onInternalClick,
onKeyDown: onInternalKeyDown,
onFocus: onInternalFocus
}), children, /*#__PURE__*/React.createElement(Icon, {
props: _extends(_extends({}, props), {
isSelected: selected
}),
icon: mergedItemIcon
}));
};
function MenuItem(props) {
var eventKey = props.eventKey;
// ==================== Record KeyPath ====================
var measure = useMeasure();
var connectedKeyPath = useFullPath(eventKey);
// eslint-disable-next-line consistent-return
React.useEffect(function () {
if (measure) {
measure.registerPath(eventKey, connectedKeyPath);
return function () {
measure.unregisterPath(eventKey, connectedKeyPath);
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [connectedKeyPath]);
if (measure) {
return null;
}
// ======================== Render ========================
return /*#__PURE__*/React.createElement(InternalMenuItem, props);
}
export default MenuItem;