import * as React from 'react' import CommonTab from '../../components/common/commonTab'; interface Props { changeContent: Function, channelData: any, setDefatuleChannel: Function, tabs: any[], tabValueField:string, tabTextField:string, onChangeTab: Function } interface State { hidden: boolean, activeCode: string, tabs: any[], isFetching: boolean; tabEleW: number } declare global { interface Window { MutationObserver: any; } } export default class HeaderTab extends React.PureComponent{ activeKey: number; constructor(props: Props) { super(props); this.state = { hidden: false, activeCode: '', tabs: [], isFetching: true, tabEleW: 0 } } willUnmount = false; resizeTimer:any; tab:any; observerLeft: any; observerRight:any; componentWillUnmount() { this.willUnmount = true } componentDidMount(){ this.setState({ isFetching: false, tabEleW:this.tabEle ? this.tabEle.clientWidth : 940, tabs: this.getTabItems(this.props.tabs) }) } getTabItems = (tabData: any) => { const {tabTextField,tabValueField} = this.props const tabs: any[] = []; let { activeCode } = this.state; activeCode = activeCode || tabData[0][tabValueField]; let newActiveCode = activeCode; tabData.forEach((item: any, index: number) => { if (!activeCode && index == 0) newActiveCode = item[tabValueField]; tabs.push({ idx: item[tabValueField], title: item[tabTextField], active: item[tabValueField] == newActiveCode ? true : false }) }) if (newActiveCode != activeCode) { this.setState({ activeCode: newActiveCode }) } return tabs; } handelClick = (idx: any, event: any) => { let { tabs, activeCode } = this.state; let newTabItems: any = []; let title; tabs.forEach((i, k) => { if (i.idx === idx) { activeCode = i.idx; title = i.title; } newTabItems.push({ active: i.idx === idx, title: i.title, idx: i.idx }) }); this.setState({ tabs: newTabItems, activeCode }) this.props.onChangeTab(activeCode,title); } tabEle: any render() { const { isFetching, tabEleW } = this.state; return
{ this.tabEle = node }}> { isFetching ? null : {this.tab = tab}} {...this.state} containerWidth={tabEleW} callBack={this.handelClick} /> }
} }