import React from 'react';
import PropTypes from 'prop-types';
class Tabs extends React.Component {
constructor(props) {
super(props);
this.state = { activeTab: this.props.defaultTab };
this.changeTab = this.changeTab.bind(this);
}
changeTab(id) {
this.setState({ activeTab: id });
}
render() {
const tabs = this.props.tabs.map((tab, t) => {
const activeness = `${((typeof this.state.activeTab === 'number' && this.state.activeTab === t) || (typeof this.state.activeTab === 'string' && this.state.activeTab === tab.id)) ? ' active' : ' disabled'}`;
return (
<li className={activeness} key={`tab-${t}`}>
<a
href={`#${tab.id}`}
data-toggle="tab"
onClick={() => {
this.changeTab(tab.id);
if (tab.onClick) { tab.onClick(); }
}}
>
{tab.subject}
</a>
</li>
);
});
const content = this.props.tabs.map((tab, t) => {
const activeness = ((typeof this.state.activeTab === 'number' && this.state.activeTab === t) || (typeof this.state.activeTab === 'string' && this.state.activeTab === tab.id)) ? ' active' : '';
return (
<div className={`tab-pane${activeness}`} id={tab.id} key={`tab-pane-${t}`}>
{tab.content}
</div>
);
});
const xs = this.props.widthXS || this.props.width;
const sm = this.props.widthSM || xs;
const md = this.props.widthMD || sm;
const lg = this.props.widthLG || md;
const xl = this.props.widthXL || lg;
return (
<div className={`col-xs-${xs} col-sm-${sm} col-md-${md} col-lg-${lg} col-xl-${xl}`}>
<div className="nav-tabs-custom">
<ul className={`nav nav-tabs${this.props.pullRight ? ' pull-right' : ''} ${this.props.tabTheme}`}>
{tabs}
{this.props.children}
</ul>
<div className="tab-content">
{content}
</div>
</div>
</div>
);
}
}
Tabs.propTypes = {
width: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
widthXS: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
widthSM: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
widthMD: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
widthLG: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
widthXL: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
pullRight: PropTypes.bool,
tabs: PropTypes.array,
defaultTab: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string
]),
tabTheme: PropTypes.string
};
Tabs.defaultProps = {
width: 12,
pullRight: false,
tabs: [],
defaultTab: 0,
tabTheme: 'tab-default'
};
export default Tabs;
|