index.js
16.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { useState, useRef, useEffect } from 'react';
import classNames from 'classnames';
import raf from "rc-util/es/raf";
import ResizeObserver from 'rc-resize-observer';
import useRaf, { useRafState } from '../hooks/useRaf';
import TabNode from './TabNode';
import useOffsets from '../hooks/useOffsets';
import useVisibleRange from '../hooks/useVisibleRange';
import OperationNode from './OperationNode';
import TabContext from '../TabContext';
import useTouchMove from '../hooks/useTouchMove';
import useRefs from '../hooks/useRefs';
import AddButton from './AddButton';
import useSyncState from '../hooks/useSyncState';
var ExtraContent = function ExtraContent(_ref) {
var position = _ref.position,
prefixCls = _ref.prefixCls,
extra = _ref.extra;
if (!extra) return null;
var content;
var assertExtra = extra;
if (position === 'right') {
content = assertExtra.right || !assertExtra.left && assertExtra || null;
}
if (position === 'left') {
content = assertExtra.left || null;
}
return content ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-extra-content")
}, content) : null;
};
function TabNavList(props, ref) {
var _classNames;
var _React$useContext = React.useContext(TabContext),
prefixCls = _React$useContext.prefixCls,
tabs = _React$useContext.tabs;
var className = props.className,
style = props.style,
id = props.id,
animated = props.animated,
activeKey = props.activeKey,
rtl = props.rtl,
extra = props.extra,
editable = props.editable,
locale = props.locale,
tabPosition = props.tabPosition,
tabBarGutter = props.tabBarGutter,
children = props.children,
onTabClick = props.onTabClick,
onTabScroll = props.onTabScroll;
var tabsWrapperRef = useRef();
var tabListRef = useRef();
var operationsRef = useRef();
var innerAddButtonRef = useRef();
var _useRefs = useRefs(),
_useRefs2 = _slicedToArray(_useRefs, 2),
getBtnRef = _useRefs2[0],
removeBtnRef = _useRefs2[1];
var tabPositionTopOrBottom = tabPosition === 'top' || tabPosition === 'bottom';
var _useSyncState = useSyncState(0, function (next, prev) {
if (tabPositionTopOrBottom && onTabScroll) {
onTabScroll({
direction: next > prev ? 'left' : 'right'
});
}
}),
_useSyncState2 = _slicedToArray(_useSyncState, 2),
transformLeft = _useSyncState2[0],
setTransformLeft = _useSyncState2[1];
var _useSyncState3 = useSyncState(0, function (next, prev) {
if (!tabPositionTopOrBottom && onTabScroll) {
onTabScroll({
direction: next > prev ? 'top' : 'bottom'
});
}
}),
_useSyncState4 = _slicedToArray(_useSyncState3, 2),
transformTop = _useSyncState4[0],
setTransformTop = _useSyncState4[1];
var _useState = useState(0),
_useState2 = _slicedToArray(_useState, 2),
wrapperScrollWidth = _useState2[0],
setWrapperScrollWidth = _useState2[1];
var _useState3 = useState(0),
_useState4 = _slicedToArray(_useState3, 2),
wrapperScrollHeight = _useState4[0],
setWrapperScrollHeight = _useState4[1];
var _useState5 = useState(0),
_useState6 = _slicedToArray(_useState5, 2),
wrapperContentWidth = _useState6[0],
setWrapperContentWidth = _useState6[1];
var _useState7 = useState(0),
_useState8 = _slicedToArray(_useState7, 2),
wrapperContentHeight = _useState8[0],
setWrapperContentHeight = _useState8[1];
var _useState9 = useState(null),
_useState10 = _slicedToArray(_useState9, 2),
wrapperWidth = _useState10[0],
setWrapperWidth = _useState10[1];
var _useState11 = useState(null),
_useState12 = _slicedToArray(_useState11, 2),
wrapperHeight = _useState12[0],
setWrapperHeight = _useState12[1];
var _useState13 = useState(0),
_useState14 = _slicedToArray(_useState13, 2),
addWidth = _useState14[0],
setAddWidth = _useState14[1];
var _useState15 = useState(0),
_useState16 = _slicedToArray(_useState15, 2),
addHeight = _useState16[0],
setAddHeight = _useState16[1];
var _useRafState = useRafState(new Map()),
_useRafState2 = _slicedToArray(_useRafState, 2),
tabSizes = _useRafState2[0],
setTabSizes = _useRafState2[1];
var tabOffsets = useOffsets(tabs, tabSizes, wrapperScrollWidth); // ========================== Util =========================
var operationsHiddenClassName = "".concat(prefixCls, "-nav-operations-hidden");
var transformMin = 0;
var transformMax = 0;
if (!tabPositionTopOrBottom) {
transformMin = Math.min(0, wrapperHeight - wrapperScrollHeight);
transformMax = 0;
} else if (rtl) {
transformMin = 0;
transformMax = Math.max(0, wrapperScrollWidth - wrapperWidth);
} else {
transformMin = Math.min(0, wrapperWidth - wrapperScrollWidth);
transformMax = 0;
}
function alignInRange(value) {
if (value < transformMin) {
return transformMin;
}
if (value > transformMax) {
return transformMax;
}
return value;
} // ========================= Mobile ========================
var touchMovingRef = useRef();
var _useState17 = useState(),
_useState18 = _slicedToArray(_useState17, 2),
lockAnimation = _useState18[0],
setLockAnimation = _useState18[1];
function doLockAnimation() {
setLockAnimation(Date.now());
}
function clearTouchMoving() {
window.clearTimeout(touchMovingRef.current);
}
useTouchMove(tabsWrapperRef, function (offsetX, offsetY) {
function doMove(setState, offset) {
setState(function (value) {
var newValue = alignInRange(value + offset);
return newValue;
});
}
if (tabPositionTopOrBottom) {
// Skip scroll if place is enough
if (wrapperWidth >= wrapperScrollWidth) {
return false;
}
doMove(setTransformLeft, offsetX);
} else {
if (wrapperHeight >= wrapperScrollHeight) {
return false;
}
doMove(setTransformTop, offsetY);
}
clearTouchMoving();
doLockAnimation();
return true;
});
useEffect(function () {
clearTouchMoving();
if (lockAnimation) {
touchMovingRef.current = window.setTimeout(function () {
setLockAnimation(0);
}, 100);
}
return clearTouchMoving;
}, [lockAnimation]); // ========================= Scroll ========================
function scrollToTab() {
var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : activeKey;
var tabOffset = tabOffsets.get(key) || {
width: 0,
height: 0,
left: 0,
right: 0,
top: 0
};
if (tabPositionTopOrBottom) {
// ============ Align with top & bottom ============
var newTransform = transformLeft; // RTL
if (rtl) {
if (tabOffset.right < transformLeft) {
newTransform = tabOffset.right;
} else if (tabOffset.right + tabOffset.width > transformLeft + wrapperWidth) {
newTransform = tabOffset.right + tabOffset.width - wrapperWidth;
}
} // LTR
else if (tabOffset.left < -transformLeft) {
newTransform = -tabOffset.left;
} else if (tabOffset.left + tabOffset.width > -transformLeft + wrapperWidth) {
newTransform = -(tabOffset.left + tabOffset.width - wrapperWidth);
}
setTransformTop(0);
setTransformLeft(alignInRange(newTransform));
} else {
// ============ Align with left & right ============
var _newTransform = transformTop;
if (tabOffset.top < -transformTop) {
_newTransform = -tabOffset.top;
} else if (tabOffset.top + tabOffset.height > -transformTop + wrapperHeight) {
_newTransform = -(tabOffset.top + tabOffset.height - wrapperHeight);
}
setTransformLeft(0);
setTransformTop(alignInRange(_newTransform));
}
} // ========================== Tab ==========================
// Render tab node & collect tab offset
var _useVisibleRange = useVisibleRange(tabOffsets, {
width: wrapperWidth,
height: wrapperHeight,
left: transformLeft,
top: transformTop
}, {
width: wrapperContentWidth,
height: wrapperContentHeight
}, {
width: addWidth,
height: addHeight
}, _objectSpread(_objectSpread({}, props), {}, {
tabs: tabs
})),
_useVisibleRange2 = _slicedToArray(_useVisibleRange, 2),
visibleStart = _useVisibleRange2[0],
visibleEnd = _useVisibleRange2[1];
var tabNodes = tabs.map(function (tab) {
var key = tab.key;
return /*#__PURE__*/React.createElement(TabNode, {
id: id,
prefixCls: prefixCls,
key: key,
rtl: rtl,
tab: tab,
closable: tab.closable,
editable: editable,
active: key === activeKey,
tabPosition: tabPosition,
tabBarGutter: tabBarGutter,
renderWrapper: children,
removeAriaLabel: locale === null || locale === void 0 ? void 0 : locale.removeAriaLabel,
ref: getBtnRef(key),
onClick: function onClick(e) {
onTabClick(key, e);
},
onRemove: function onRemove() {
removeBtnRef(key);
},
onFocus: function onFocus() {
scrollToTab(key);
doLockAnimation(); // Focus element will make scrollLeft change which we should reset back
if (!rtl) {
tabsWrapperRef.current.scrollLeft = 0;
}
tabsWrapperRef.current.scrollTop = 0;
}
});
});
var onListHolderResize = useRaf(function () {
var _tabsWrapperRef$curre, _tabsWrapperRef$curre2, _innerAddButtonRef$cu, _innerAddButtonRef$cu2, _operationsRef$curren, _operationsRef$curren2, _tabListRef$current, _tabListRef$current2, _operationsRef$curren3;
// Update wrapper records
var offsetWidth = ((_tabsWrapperRef$curre = tabsWrapperRef.current) === null || _tabsWrapperRef$curre === void 0 ? void 0 : _tabsWrapperRef$curre.offsetWidth) || 0;
var offsetHeight = ((_tabsWrapperRef$curre2 = tabsWrapperRef.current) === null || _tabsWrapperRef$curre2 === void 0 ? void 0 : _tabsWrapperRef$curre2.offsetHeight) || 0;
var newAddWidth = ((_innerAddButtonRef$cu = innerAddButtonRef.current) === null || _innerAddButtonRef$cu === void 0 ? void 0 : _innerAddButtonRef$cu.offsetWidth) || 0;
var newAddHeight = ((_innerAddButtonRef$cu2 = innerAddButtonRef.current) === null || _innerAddButtonRef$cu2 === void 0 ? void 0 : _innerAddButtonRef$cu2.offsetHeight) || 0;
var newOperationWidth = ((_operationsRef$curren = operationsRef.current) === null || _operationsRef$curren === void 0 ? void 0 : _operationsRef$curren.offsetWidth) || 0;
var newOperationHeight = ((_operationsRef$curren2 = operationsRef.current) === null || _operationsRef$curren2 === void 0 ? void 0 : _operationsRef$curren2.offsetHeight) || 0;
setWrapperWidth(offsetWidth);
setWrapperHeight(offsetHeight);
setAddWidth(newAddWidth);
setAddHeight(newAddHeight);
var newWrapperScrollWidth = (((_tabListRef$current = tabListRef.current) === null || _tabListRef$current === void 0 ? void 0 : _tabListRef$current.offsetWidth) || 0) - newAddWidth;
var newWrapperScrollHeight = (((_tabListRef$current2 = tabListRef.current) === null || _tabListRef$current2 === void 0 ? void 0 : _tabListRef$current2.offsetHeight) || 0) - newAddHeight;
setWrapperScrollWidth(newWrapperScrollWidth);
setWrapperScrollHeight(newWrapperScrollHeight);
var isOperationHidden = (_operationsRef$curren3 = operationsRef.current) === null || _operationsRef$curren3 === void 0 ? void 0 : _operationsRef$curren3.className.includes(operationsHiddenClassName);
setWrapperContentWidth(newWrapperScrollWidth - (isOperationHidden ? 0 : newOperationWidth));
setWrapperContentHeight(newWrapperScrollHeight - (isOperationHidden ? 0 : newOperationHeight)); // Update buttons records
setTabSizes(function () {
var newSizes = new Map();
tabs.forEach(function (_ref2) {
var key = _ref2.key;
var btnNode = getBtnRef(key).current;
if (btnNode) {
newSizes.set(key, {
width: btnNode.offsetWidth,
height: btnNode.offsetHeight,
left: btnNode.offsetLeft,
top: btnNode.offsetTop
});
}
});
return newSizes;
});
}); // ======================== Dropdown =======================
var startHiddenTabs = tabs.slice(0, visibleStart);
var endHiddenTabs = tabs.slice(visibleEnd + 1);
var hiddenTabs = [].concat(_toConsumableArray(startHiddenTabs), _toConsumableArray(endHiddenTabs)); // =================== Link & Operations ===================
var _useState19 = useState(),
_useState20 = _slicedToArray(_useState19, 2),
inkStyle = _useState20[0],
setInkStyle = _useState20[1];
var activeTabOffset = tabOffsets.get(activeKey); // Delay set ink style to avoid remove tab blink
var inkBarRafRef = useRef();
function cleanInkBarRaf() {
raf.cancel(inkBarRafRef.current);
}
useEffect(function () {
var newInkStyle = {};
if (activeTabOffset) {
if (tabPositionTopOrBottom) {
if (rtl) {
newInkStyle.right = activeTabOffset.right;
} else {
newInkStyle.left = activeTabOffset.left;
}
newInkStyle.width = activeTabOffset.width;
} else {
newInkStyle.top = activeTabOffset.top;
newInkStyle.height = activeTabOffset.height;
}
}
cleanInkBarRaf();
inkBarRafRef.current = raf(function () {
setInkStyle(newInkStyle);
});
return cleanInkBarRaf;
}, [activeTabOffset, tabPositionTopOrBottom, rtl]); // ========================= Effect ========================
useEffect(function () {
scrollToTab();
}, [activeKey, activeTabOffset, tabOffsets, tabPositionTopOrBottom]); // Should recalculate when rtl changed
useEffect(function () {
onListHolderResize();
}, [rtl, tabBarGutter, activeKey, tabs.map(function (tab) {
return tab.key;
}).join('_')]); // ========================= Render ========================
var hasDropdown = !!hiddenTabs.length;
var wrapPrefix = "".concat(prefixCls, "-nav-wrap");
var pingLeft;
var pingRight;
var pingTop;
var pingBottom;
if (tabPositionTopOrBottom) {
if (rtl) {
pingRight = transformLeft > 0;
pingLeft = transformLeft + wrapperWidth < wrapperScrollWidth;
} else {
pingLeft = transformLeft < 0;
pingRight = -transformLeft + wrapperWidth < wrapperScrollWidth;
}
} else {
pingTop = transformTop < 0;
pingBottom = -transformTop + wrapperHeight < wrapperScrollHeight;
}
/* eslint-disable jsx-a11y/interactive-supports-focus */
return /*#__PURE__*/React.createElement("div", {
ref: ref,
role: "tablist",
className: classNames("".concat(prefixCls, "-nav"), className),
style: style,
onKeyDown: function onKeyDown() {
// No need animation when use keyboard
doLockAnimation();
}
}, /*#__PURE__*/React.createElement(ExtraContent, {
position: "left",
extra: extra,
prefixCls: prefixCls
}), /*#__PURE__*/React.createElement(ResizeObserver, {
onResize: onListHolderResize
}, /*#__PURE__*/React.createElement("div", {
className: classNames(wrapPrefix, (_classNames = {}, _defineProperty(_classNames, "".concat(wrapPrefix, "-ping-left"), pingLeft), _defineProperty(_classNames, "".concat(wrapPrefix, "-ping-right"), pingRight), _defineProperty(_classNames, "".concat(wrapPrefix, "-ping-top"), pingTop), _defineProperty(_classNames, "".concat(wrapPrefix, "-ping-bottom"), pingBottom), _classNames)),
ref: tabsWrapperRef
}, /*#__PURE__*/React.createElement(ResizeObserver, {
onResize: onListHolderResize
}, /*#__PURE__*/React.createElement("div", {
ref: tabListRef,
className: "".concat(prefixCls, "-nav-list"),
style: {
transform: "translate(".concat(transformLeft, "px, ").concat(transformTop, "px)"),
transition: lockAnimation ? 'none' : undefined
}
}, tabNodes, /*#__PURE__*/React.createElement(AddButton, {
ref: innerAddButtonRef,
prefixCls: prefixCls,
locale: locale,
editable: editable,
style: {
visibility: hasDropdown ? 'hidden' : null
}
}), /*#__PURE__*/React.createElement("div", {
className: classNames("".concat(prefixCls, "-ink-bar"), _defineProperty({}, "".concat(prefixCls, "-ink-bar-animated"), animated.inkBar)),
style: inkStyle
}))))), /*#__PURE__*/React.createElement(OperationNode, Object.assign({}, props, {
ref: operationsRef,
prefixCls: prefixCls,
tabs: hiddenTabs,
className: !hasDropdown && operationsHiddenClassName
})), /*#__PURE__*/React.createElement(ExtraContent, {
position: "right",
extra: extra,
prefixCls: prefixCls
}));
/* eslint-enable */
}
export default /*#__PURE__*/React.forwardRef(TabNavList);