All files Tabs.jsx

100% Statements 14/14
100% Branches 0/0
100% Functions 9/9
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60            4x   4x 4x 8x     4x           4x   2x       1x 2x     1x           1x 2x                       1x                      
import React, { Component } from 'react'
 
import Tab from './Tab'
 
export default class Tabs extends Component {
    constructor(props) {
        super(props);
 
        this.tabComponents = React.Children.toArray(props.children);
        const tabState = this.tabComponents.map((item) => {
            return item.props.isActive;
        });
 
        this.state = {
            tabState: tabState
        }
    }
 
    _renderActiveTabContent() {
        const activeTabIndex = this.state.tabState.findIndex(x => x);
 
        return this.tabComponents[activeTabIndex].props.children;
    }
 
    onTabChange(tabIndex) {
        const newTabState = this.state.tabState.map((item, index) => {
            return index === tabIndex;
        });
 
        this.setState({
            tabState: newTabState
        });
    }
 
    _renderTabComponents() {
        return this.tabComponents.map((item, index) => {
            return (
                <Tab key={`tab${index}`} 
                     isActive={this.state.tabState[index]} 
                     label={item.props.label} 
                     onClick={this.onTabChange.bind(this, index)}
                     url={item.props.url}
                     />
            );
        });
    }
 
    render() {
        return (
            <div className="page-filter-tab">
                <div className="header-tab">
                    <div className="content-tab">
                        <ul className="tabs">{this._renderTabComponents()}</ul>
                    </div>
                </div>
                <div className="tab-panel">{this._renderActiveTabContent()}</div>
            </div>
        );
    }
}