"use client"; import cx from "classnames"; import React from "react"; import { useStatic } from "../../utils/hooks"; import { Tab } from "./Tab"; import type { TabPanelProps } from "./TabPanel"; import TabsStatic from "./Tabs.static"; interface TabsProps extends React.HTMLAttributes { /** Active Tab index */ activeTabIndex?: number; /** Additional classes for tabs header. */ classesTabNav?: string; /** All tabs have equal width inside a full-width container */ hasEqualTabWidth?: boolean; /** Fullwidth container */ isFullWidth?: boolean; /** Tabs behave as links rather than tabs */ isLink?: boolean; /** Tab panels as children */ children: React.ReactElement[]; } const Tabs: React.FC = ({ activeTabIndex = 0, className, classesTabNav, isFullWidth, isLink, hasEqualTabWidth, children, ...other }) => { const CLASS_TABNAV = "tab-list"; const tabNavClasses = cx( CLASS_TABNAV, { [`${CLASS_TABNAV}--equal`]: hasEqualTabWidth }, { [`${CLASS_TABNAV}--fullwidth`]: isFullWidth }, classesTabNav, className, ); const [tabListRef] = useStatic(TabsStatic); const tabPanels = React.Children.toArray(children).filter( (tabPanel): tabPanel is React.ReactElement => React.isValidElement(tabPanel), ); function getTabs() { return tabPanels.map((tabPanel, i) => { const isActive = i === activeTabIndex; const props = tabPanel.props || {}; const { renderTab = undefined, id, isDisabled, tab, href, } = props as TabPanelProps; // Generate ID if not provided const tabId = id || `tab-${i}`; if (!renderTab) { return ( {tab} ); } const renderedTab = renderTab(props as TabPanelProps); return React.cloneElement(renderedTab, { key: tabId, isActive, } as React.HTMLAttributes); }); } function getPanels() { return tabPanels.map((tabPanel, i) => { const isActive = i === activeTabIndex; const props = tabPanel.props || {}; const { id } = props; const tabId = id || `tab-${i}`; return React.cloneElement(tabPanel, { key: tabId, isActive, tab: undefined, } as Partial); }); } return ( <>
    {getTabs()} {isFullWidth &&
  • }
{!isLink ? getPanels() : null} ); }; Tabs.displayName = "Tabs"; export { Tabs };