forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
167 lines (135 loc) · 4.74 KB
/
tabs.js
File metadata and controls
167 lines (135 loc) · 4.74 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var babelHelpers = require('./babel-helpers.js');
/**
* MUI React tabs module
* @module react/tabs
*/
/* jshint quotmark:false */
// jscs:disable validateQuoteMarks
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = babelHelpers.interopRequireDefault(_react);
var _tab = require('./tab');
var _tab2 = babelHelpers.interopRequireDefault(_tab);
var _util = require('../js/lib/util');
var util = babelHelpers.interopRequireWildcard(_util);
var tabsBarClass = 'mui-tabs__bar',
tabsBarJustifiedClass = 'mui-tabs__bar--justified',
tabsPaneClass = 'mui-tabs__pane',
isActiveClass = 'mui--is-active';
/**
* Tabs constructor
* @class
*/
var Tabs = function (_React$Component) {
babelHelpers.inherits(Tabs, _React$Component);
function Tabs(props) {
babelHelpers.classCallCheck(this, Tabs);
/*
* 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.
*/
var defaultSelectedIndex = void 0;
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
*/
var _this = babelHelpers.possibleConstructorReturn(this, (Tabs.__proto__ || Object.getPrototypeOf(Tabs)).call(this, props));
_this.state = { currentSelectedIndex: typeof props.selectedIndex === 'number' ? props.selectedIndex : defaultSelectedIndex };
return _this;
}
babelHelpers.createClass(Tabs, [{
key: 'onClick',
value: function 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);
}
}
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
children = _props.children,
defaultSelectedIndex = _props.defaultSelectedIndex,
initialSelectedIndex = _props.initialSelectedIndex,
justified = _props.justified,
selectedIndex = _props.selectedIndex,
reactProps = babelHelpers.objectWithoutProperties(_props, ['children', 'defaultSelectedIndex', 'initialSelectedIndex', 'justified', 'selectedIndex']);
var tabs = _react2.default.Children.toArray(children);
var tabEls = [],
paneEls = [],
m = tabs.length,
currentSelectedIndex = (typeof selectedIndex === 'number' ? selectedIndex : this.state.currentSelectedIndex) % m,
isActive = void 0,
item = void 0,
cls = void 0,
i = void 0;
for (i = 0; i < m; i++) {
item = tabs[i];
// only accept MUITab elements
if (item.type !== _tab2.default) util.raiseError('Expecting MUITab React Element');
isActive = i === currentSelectedIndex ? true : false;
// tab element
tabEls.push(_react2.default.createElement(
'li',
{ key: i, className: isActive ? isActiveClass : '' },
_react2.default.createElement(
'a',
{ onClick: this.onClick.bind(this, i, item) },
item.props.label
)
));
// pane element
cls = tabsPaneClass + ' ';
if (isActive) cls += isActiveClass;
paneEls.push(_react2.default.createElement(
'div',
{ key: i, className: cls },
item.props.children
));
}
cls = tabsBarClass;
if (justified) cls += ' ' + tabsBarJustifiedClass;
return _react2.default.createElement(
'div',
reactProps,
_react2.default.createElement(
'ul',
{ className: cls },
tabEls
),
paneEls
);
}
}]);
return Tabs;
}(_react2.default.Component);
/** Define module API */
Tabs.defaultProps = {
className: '',
defaultSelectedIndex: 0,
/*
* @deprecated
*/
initialSelectedIndex: null,
justified: false,
onChange: null,
selectedIndex: null
};
exports.default = Tabs;
module.exports = exports['default'];