Tabs.js
8.39 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
"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 _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
var _isMobile = _interopRequireDefault(require("rc-util/lib/isMobile"));
var _useMergedState5 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));
var _TabNavList = _interopRequireDefault(require("./TabNavList"));
var _TabPanelList = _interopRequireDefault(require("./TabPanelList"));
var _TabPane = _interopRequireDefault(require("./TabPanelList/TabPane"));
var _TabContext = _interopRequireDefault(require("./TabContext"));
// Accessibility https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Tab_Role
/**
* Should added antd:
* - type
*
* Removed:
* - onNextClick
* - onPrevClick
* - keyboard
*/
// Used for accessibility
var uuid = 0;
function parseTabList(children) {
return (0, _toArray.default)(children).map(function (node) {
if ( /*#__PURE__*/React.isValidElement(node)) {
var key = node.key !== undefined ? String(node.key) : undefined;
return (0, _objectSpread2.default)((0, _objectSpread2.default)({
key: key
}, node.props), {}, {
node: node
});
}
return null;
}).filter(function (tab) {
return tab;
});
}
function Tabs(_ref, ref) {
var _classNames;
var id = _ref.id,
_ref$prefixCls = _ref.prefixCls,
prefixCls = _ref$prefixCls === void 0 ? 'rc-tabs' : _ref$prefixCls,
className = _ref.className,
children = _ref.children,
direction = _ref.direction,
activeKey = _ref.activeKey,
defaultActiveKey = _ref.defaultActiveKey,
editable = _ref.editable,
_ref$animated = _ref.animated,
animated = _ref$animated === void 0 ? {
inkBar: true,
tabPane: false
} : _ref$animated,
_ref$tabPosition = _ref.tabPosition,
tabPosition = _ref$tabPosition === void 0 ? 'top' : _ref$tabPosition,
tabBarGutter = _ref.tabBarGutter,
tabBarStyle = _ref.tabBarStyle,
tabBarExtraContent = _ref.tabBarExtraContent,
locale = _ref.locale,
moreIcon = _ref.moreIcon,
moreTransitionName = _ref.moreTransitionName,
destroyInactiveTabPane = _ref.destroyInactiveTabPane,
renderTabBar = _ref.renderTabBar,
onChange = _ref.onChange,
onTabClick = _ref.onTabClick,
onTabScroll = _ref.onTabScroll,
restProps = (0, _objectWithoutProperties2.default)(_ref, ["id", "prefixCls", "className", "children", "direction", "activeKey", "defaultActiveKey", "editable", "animated", "tabPosition", "tabBarGutter", "tabBarStyle", "tabBarExtraContent", "locale", "moreIcon", "moreTransitionName", "destroyInactiveTabPane", "renderTabBar", "onChange", "onTabClick", "onTabScroll"]);
var tabs = parseTabList(children);
var rtl = direction === 'rtl';
var mergedAnimated;
if (animated === false) {
mergedAnimated = {
inkBar: false,
tabPane: false
};
} else if (animated === true) {
mergedAnimated = {
inkBar: true,
tabPane: true
};
} else {
mergedAnimated = (0, _objectSpread2.default)({
inkBar: true,
tabPane: false
}, (0, _typeof2.default)(animated) === 'object' ? animated : {});
} // ======================== Mobile ========================
var _useState = (0, React.useState)(false),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
mobile = _useState2[0],
setMobile = _useState2[1];
(0, React.useEffect)(function () {
// Only update on the client side
setMobile((0, _isMobile.default)());
}, []); // ====================== Active Key ======================
var _useMergedState = (0, _useMergedState5.default)(function () {
var _tabs$;
return (_tabs$ = tabs[0]) === null || _tabs$ === void 0 ? void 0 : _tabs$.key;
}, {
value: activeKey,
defaultValue: defaultActiveKey
}),
_useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 2),
mergedActiveKey = _useMergedState2[0],
setMergedActiveKey = _useMergedState2[1];
var _useState3 = (0, React.useState)(function () {
return tabs.findIndex(function (tab) {
return tab.key === mergedActiveKey;
});
}),
_useState4 = (0, _slicedToArray2.default)(_useState3, 2),
activeIndex = _useState4[0],
setActiveIndex = _useState4[1]; // Reset active key if not exist anymore
(0, React.useEffect)(function () {
var newActiveIndex = tabs.findIndex(function (tab) {
return tab.key === mergedActiveKey;
});
if (newActiveIndex === -1) {
var _tabs$newActiveIndex;
newActiveIndex = Math.max(0, Math.min(activeIndex, tabs.length - 1));
setMergedActiveKey((_tabs$newActiveIndex = tabs[newActiveIndex]) === null || _tabs$newActiveIndex === void 0 ? void 0 : _tabs$newActiveIndex.key);
}
setActiveIndex(newActiveIndex);
}, [tabs.map(function (tab) {
return tab.key;
}).join('_'), mergedActiveKey, activeIndex]); // ===================== Accessibility ====================
var _useMergedState3 = (0, _useMergedState5.default)(null, {
value: id
}),
_useMergedState4 = (0, _slicedToArray2.default)(_useMergedState3, 2),
mergedId = _useMergedState4[0],
setMergedId = _useMergedState4[1];
var mergedTabPosition = tabPosition;
if (mobile && !['left', 'right'].includes(tabPosition)) {
mergedTabPosition = 'top';
} // Async generate id to avoid ssr mapping failed
(0, React.useEffect)(function () {
if (!id) {
setMergedId("rc-tabs-".concat(process.env.NODE_ENV === 'test' ? 'test' : uuid));
uuid += 1;
}
}, []); // ======================== Events ========================
function onInternalTabClick(key, e) {
onTabClick === null || onTabClick === void 0 ? void 0 : onTabClick(key, e);
setMergedActiveKey(key);
onChange === null || onChange === void 0 ? void 0 : onChange(key);
} // ======================== Render ========================
var sharedProps = {
id: mergedId,
activeKey: mergedActiveKey,
animated: mergedAnimated,
tabPosition: mergedTabPosition,
rtl: rtl,
mobile: mobile
};
var tabNavBar;
var tabNavBarProps = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, sharedProps), {}, {
editable: editable,
locale: locale,
moreIcon: moreIcon,
moreTransitionName: moreTransitionName,
tabBarGutter: tabBarGutter,
onTabClick: onInternalTabClick,
onTabScroll: onTabScroll,
extra: tabBarExtraContent,
style: tabBarStyle,
panes: children
});
if (renderTabBar) {
tabNavBar = renderTabBar(tabNavBarProps, _TabNavList.default);
} else {
tabNavBar = /*#__PURE__*/React.createElement(_TabNavList.default, Object.assign({}, tabNavBarProps));
}
return /*#__PURE__*/React.createElement(_TabContext.default.Provider, {
value: {
tabs: tabs,
prefixCls: prefixCls
}
}, /*#__PURE__*/React.createElement("div", Object.assign({
ref: ref,
id: id,
className: (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(mergedTabPosition), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-mobile"), mobile), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-editable"), editable), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-rtl"), rtl), _classNames), className)
}, restProps), tabNavBar, /*#__PURE__*/React.createElement(_TabPanelList.default, Object.assign({
destroyInactiveTabPane: destroyInactiveTabPane
}, sharedProps, {
animated: mergedAnimated
}))));
}
var ForwardTabs = /*#__PURE__*/React.forwardRef(Tabs);
ForwardTabs.TabPane = _TabPane.default;
var _default = ForwardTabs;
exports.default = _default;