import _ from "lodash" import React, { ReactNode } from "react" /** Single tab of a tabbed component */ export interface TabbedComponentTab { id: string label: ReactNode elem: ReactNode onRemove?: () => void } export interface TabbedComponentProps { /** Array of { id, label, elem, onRemove (optional) } */ tabs: TabbedComponentTab[] /** Initially selected id of tab */ initialTabId?: string /** Selected id of tab if controlled component */ tabId?: string /** Set to have a plus to add a tab */ onAddTab?: () => void /** Set to be called back when a tab is clicked (tabId) instead of setting internal state */ onTabClick?: (tabId: string) => void } /** Simple bootstrap tabbed component */ export default class TabbedComponent extends React.Component { constructor(props: any) { super(props) this.state = { tabId: this.props.initialTabId } } handleClick = (tabId: any) => { if (this.props.onTabClick != null) { return this.props.onTabClick(tabId) } else { return this.setState({ tabId }) } } handleRemove = (tab: any, ev: any) => { ev.stopPropagation() return tab.onRemove() } renderTab = (tab: any) => { let tabId if (this.props.tabId != null) { ;({ tabId } = this.props) } else { ;({ tabId } = this.state) } return (
  • {tab.label} {tab.onRemove ? ( ) : undefined}
  • ) } render() { let tabId if (this.props.tabId != null) { ;({ tabId } = this.props) } else { ;({ tabId } = this.state) } const currentTab = _.findWhere(this.props.tabs, { id: tabId }) return (
    {currentTab ? currentTab.elem : undefined}
    ) } }