import * as React from "react"; import { cx } from "emotion"; import { TabsState, TabsProps, TabSectionProps } from "./typings/Tabs"; import { tabStyle, tabsWrap, selectedTabStyle } from "./styles/Tabs.styles"; const TabSelectedContext = /*@__PURE__*/ React.createContext( null ); export class Tabs extends React.PureComponent { state: Readonly = { selectedTab: this.props.initialSelectedTab || (this.props.tabs.length && this.props.tabs[0]) || "" }; static getDerivedStateFromProps(props: TabsProps): Partial | null { if (!props.selectedTab) { return null; } return { selectedTab: props.selectedTab }; } render() { const _className = cx(tabStyle, this.props.tabClassName); const { labels } = this.props; return (
{this.props.tabs.map((tab, i) => { const isSelected = this.state.selectedTab === tab; return ( { this.setState({ selectedTab: tab }); if (this.props.onTabChange) { this.props.onTabChange(tab); } }} > {labels && labels[tab] ? labels[tab] : tab} ); })}
{this.props.children}
); } } // tslint:disable-next-line:max-classes-per-file export class TabSection extends React.PureComponent { static context = TabSelectedContext; render() { const { section, children } = this.props; return ( {selectedTab => <>{section === selectedTab && children}} ); } }