import * as React from "react"; import { IElement } from "./basic"; import { classes } from "./helpers"; type ITabBar = IElement & { tabs: [any, React.ReactNode][]; onSelectionChanged?: (key: any) => void; selected?: any; }; export class TabBar extends React.Component { constructor(props: ITabBar) { super(props); this.state = { selected: props.selected }; } render() { return (
{this.props.tabs.map((x) => this.renderTab(x))}
); } renderTab([key, tab]: [any, React.ReactNode]) { return (
this.select(key)} > {tab}
); } select(key: any) { this.setState({ selected: key }); this.props.onSelectionChanged?.(key); } } export function TabView( props: IElement & { selected: any; tabs: [any, React.ReactNode][] } ) { return (
{props.tabs.map(([key, view]) => { return (
{view}
); })}
); } type ITabs = IElement & { tabs: { key: any; tab: React.ReactNode; content: React.ReactNode }[]; selected?: any; }; export class Tabs extends React.Component { constructor(props: ITabs) { super(props); this.state = { selected: props.selected ?? props.tabs[0]?.key }; } render() { return (
[x.key, x.tab] as [any, React.ReactNode] )} /> [x.key, x.content] as [any, React.ReactNode] )} />
); } selectionChanged = (x: any) => { this.setState({ selected: x }); }; }