OperationNode.js
5.15 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
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import classNames from 'classnames';
import { useState, useEffect } from 'react';
import KeyCode from "rc-util/es/KeyCode";
import Menu, { MenuItem } from 'rc-menu';
import Dropdown from 'rc-dropdown';
import AddButton from './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 = useState(false),
_useState2 = _slicedToArray(_useState, 2),
open = _useState2[0],
setOpen = _useState2[1];
var _useState3 = useState(null),
_useState4 = _slicedToArray(_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(Menu, {
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(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.DOWN, KeyCode.SPACE, KeyCode.ENTER].includes(which)) {
setOpen(true);
e.preventDefault();
}
return;
}
switch (which) {
case KeyCode.UP:
selectOffset(-1);
e.preventDefault();
break;
case KeyCode.DOWN:
selectOffset(1);
e.preventDefault();
break;
case KeyCode.ESC:
setOpen(false);
break;
case KeyCode.SPACE:
case KeyCode.ENTER:
if (selectedKey !== null) onTabClick(selectedKey, e);
break;
}
} // ========================= Effect =========================
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]);
useEffect(function () {
if (!open) {
setSelectedKey(null);
}
}, [open]); // ========================= Render =========================
var moreStyle = _defineProperty({}, rtl ? 'marginLeft' : 'marginRight', tabBarGutter);
if (!tabs.length) {
moreStyle.visibility = 'hidden';
moreStyle.order = 1;
}
var overlayClassName = classNames(_defineProperty({}, "".concat(dropdownPrefix, "-rtl"), rtl));
var moreNode = mobile ? null : /*#__PURE__*/React.createElement(Dropdown, {
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: classNames("".concat(prefixCls, "-nav-operations"), className),
style: style,
ref: ref
}, moreNode, /*#__PURE__*/React.createElement(AddButton, {
prefixCls: prefixCls,
locale: locale,
editable: editable
}));
}
export default /*#__PURE__*/React.forwardRef(OperationNode);