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
137 lines (114 loc) · 3.46 KB
/
tabs.jsx
File metadata and controls
137 lines (114 loc) · 3.46 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* 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 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) {
/*
* The following code exists only to warn about deprecating props.initialSelectedIndex in favor of props.defaultSelectedIndex.
* It can be removed once support for props.initialSelectedIndex is officially dropped.
*/
let defaultSelectedIndex;
if (typeof props.initialSelectedIndex === 'number') {
defaultSelectedIndex = props.initialSelectedIndex;
if (console && process && process.env && process.NODE_ENV !== 'production') {
console.warn(
'MUICSS DEPRECATION WARNING: '
+ 'property "initialSelectedIndex" on the muicss Tabs component is deprecated in favor of "defaultSelectedIndex". '
+ 'It will be removed in a future release.'
);
}
}
else {
defaultSelectedIndex = props.defaultSelectedIndex;
}
/*
* End deprecation warning
*/
super(props);
this.state = {currentSelectedIndex: typeof props.selectedIndex === 'number' ? props.selectedIndex : defaultSelectedIndex};
}
static defaultProps = {
className: '',
defaultSelectedIndex: 0,
/*
* @deprecated
*/
initialSelectedIndex: null,
justified: false,
onChange: null,
selectedIndex: null
};
onClick(i, tab, ev) {
if ((typeof this.props.selectedIndex === 'number' && i !== this.props.selectedIndex) || 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, defaultSelectedIndex, initialSelectedIndex, justified, selectedIndex,
...reactProps } = this.props;
let tabs = React.Children.toArray(children);
let tabEls = [],
paneEls = [],
m = tabs.length,
currentSelectedIndex = (typeof selectedIndex === 'number' ? selectedIndex : this.state.currentSelectedIndex) % m,
isActive,
item,
cls,
i;
for (i=0; i < m; i++) {
item = tabs[i];
// only accept MUITab elements
if (item.type !== Tab) util.raiseError('Expecting MUITab React Element');
isActive = (i === currentSelectedIndex) ? 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;