205 lines
8.0 KiB
JavaScript
205 lines
8.0 KiB
JavaScript
"use strict";
|
||
|
||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||
var _typeof = require("@babel/runtime/helpers/typeof");
|
||
Object.defineProperty(exports, "__esModule", {
|
||
value: true
|
||
});
|
||
exports.default = void 0;
|
||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
||
var _classnames = _interopRequireDefault(require("classnames"));
|
||
var _rcDropdown = _interopRequireDefault(require("rc-dropdown"));
|
||
var _rcMenu = _interopRequireWildcard(require("rc-menu"));
|
||
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
|
||
var _react = _interopRequireWildcard(require("react"));
|
||
var React = _react;
|
||
var _util = require("../util");
|
||
var _AddButton = _interopRequireDefault(require("./AddButton"));
|
||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
||
var OperationNode = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
||
var prefixCls = props.prefixCls,
|
||
id = props.id,
|
||
tabs = props.tabs,
|
||
locale = props.locale,
|
||
mobile = props.mobile,
|
||
_props$more = props.more,
|
||
moreProps = _props$more === void 0 ? {} : _props$more,
|
||
style = props.style,
|
||
className = props.className,
|
||
editable = props.editable,
|
||
tabBarGutter = props.tabBarGutter,
|
||
rtl = props.rtl,
|
||
removeAriaLabel = props.removeAriaLabel,
|
||
onTabClick = props.onTabClick,
|
||
getPopupContainer = props.getPopupContainer,
|
||
popupClassName = props.popupClassName;
|
||
// ======================== Dropdown ========================
|
||
var _useState = (0, _react.useState)(false),
|
||
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
||
open = _useState2[0],
|
||
setOpen = _useState2[1];
|
||
var _useState3 = (0, _react.useState)(null),
|
||
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
|
||
selectedKey = _useState4[0],
|
||
setSelectedKey = _useState4[1];
|
||
var _moreProps$icon = moreProps.icon,
|
||
moreIcon = _moreProps$icon === void 0 ? 'More' : _moreProps$icon;
|
||
var popupId = "".concat(id, "-more-popup");
|
||
var dropdownPrefix = "".concat(prefixCls, "-dropdown");
|
||
var selectedItemId = selectedKey !== null ? "".concat(popupId, "-").concat(selectedKey) : null;
|
||
var dropdownAriaLabel = locale === null || locale === void 0 ? void 0 : locale.dropdownAriaLabel;
|
||
function onRemoveTab(event, key) {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
editable.onEdit('remove', {
|
||
key: key,
|
||
event: event
|
||
});
|
||
}
|
||
var menu = /*#__PURE__*/React.createElement(_rcMenu.default, {
|
||
onClick: function onClick(_ref) {
|
||
var key = _ref.key,
|
||
domEvent = _ref.domEvent;
|
||
onTabClick(key, domEvent);
|
||
setOpen(false);
|
||
},
|
||
prefixCls: "".concat(dropdownPrefix, "-menu"),
|
||
id: popupId,
|
||
tabIndex: -1,
|
||
role: "listbox",
|
||
"aria-activedescendant": selectedItemId,
|
||
selectedKeys: [selectedKey],
|
||
"aria-label": dropdownAriaLabel !== undefined ? dropdownAriaLabel : 'expanded dropdown'
|
||
}, tabs.map(function (tab) {
|
||
var closable = tab.closable,
|
||
disabled = tab.disabled,
|
||
closeIcon = tab.closeIcon,
|
||
key = tab.key,
|
||
label = tab.label;
|
||
var removable = (0, _util.getRemovable)(closable, closeIcon, editable, disabled);
|
||
return /*#__PURE__*/React.createElement(_rcMenu.MenuItem, {
|
||
key: key,
|
||
id: "".concat(popupId, "-").concat(key),
|
||
role: "option",
|
||
"aria-controls": id && "".concat(id, "-panel-").concat(key),
|
||
disabled: disabled
|
||
}, /*#__PURE__*/React.createElement("span", null, label), removable && /*#__PURE__*/React.createElement("button", {
|
||
type: "button",
|
||
"aria-label": removeAriaLabel || 'remove',
|
||
tabIndex: 0,
|
||
className: "".concat(dropdownPrefix, "-menu-item-remove"),
|
||
onClick: function onClick(e) {
|
||
e.stopPropagation();
|
||
onRemoveTab(e, key);
|
||
}
|
||
}, closeIcon || editable.removeIcon || '×'));
|
||
}));
|
||
function selectOffset(offset) {
|
||
var enabledTabs = tabs.filter(function (tab) {
|
||
return !tab.disabled;
|
||
});
|
||
var selectedIndex = enabledTabs.findIndex(function (tab) {
|
||
return tab.key === selectedKey;
|
||
}) || 0;
|
||
var len = enabledTabs.length;
|
||
for (var i = 0; i < len; i += 1) {
|
||
selectedIndex = (selectedIndex + offset + len) % len;
|
||
var tab = enabledTabs[selectedIndex];
|
||
if (!tab.disabled) {
|
||
setSelectedKey(tab.key);
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
function onKeyDown(e) {
|
||
var which = e.which;
|
||
if (!open) {
|
||
if ([_KeyCode.default.DOWN, _KeyCode.default.SPACE, _KeyCode.default.ENTER].includes(which)) {
|
||
setOpen(true);
|
||
e.preventDefault();
|
||
}
|
||
return;
|
||
}
|
||
switch (which) {
|
||
case _KeyCode.default.UP:
|
||
selectOffset(-1);
|
||
e.preventDefault();
|
||
break;
|
||
case _KeyCode.default.DOWN:
|
||
selectOffset(1);
|
||
e.preventDefault();
|
||
break;
|
||
case _KeyCode.default.ESC:
|
||
setOpen(false);
|
||
break;
|
||
case _KeyCode.default.SPACE:
|
||
case _KeyCode.default.ENTER:
|
||
if (selectedKey !== null) {
|
||
onTabClick(selectedKey, e);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
// ========================= Effect =========================
|
||
(0, _react.useEffect)(function () {
|
||
// We use query element here to avoid React strict warning
|
||
var ele = document.getElementById(selectedItemId);
|
||
if (ele && ele.scrollIntoView) {
|
||
ele.scrollIntoView(false);
|
||
}
|
||
}, [selectedKey]);
|
||
(0, _react.useEffect)(function () {
|
||
if (!open) {
|
||
setSelectedKey(null);
|
||
}
|
||
}, [open]);
|
||
|
||
// ========================= Render =========================
|
||
var moreStyle = (0, _defineProperty2.default)({}, rtl ? 'marginRight' : 'marginLeft', tabBarGutter);
|
||
if (!tabs.length) {
|
||
moreStyle.visibility = 'hidden';
|
||
moreStyle.order = 1;
|
||
}
|
||
var overlayClassName = (0, _classnames.default)((0, _defineProperty2.default)({}, "".concat(dropdownPrefix, "-rtl"), rtl));
|
||
var moreNode = mobile ? null : /*#__PURE__*/React.createElement(_rcDropdown.default, (0, _extends2.default)({
|
||
prefixCls: dropdownPrefix,
|
||
overlay: menu,
|
||
visible: tabs.length ? open : false,
|
||
onVisibleChange: setOpen,
|
||
overlayClassName: (0, _classnames.default)(overlayClassName, popupClassName),
|
||
mouseEnterDelay: 0.1,
|
||
mouseLeaveDelay: 0.1,
|
||
getPopupContainer: getPopupContainer
|
||
}, moreProps), /*#__PURE__*/React.createElement("button", {
|
||
type: "button",
|
||
className: "".concat(prefixCls, "-nav-more"),
|
||
style: moreStyle,
|
||
tabIndex: -1,
|
||
"aria-hidden": "true",
|
||
"aria-haspopup": "listbox",
|
||
"aria-controls": popupId,
|
||
id: "".concat(id, "-more"),
|
||
"aria-expanded": open,
|
||
onKeyDown: onKeyDown
|
||
}, moreIcon));
|
||
return /*#__PURE__*/React.createElement("div", {
|
||
className: (0, _classnames.default)("".concat(prefixCls, "-nav-operations"), className),
|
||
style: style,
|
||
ref: ref
|
||
}, moreNode, /*#__PURE__*/React.createElement(_AddButton.default, {
|
||
prefixCls: prefixCls,
|
||
locale: locale,
|
||
editable: editable
|
||
}));
|
||
});
|
||
var _default = exports.default = /*#__PURE__*/React.memo(OperationNode, function (_, next) {
|
||
return (
|
||
// https://github.com/ant-design/ant-design/issues/32544
|
||
// We'd better remove syntactic sugar in `rc-menu` since this has perf issue
|
||
next.tabMoving
|
||
);
|
||
}); |