Skip to content

Commit 26d067c

Browse files
committed
feat: Tabs - allowEmpty
1 parent 2c00231 commit 26d067c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/components/Tabs/Tabs.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function Tabs(props: T.Props) {
1717
contentClassName,
1818
items,
1919
hideTabsIfSingle = false,
20+
allowEmpty = false,
2021
onChange,
2122
renderAll,
2223
activeId: initialId,
@@ -33,16 +34,21 @@ export function Tabs(props: T.Props) {
3334
}
3435
}, [initialId]);
3536

36-
const onTabClick = useCallback((e, { id, onClick } = {} as T.Item) => {
37-
// @ts-ignore
38-
if (onClick && !onClick(e)) {
39-
e.peventDefault();
40-
return;
41-
}
37+
const onTabClick = useCallback(
38+
(e, { id, onClick } = {} as T.Item) => {
39+
// @ts-ignore
40+
if (onClick && !onClick(e)) {
41+
e.peventDefault();
42+
return;
43+
}
44+
45+
const newId = allowEmpty && id === activeId ? null : id;
4246

43-
setActiveId(id);
44-
onChange?.(id);
45-
}, []);
47+
setActiveId(newId);
48+
onChange?.(newId);
49+
},
50+
[activeId]
51+
);
4652

4753
const tabsContent = [];
4854
const tabsButtons = items.map((params: T.Item) => {

src/components/Tabs/Tabs.types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type Props = {
2323
contentClassName?: string;
2424
items: Item[];
2525
hideTabsIfSingle?: boolean;
26+
// Allow to unselect tab, so no content will be rendered
27+
allowEmpty?: boolean;
2628
activeId?: ID;
2729
onChange: (id: ID) => void;
2830
renderAll?: boolean;

0 commit comments

Comments
 (0)