forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.jsx
More file actions
117 lines (93 loc) · 2.43 KB
/
tabs.jsx
File metadata and controls
117 lines (93 loc) · 2.43 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* MUI React tabs module
* @module react/tabs
*/
/* jshint quotmark:false */
// jscs:disable validateQuoteMarks
'use strict';
import React from 'react';
import Tab from './tab';
import * as util from '../js/lib/util';
const PropTypes = React.PropTypes,
tabsBarClass = 'mui-tabs__bar',
tabsBarJustifiedClass = 'mui-tabs__bar--justified',
tabsPaneClass = 'mui-tabs__pane',
isActiveClass = 'mui--is-active';
/**
* Tabs constructor
* @class
*/
class Tabs extends React.Component {
constructor(props) {
super(props);
this.state = {currentSelectedIndex: props.initialSelectedIndex};
}
static propTypes = {
initialSelectedIndex: PropTypes.number,
justified: PropTypes.bool,
onChange: PropTypes.func
};
static defaultProps = {
className: '',
initialSelectedIndex: 0,
justified: false,
onChange: null
};
onClick(i, tab, ev) {
if (i !== this.state.currentSelectedIndex) {
this.setState({currentSelectedIndex: i});
// onActive callback
if (tab.props.onActive) tab.props.onActive(tab);
// onChange callback
if (this.props.onChange) {
this.props.onChange(i, tab.props.value, tab, ev);
}
}
}
render() {
const { children, initialSelectedIndex, justified,
...reactProps } = this.props;
let tabEls = [],
paneEls = [],
m = children.length,
selectedIndex = this.state.currentSelectedIndex % m,
isActive,
item,
cls,
i;
for (i=0; i < m; i++) {
item = children[i];
// only accept MUITab elements
if (item.type !== Tab) util.raiseError('Expecting MUITab React Element');
isActive = (i === selectedIndex) ? true : false;
// tab element
tabEls.push(
<li key={i} className={(isActive) ? isActiveClass : ''}>
<a onClick={this.onClick.bind(this, i, item)}>
{item.props.label}
</a>
</li>
);
// pane element
cls = tabsPaneClass + ' ';
if (isActive) cls += isActiveClass;
paneEls.push(
<div key={i} className={cls}>
{item.props.children}
</div>
);
}
cls = tabsBarClass;
if (justified) cls += ' ' + tabsBarJustifiedClass;
return (
<div { ...reactProps }>
<ul className={cls}>
{tabEls}
</ul>
{paneEls}
</div>
);
}
}
/** Define module API */
export default Tabs;