import React from 'react'; import { NavLink, RouteComponentProps } from 'react-router-dom'; import Select, { Option } from 'react-select'; import './SubTabs.scss'; export interface Tab { name: string | React.ReactElement; path: string; disabled?: boolean; redirect?: string; } interface OwnProps { tabs: Tab[]; } type Props = OwnProps & RouteComponentProps<{}>; interface State { tabsWidth: number; isCollapsed: boolean; } export default class SubTabs extends React.PureComponent { public state: State = { tabsWidth: 0, isCollapsed: false }; private containerEl: HTMLDivElement | null; private tabsEl: HTMLDivElement | null; public componentDidMount() { this.measureTabsWidth(); window.addEventListener('resize', this.handleResize); } public componentWillUnmount() { window.removeEventListener('resize', this.handleResize); } public UNSAFE_componentWillReceiveProps(nextProps: Props) { // When new tabs come in, we'll need to uncollapse so that they can // be measured and collapsed again, if needed. if (this.props.tabs !== nextProps.tabs) { this.setState({ isCollapsed: false }); } } public componentDidUpdate(prevProps: Props) { // New tabs === new measurements if (this.props.tabs !== prevProps.tabs) { this.measureTabsWidth(); } } public render() { const { tabs, match } = this.props; const { isCollapsed } = this.state; const basePath = match.url; const currentPath = location.pathname; let content: React.ReactElement; if (isCollapsed) { const options = tabs.map(tab => ({ label: tab.name as string, value: tab.path, disabled: tab.disabled })); content = (