import * as React from 'react'; import Item from './item'; import './index.scss'; // import { line } from '&/pages/BountySetting/bountyStatistics/lineChart/data'; interface Props { hidden: boolean; tabs: PropsItem[]; containerWidth: number; callBack: (idx: any, item: PropsItem) => void } interface PropsItem { idx: string, //string,标签的唯一标识名称 title: string, //string,标签内容 active: boolean, //boolean } export default class CommonTab extends React.PureComponent{ state = { dropDownTabs: [], showMoreList: false, showTabsWidth: this.props.containerWidth, showMoreBtnActive: false, activeIdx: 0 } refs: any itemWith: any = [] activeIndex = 0; getItemWidth = (active: boolean, index: number, width: number) => { if (active) this.activeIndex = index; this.itemWith[index] = width; if (this.itemWith.length == this.props.tabs.length) { this.setDropdownTabs() } } componentWillReceiveProps() { this.itemWith = []; } setDropdownTabs = () => { let tabsWidth = 0, { containerWidth, tabs } = this.props, firstInDropdownIndex = -1; this.itemWith.forEach((item: number, index: number) => { tabsWidth += item; if (tabsWidth + 180 > containerWidth && firstInDropdownIndex == -1) { console.log('tabsWidth', tabsWidth) firstInDropdownIndex = index; this.setState({ dropDownTabs: tabs.slice(firstInDropdownIndex + 1), showTabsWidth: tabsWidth + 2 }) } }); } handleMoreBtnClick = (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); this.setState({ showMoreList: true }) } componentDidMount() { document.addEventListener('click', this.hideDropdownList, false); } componentWillUnmount() { document.removeEventListener('click', this.hideDropdownList, false); } handleDropdownTabsClick = (idx: any, item: any) => (e: React.MouseEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); const { callBack } = this.props; callBack && callBack(idx, item) if (this.state.dropDownTabs.find((item: any) => item.idx == idx)) { this.setState({ showMoreBtnActive: true, activeIdx: idx }) } else { this.setState({ showMoreBtnActive: false, activeIdx: idx }) } this.hideDropdownList(); } hideDropdownList = () => { this.setState({ showMoreList: false }) } render() { const { hidden, tabs } = this.props, { dropDownTabs, showMoreList, showTabsWidth, showMoreBtnActive, activeIdx } = this.state; return } }