Repository navigation
Expand file tree
/
Copy pathOperationNode.tsx
More file actions
245 lines (224 loc) · 6.7 KB
/
Copy pathOperationNode.tsx
File metadata and controls
245 lines (224 loc) · 6.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { clsx } from 'clsx';
import Dropdown from '@rc-component/dropdown';
import Menu, { MenuItem } from '@rc-component/menu';
import { KeyCode } from '@rc-component/util';
import * as React from 'react';
import { useEffect, useState } from 'react';
import type { EditableConfig, Tab, TabsLocale, MoreProps } from '../interface';
import { getRemovable } from '../util';
import AddButton from './AddButton';
import type { SemanticName } from '../Tabs';
export interface OperationNodeProps {
prefixCls: string;
className?: string;
style?: React.CSSProperties;
id: string;
tabs: Tab[];
rtl: boolean;
tabBarGutter?: number;
activeKey: string;
mobile: boolean;
more?: MoreProps;
editable?: EditableConfig;
locale?: TabsLocale;
removeAriaLabel?: string;
onTabClick: (key: string, e: React.MouseEvent | React.KeyboardEvent) => void;
tabMoving?: boolean;
getPopupContainer?: (node: HTMLElement) => HTMLElement;
popupClassName?: string;
popupStyle?: React.CSSProperties;
styles?: Pick<Partial<Record<SemanticName, React.CSSProperties>>, 'remove'>;
classNames?: Pick<Partial<Record<SemanticName, string>>, 'remove'>;
}
const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((props, ref) => {
const {
prefixCls,
id,
tabs,
locale,
mobile,
more: moreProps = {},
style,
className,
editable,
tabBarGutter,
rtl,
removeAriaLabel,
onTabClick,
getPopupContainer,
popupClassName,
popupStyle,
classNames,
styles,
} = props;
// ======================== Dropdown ========================
const [open, setOpen] = useState(false);
const [selectedKey, setSelectedKey] = useState<string>(null);
const { icon: moreIcon = 'More', popupRender } = moreProps;
const popupId = `${id}-more-popup`;
const dropdownPrefix = `${prefixCls}-dropdown`;
const selectedItemId = selectedKey !== null ? `${popupId}-${selectedKey}` : null;
const dropdownAriaLabel = locale?.dropdownAriaLabel;
function onRemoveTab(event: React.MouseEvent | React.KeyboardEvent, key: string) {
event.preventDefault();
event.stopPropagation();
editable.onEdit('remove', { key, event });
}
const menu = (
<Menu
onClick={({ key, domEvent }) => {
onTabClick(key, domEvent);
setOpen(false);
}}
prefixCls={`${dropdownPrefix}-menu`}
id={popupId}
tabIndex={-1}
role="listbox"
aria-activedescendant={selectedItemId}
selectedKeys={[selectedKey]}
aria-label={dropdownAriaLabel !== undefined ? dropdownAriaLabel : 'expanded dropdown'}
>
{tabs.map<React.ReactNode>(tab => {
const { closable, disabled, closeIcon, key, label } = tab;
const removable = getRemovable(closable, closeIcon, editable, disabled);
return (
<MenuItem
key={key}
id={`${popupId}-${key}`}
role="option"
aria-controls={id && `${id}-panel-${key}`}
disabled={disabled}
>
{/* {tab.tab} */}
<span>{label}</span>
{removable && (
<button
type="button"
aria-label={removeAriaLabel || 'remove'}
tabIndex={0}
className={clsx(`${dropdownPrefix}-menu-item-remove`, classNames?.remove)}
style={styles?.remove}
onClick={e => {
e.stopPropagation();
onRemoveTab(e, key);
}}
>
{closeIcon || editable.removeIcon || '×'}
</button>
)}
</MenuItem>
);
})}
</Menu>
);
const overlay = popupRender
? popupRender(menu, {
restTabs: tabs,
onClose: () => setOpen(false),
})
: menu;
function selectOffset(offset: -1 | 1) {
const enabledTabs = tabs.filter(tab => !tab.disabled);
let selectedIndex = enabledTabs.findIndex(tab => tab.key === selectedKey) || 0;
const len = enabledTabs.length;
for (let i = 0; i < len; i += 1) {
selectedIndex = (selectedIndex + offset + len) % len;
const tab = enabledTabs[selectedIndex];
if (!tab.disabled) {
setSelectedKey(tab.key);
return;
}
}
}
function onKeyDown(e: React.KeyboardEvent) {
const { which } = e;
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(() => {
// We use query element here to avoid React strict warning
const ele = document.getElementById(selectedItemId);
if (ele?.scrollIntoView) {
ele.scrollIntoView(false);
}
}, [selectedItemId, selectedKey]);
useEffect(() => {
if (!open) {
setSelectedKey(null);
}
}, [open]);
// ========================= Render =========================
const moreStyle: React.CSSProperties = {
marginInlineStart: tabBarGutter,
};
if (!tabs.length) {
moreStyle.visibility = 'hidden';
moreStyle.order = 1;
}
const overlayClassName = clsx(popupClassName, { [`${dropdownPrefix}-rtl`]: rtl });
const moreNode: React.ReactNode = mobile ? null : (
<Dropdown
prefixCls={dropdownPrefix}
overlay={overlay}
visible={tabs.length ? open : false}
onVisibleChange={setOpen}
overlayClassName={overlayClassName}
overlayStyle={popupStyle}
mouseEnterDelay={0.1}
mouseLeaveDelay={0.1}
getPopupContainer={getPopupContainer}
{...moreProps}
>
<button
type="button"
className={`${prefixCls}-nav-more`}
style={moreStyle}
aria-haspopup="listbox"
aria-controls={popupId}
id={`${id}-more`}
aria-expanded={open}
onKeyDown={onKeyDown}
>
{moreIcon}
</button>
</Dropdown>
);
return (
<div className={clsx(`${prefixCls}-nav-operations`, className)} style={style} ref={ref}>
{moreNode}
<AddButton prefixCls={prefixCls} locale={locale} editable={editable} />
</div>
);
});
export default React.memo(
OperationNode,
(_, next) =>
// 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,
);