-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathTabs.tsx
More file actions
231 lines (207 loc) · 6.01 KB
/
Tabs.tsx
File metadata and controls
231 lines (207 loc) · 6.01 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
// Accessibility https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Tab_Role
import { clsx } from 'clsx';
import useControlledState from '@rc-component/util/lib/hooks/useControlledState';
import isMobile from '@rc-component/util/lib/isMobile';
import * as React from 'react';
import { useEffect, useState } from 'react';
import TabContext from './TabContext';
import type { TabContextProps } from './TabContext';
import TabNavListWrapper from './TabNavList/Wrapper';
import TabPanelList from './TabPanelList';
import useAnimateConfig from './hooks/useAnimateConfig';
import type { GetIndicatorSize } from './hooks/useIndicator';
import type {
AnimatedConfig,
EditableConfig,
MoreProps,
OnTabScroll,
RenderTabBar,
Tab,
TabBarExtraContent,
TabPosition,
TabsLocale,
} from './interface';
/**
* Should added antd:
* - type
*
* Removed:
* - onNextClick
* - onPrevClick
* - keyboard
*/
// Used for accessibility
let uuid = 0;
export type SemanticName = 'popup' | 'item' | 'indicator' | 'content' | 'header' | 'remove';
export interface TabsProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'children'> {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
id?: string;
items?: Tab[];
activeKey?: string;
defaultActiveKey?: string;
direction?: 'ltr' | 'rtl';
animated?: boolean | AnimatedConfig;
renderTabBar?: RenderTabBar;
tabBarExtraContent?: TabBarExtraContent;
tabBarGutter?: number;
tabBarStyle?: React.CSSProperties;
tabPosition?: TabPosition;
destroyOnHidden?: boolean;
onChange?: (activeKey: string) => void;
onTabClick?: (activeKey: string, e: React.KeyboardEvent | React.MouseEvent) => void;
onTabScroll?: OnTabScroll;
editable?: EditableConfig;
getPopupContainer?: (node: HTMLElement) => HTMLElement;
// Accessibility
locale?: TabsLocale;
// Icons
more?: MoreProps;
/** @private Internal usage. Not promise will rename in future */
popupClassName?: string;
indicator?: {
size?: GetIndicatorSize;
align?: 'start' | 'center' | 'end';
};
}
const Tabs = React.forwardRef<HTMLDivElement, TabsProps>((props, ref) => {
const {
id,
prefixCls = 'rc-tabs',
className,
items,
direction,
activeKey,
defaultActiveKey,
editable,
animated,
tabPosition = 'top',
tabBarGutter,
tabBarStyle,
tabBarExtraContent,
locale,
more,
destroyOnHidden,
renderTabBar,
onChange,
onTabClick,
onTabScroll,
getPopupContainer,
popupClassName,
indicator,
classNames: tabsClassNames,
styles,
...restProps
} = props;
const tabs = React.useMemo<Tab[]>(
() => (items || []).filter(item => item && typeof item === 'object' && 'key' in item),
[items],
);
const rtl = direction === 'rtl';
const mergedAnimated = useAnimateConfig(animated);
// ======================== Mobile ========================
const [mobile, setMobile] = useState(false);
useEffect(() => {
// Only update on the client side
setMobile(isMobile());
}, []);
// ====================== Active Key ======================
const [mergedActiveKey, setMergedActiveKey] = useControlledState<string>(
defaultActiveKey ?? tabs[0]?.key,
activeKey,
);
const [activeIndex, setActiveIndex] = useState(() =>
tabs.findIndex(tab => tab.key === mergedActiveKey),
);
// Reset active key if not exist anymore
useEffect(() => {
let newActiveIndex = tabs.findIndex(tab => tab.key === mergedActiveKey);
if (newActiveIndex === -1) {
newActiveIndex = Math.max(0, Math.min(activeIndex, tabs.length - 1));
setMergedActiveKey(tabs[newActiveIndex]?.key);
}
setActiveIndex(newActiveIndex);
}, [tabs.map(tab => tab.key).join('_'), mergedActiveKey, activeIndex]);
// ===================== Accessibility ====================
const [mergedId, setMergedId] = useControlledState(null, id);
// Async generate id to avoid ssr mapping failed
useEffect(() => {
if (!id) {
setMergedId(`rc-tabs-${process.env.NODE_ENV === 'test' ? 'test' : uuid}`);
uuid += 1;
}
}, []);
// ======================== Events ========================
function onInternalTabClick(key: string, e: React.MouseEvent | React.KeyboardEvent) {
onTabClick?.(key, e);
const isActiveChanged = key !== mergedActiveKey;
setMergedActiveKey(key);
if (isActiveChanged) {
onChange?.(key);
}
}
// ======================== Render ========================
const sharedProps = {
id: mergedId,
activeKey: mergedActiveKey,
animated: mergedAnimated,
tabPosition,
rtl,
mobile,
};
const tabNavBarProps = {
...sharedProps,
editable,
locale,
more,
tabBarGutter,
onTabClick: onInternalTabClick,
onTabScroll,
extra: tabBarExtraContent,
style: tabBarStyle,
getPopupContainer,
popupClassName: clsx(popupClassName, tabsClassNames?.popup),
indicator,
styles,
classNames: tabsClassNames,
};
const memoizedValue = React.useMemo<TabContextProps>(() => {
return { tabs, prefixCls };
}, [tabs, prefixCls]);
return (
<TabContext.Provider value={memoizedValue}>
<div
ref={ref}
id={id}
className={clsx(
prefixCls,
`${prefixCls}-${tabPosition}`,
{
[`${prefixCls}-mobile`]: mobile,
[`${prefixCls}-editable`]: editable,
[`${prefixCls}-rtl`]: rtl,
},
className,
)}
{...restProps}
>
<TabNavListWrapper {...tabNavBarProps} renderTabBar={renderTabBar} />
<TabPanelList
destroyOnHidden={destroyOnHidden}
{...sharedProps}
contentStyle={styles?.content}
contentClassName={tabsClassNames?.content}
animated={mergedAnimated}
/>
</div>
</TabContext.Provider>
);
});
if (process.env.NODE_ENV !== 'production') {
Tabs.displayName = 'Tabs';
}
export default Tabs;