@homecode/ui 4.18.21 → 4.18.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState, useCallback, createElement } from 'react';
|
|
1
|
+
import { useState, useEffect, useCallback, createElement } from 'react';
|
|
2
2
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
import cn from 'classnames';
|
|
4
4
|
import { Button } from '../Button/Button.js';
|
|
@@ -9,8 +9,13 @@ function isId(id) {
|
|
|
9
9
|
return ['string', 'number'].includes(typeof id);
|
|
10
10
|
}
|
|
11
11
|
function Tabs(props) {
|
|
12
|
-
const { size = 'm', className, contentClassName, items, onChange, renderAll, activeId: initialId, children, ...rest } = props;
|
|
12
|
+
const { size = 'm', className, contentClassName, items, hideTabsIfSingle = true, onChange, renderAll, activeId: initialId, children, ...rest } = props;
|
|
13
13
|
const [activeId, setActiveId] = useState(isId(initialId) ? initialId : items[0].id);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (isId(initialId)) {
|
|
16
|
+
setActiveId(initialId);
|
|
17
|
+
}
|
|
18
|
+
}, [initialId]);
|
|
14
19
|
const onTabClick = useCallback((e, { id, onClick } = {}) => {
|
|
15
20
|
// @ts-ignore
|
|
16
21
|
if (onClick && !onClick(e)) {
|
|
@@ -21,15 +26,17 @@ function Tabs(props) {
|
|
|
21
26
|
onChange?.(id);
|
|
22
27
|
}, []);
|
|
23
28
|
const tabsContent = [];
|
|
24
|
-
const tabsButtons = items.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
const tabsButtons = items.length === 1 && hideTabsIfSingle
|
|
30
|
+
? []
|
|
31
|
+
: items.map((params) => {
|
|
32
|
+
const { id, label, forceRender, content, contentClassName: currContentClassName, ...rest } = params;
|
|
33
|
+
const isActive = activeId === id;
|
|
34
|
+
const tabContent = typeof content === 'function' ? content() : content;
|
|
35
|
+
if (renderAll || forceRender || isActive) {
|
|
36
|
+
tabsContent.push(jsx("div", { className: cn(contentClassName, currContentClassName, !isActive && S.inactive), children: tabContent }, id));
|
|
37
|
+
}
|
|
38
|
+
return (createElement(Button, { ...rest, size: size, key: id, onClick: e => onTabClick(e, params), checked: isActive }, label));
|
|
39
|
+
});
|
|
33
40
|
const tabs = (jsx(ButtonGroup, { className: className, ...rest, children: tabsButtons }));
|
|
34
41
|
if (typeof children === 'function') {
|
|
35
42
|
return children({
|