import React from 'react'; import cx from 'classnames'; import bem from '../utils/bem'; import Item, { ItemProps } from './Item'; const b = bem('rdf-tab'); type Indicator = React.ReactNode | ((item: ItemProps) => React.ReactNode); export interface TabProps { items?: ItemProps[]; active?: any; defaultActive?: any; indicator?: Indicator; stretch?: boolean; vertical?: boolean; onActiveChange?: (item: ItemProps) => void; className?: string; style?: React.CSSProperties; } export interface TabState { active?: any; } export class Tab extends React.Component { static Item: typeof Item; static getDerivedStateFromProps(props: TabProps): TabState | null { if ('active' in props) { return { active: props.active }; } return null; } state: TabState = { active: 'active' in this.props ? this.props.active : this.props.defaultActive, }; getIndicator(indicator: Indicator, item: ItemProps): React.ReactNode { if (typeof indicator === 'function') { return indicator(item); } return indicator; } handleItemClick: ItemProps['onClick'] = item => { const { onActiveChange } = this.props; if (item.menuItems) { return; } if (!('active' in this.props)) { this.setState({ active: item.value }); } if (onActiveChange) { onActiveChange(item); } }; renderItems() { const { items, stretch, indicator, children } = this.props; const { active } = this.state; if (items) { return items.map(item => { const { value, indicator: itemIndicator } = item; return ( ); }); } return React.Children.map(children, (item: any) => { if (!item) { return null; } const { value, indicator: itemIndicator } = item.props; return ( ); }); } render() { const { className, style } = this.props; const items = this.renderItems(); if (!items) { return null; } return (
{items}
{items.map((item: any) => { const { value, active, children } = item.props; return ( active && children && (
{children}
) ); })}
); } } export default Tab;