import * as React from "react"; import { Pivot, PivotItem } from "@fluentui/react"; import "./TabComponent.less"; export interface TabContent { render: () => JSX.Element; className?: string; } export interface Tab { title: string; content: TabContent; isVisible: () => boolean; } interface TabComponentProps { tabs: Tab[]; currentTabIndex: number; onTabIndexChange: (newIndex: number) => void; hideHeader: boolean; } /** * We assume there's at least one tab */ export class TabComponent extends React.Component { public constructor(props: TabComponentProps) { super(props); if (this.props.tabs.length < 1) { const msg = "TabComponent must have at least one tab"; console.error(msg); throw new Error(msg); } } private setActiveTab(index: number): void { this.props.onTabIndexChange(index); } public render(): JSX.Element { const { tabs, currentTabIndex, hideHeader } = this.props; const currentTabContent = tabs[currentTabIndex].content; let className = "tabComponentContent"; if (currentTabContent.className) { className += ` ${currentTabContent.className}`; } return (
{!hideHeader && ( this.setActiveTab(parseInt(item?.props.itemKey || ""))} > {tabs.map((tab: Tab, index: number) => { if (!tab.isVisible()) { return null; // Skip rendering invisible tabs } return ; })} )}
{tabs[currentTabIndex].content.render()}
); } }