import { ReactNode, CSSProperties } from "react" import NOOP from "../../modules/noop" import Title from "./Title" type TabsProps = { children?: ReactNode, tabs: {label: string, id: string}[], active?: string onChange?: (tab: string) => void, style?: CSSProperties } export default class Tabs extends React.Component { static defaultProps:TabsProps = { children: null, tabs: [{label: "No tabs was passed to .", id: "none"}], active: null, onChange: NOOP, style: {} } constructor(props:TabsProps){ super(props) this.state = { active: this.props.active || null } } tabsElements:Tab[] = [] get tabs():Tabs["props"]["tabs"]{ return this.props.tabs || [] } changeTab(tab:string){ if(tab === this.state.active)return if(this.props.onChange)this.props.onChange(tab) this.setState({ active: tab }) this.tabsElements.forEach(e => e.setActive(tab === e.props.id)) } render(){ return (
{this.tabs.map(tab => { return React.createElement(Tab, {TabContainer: this, title: tab.label, id: tab.id, key: Buffer.from(tab.label+":"+tab.id, "utf8").toString("base64")}) })}
{this.props.children}
) } isActive(tab){ return this.state.active === tab } static get AllPreviews(){ return AllPreviews || (() => { AllPreviews = [] AllPreviews.push([ { children: (
Preview tabs
) } ], [ { tabs: [ { label: "Active tab", id: "1" }, { label: "Unactive tab", id: "2" } ] } ], [ { active: "1" } ], [ { onChange: (tabId) => {} } ]) return AllPreviews })() } } let AllPreviews export class Tab extends React.Component<{ TabContainer: Tabs, title: string, id: string }, { active: boolean }> { constructor(props){ super(props) this.state = { active: props.TabContainer.isActive(props.id) } this.props.TabContainer.tabsElements.push(this) } setActive(isActive:boolean){ this.setState({ active: !!isActive }) } render(){ let className = `lc-navItem` if(this.state.active){ className += ` lc-navItemActive` }else{ className += ` lc-navItemInactive` } return (
{ this.props.TabContainer.changeTab(this.props.id) }}> {this.props.title}
) } }