@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.map((params) => {
25
- const { id, label, forceRender, content, contentClassName: currContentClassName, ...rest } = params;
26
- const isActive = activeId === id;
27
- const tabContent = typeof content === 'function' ? content() : content;
28
- if (renderAll || forceRender || isActive) {
29
- tabsContent.push(jsx("div", { className: cn(contentClassName, currContentClassName, !isActive && S.inactive), children: tabContent }, id));
30
- }
31
- return (createElement(Button, { ...rest, size: size, key: id, onClick: e => onTabClick(e, params), checked: isActive }, label));
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({
@@ -19,6 +19,7 @@ export type Props = {
19
19
  className?: string;
20
20
  contentClassName?: string;
21
21
  items: Item[];
22
+ hideTabsIfSingle?: boolean;
22
23
  activeId?: ID;
23
24
  onChange: (id: ID) => void;
24
25
  renderAll?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homecode/ui",
3
- "version": "4.18.21",
3
+ "version": "4.18.23",
4
4
  "description": "React UI components library",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",