OperationNode.js
5.85 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _rcMenu = _interopRequireWildcard(require("rc-menu"));
var _rcDropdown = _interopRequireDefault(require("rc-dropdown"));
var _AddButton = _interopRequireDefault(require("./AddButton"));
function OperationNode(_ref, ref) {
var prefixCls = _ref.prefixCls,
id = _ref.id,
tabs = _ref.tabs,
locale = _ref.locale,
mobile = _ref.mobile,
_ref$moreIcon = _ref.moreIcon,
moreIcon = _ref$moreIcon === void 0 ? 'More' : _ref$moreIcon,
moreTransitionName = _ref.moreTransitionName,
style = _ref.style,
className = _ref.className,
editable = _ref.editable,
tabBarGutter = _ref.tabBarGutter,
rtl = _ref.rtl,
onTabClick = _ref.onTabClick;
// ======================== 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 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;
var menu = /*#__PURE__*/React.createElement(_rcMenu.default, {
onClick: function onClick(_ref2) {
var key = _ref2.key,
domEvent = _ref2.domEvent;
onTabClick(key, domEvent);
setOpen(false);
},
id: popupId,
tabIndex: -1,
role: "listbox",
"aria-activedescendant": selectedItemId,
selectedKeys: [selectedKey],
"aria-label": dropdownAriaLabel !== undefined ? dropdownAriaLabel : 'expanded dropdown'
}, tabs.map(function (tab) {
return /*#__PURE__*/React.createElement(_rcMenu.MenuItem, {
key: tab.key,
id: "".concat(popupId, "-").concat(tab.key),
role: "option",
"aria-controls": id && "".concat(id, "-panel-").concat(tab.key),
disabled: tab.disabled
}, tab.tab);
}));
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 ? 'marginLeft' : 'marginRight', 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, {
prefixCls: dropdownPrefix,
overlay: menu,
trigger: ['hover'],
visible: open,
transitionName: moreTransitionName,
onVisibleChange: setOpen,
overlayClassName: overlayClassName,
mouseEnterDelay: 0.1,
mouseLeaveDelay: 0.1
}, /*#__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 = /*#__PURE__*/React.forwardRef(OperationNode);
exports.default = _default;