UNPKG

10.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
12
13var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
14
15var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16
17var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28
29var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30
31var _inherits2 = require('babel-runtime/helpers/inherits');
32
33var _inherits3 = _interopRequireDefault(_inherits2);
34
35var _simpleAssign = require('simple-assign');
36
37var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
38
39var _react = require('react');
40
41var _react2 = _interopRequireDefault(_react);
42
43var _propTypes = require('prop-types');
44
45var _propTypes2 = _interopRequireDefault(_propTypes);
46
47var _warning = require('warning');
48
49var _warning2 = _interopRequireDefault(_warning);
50
51var _TabTemplate = require('./TabTemplate');
52
53var _TabTemplate2 = _interopRequireDefault(_TabTemplate);
54
55var _InkBar = require('./InkBar');
56
57var _InkBar2 = _interopRequireDefault(_InkBar);
58
59function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
60
61function getStyles(props, context) {
62 var tabs = context.muiTheme.tabs;
63
64
65 return {
66 tabItemContainer: {
67 width: '100%',
68 backgroundColor: tabs.backgroundColor,
69 whiteSpace: 'nowrap',
70 display: 'flex'
71 }
72 };
73}
74
75var Tabs = function (_Component) {
76 (0, _inherits3.default)(Tabs, _Component);
77
78 function Tabs() {
79 var _ref;
80
81 var _temp, _this, _ret;
82
83 (0, _classCallCheck3.default)(this, Tabs);
84
85 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
86 args[_key] = arguments[_key];
87 }
88
89 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Tabs.__proto__ || (0, _getPrototypeOf2.default)(Tabs)).call.apply(_ref, [this].concat(args))), _this), _this.state = { selectedIndex: 0 }, _this.handleTabClick = function (value, event, tab) {
90 var valueLink = _this.getValueLink(_this.props);
91 var index = tab.props.index;
92
93 if (valueLink.value && valueLink.value !== value || _this.state.selectedIndex !== index) {
94 valueLink.requestChange(value, event, tab);
95 }
96
97 _this.setState({ selectedIndex: index });
98
99 if (tab.props.onActive) {
100 tab.props.onActive(tab);
101 }
102 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
103 }
104
105 (0, _createClass3.default)(Tabs, [{
106 key: 'componentWillMount',
107 value: function componentWillMount() {
108 var valueLink = this.getValueLink(this.props);
109 var initialIndex = this.props.initialSelectedIndex;
110
111 this.setState({
112 selectedIndex: valueLink.value !== undefined ? this.getSelectedIndex(this.props) : initialIndex < this.getTabCount() ? initialIndex : 0
113 });
114 }
115 }, {
116 key: 'componentWillReceiveProps',
117 value: function componentWillReceiveProps(newProps, nextContext) {
118 var valueLink = this.getValueLink(newProps);
119 var newState = {
120 muiTheme: nextContext.muiTheme || this.context.muiTheme
121 };
122
123 if (valueLink.value !== undefined) {
124 newState.selectedIndex = this.getSelectedIndex(newProps);
125 }
126
127 this.setState(newState);
128 }
129 }, {
130 key: 'getTabs',
131 value: function getTabs() {
132 var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props;
133
134 var tabs = [];
135
136 _react.Children.forEach(props.children, function (tab) {
137 if ((0, _react.isValidElement)(tab)) {
138 tabs.push(tab);
139 }
140 });
141
142 return tabs;
143 }
144 }, {
145 key: 'getTabCount',
146 value: function getTabCount() {
147 return this.getTabs().length;
148 }
149
150 // Do not use outside of this component, it will be removed once valueLink is deprecated
151
152 }, {
153 key: 'getValueLink',
154 value: function getValueLink(props) {
155 return props.valueLink || {
156 value: props.value,
157 requestChange: props.onChange
158 };
159 }
160 }, {
161 key: 'getSelectedIndex',
162 value: function getSelectedIndex(props) {
163 var valueLink = this.getValueLink(props);
164 var selectedIndex = -1;
165
166 this.getTabs(props).forEach(function (tab, index) {
167 if (valueLink.value === tab.props.value) {
168 selectedIndex = index;
169 }
170 });
171
172 return selectedIndex;
173 }
174 }, {
175 key: 'getSelected',
176 value: function getSelected(tab, index) {
177 var valueLink = this.getValueLink(this.props);
178 return valueLink.value ? valueLink.value === tab.props.value : this.state.selectedIndex === index;
179 }
180 }, {
181 key: 'render',
182 value: function render() {
183 var _this2 = this;
184
185 var _props = this.props,
186 contentContainerClassName = _props.contentContainerClassName,
187 contentContainerStyle = _props.contentContainerStyle,
188 initialSelectedIndex = _props.initialSelectedIndex,
189 inkBarStyle = _props.inkBarStyle,
190 inkBarContainerStyle = _props.inkBarContainerStyle,
191 onChange = _props.onChange,
192 style = _props.style,
193 tabItemContainerStyle = _props.tabItemContainerStyle,
194 tabTemplate = _props.tabTemplate,
195 tabTemplateStyle = _props.tabTemplateStyle,
196 other = (0, _objectWithoutProperties3.default)(_props, ['contentContainerClassName', 'contentContainerStyle', 'initialSelectedIndex', 'inkBarStyle', 'inkBarContainerStyle', 'onChange', 'style', 'tabItemContainerStyle', 'tabTemplate', 'tabTemplateStyle']);
197 var prepareStyles = this.context.muiTheme.prepareStyles;
198
199 var styles = getStyles(this.props, this.context);
200 var valueLink = this.getValueLink(this.props);
201 var tabValue = valueLink.value;
202 var tabContent = [];
203 var width = 100 / this.getTabCount();
204
205 var tabs = this.getTabs().map(function (tab, index) {
206 process.env.NODE_ENV !== "production" ? (0, _warning2.default)(tab.type && tab.type.muiName === 'Tab', 'Material-UI: Tabs only accepts Tab Components as children.\n Found ' + (tab.type.muiName || tab.type) + ' as child number ' + (index + 1) + ' of Tabs') : void 0;
207
208 process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!tabValue || tab.props.value !== undefined, 'Material-UI: Tabs value prop has been passed, but Tab ' + index + '\n does not have a value prop. Needs value if Tabs is going\n to be a controlled component.') : void 0;
209
210 tabContent.push(tab.props.children ? (0, _react.createElement)(tabTemplate || _TabTemplate2.default, {
211 key: index,
212 selected: _this2.getSelected(tab, index),
213 style: tabTemplateStyle
214 }, tab.props.children) : undefined);
215
216 return (0, _react.cloneElement)(tab, {
217 key: index,
218 index: index,
219 selected: _this2.getSelected(tab, index),
220 width: width + '%',
221 onClick: _this2.handleTabClick
222 });
223 });
224
225 var realSelectedIndex = valueLink.value ? this.getSelectedIndex(this.props) : this.state.selectedIndex;
226
227 var inkBar = realSelectedIndex !== -1 ? _react2.default.createElement(_InkBar2.default, {
228 left: width * realSelectedIndex + '%',
229 width: width + '%',
230 style: inkBarStyle
231 }) : null;
232
233 var inkBarContainerWidth = tabItemContainerStyle ? tabItemContainerStyle.width : '100%';
234
235 return _react2.default.createElement(
236 'div',
237 (0, _extends3.default)({ style: prepareStyles((0, _simpleAssign2.default)({}, style)) }, other),
238 _react2.default.createElement(
239 'div',
240 { style: prepareStyles((0, _simpleAssign2.default)(styles.tabItemContainer, tabItemContainerStyle)) },
241 tabs
242 ),
243 _react2.default.createElement(
244 'div',
245 { style: prepareStyles((0, _simpleAssign2.default)({ width: inkBarContainerWidth }, inkBarContainerStyle)) },
246 inkBar
247 ),
248 _react2.default.createElement(
249 'div',
250 {
251 style: prepareStyles((0, _simpleAssign2.default)({}, contentContainerStyle)),
252 className: contentContainerClassName
253 },
254 tabContent
255 )
256 );
257 }
258 }]);
259 return Tabs;
260}(_react.Component);
261
262Tabs.defaultProps = {
263 initialSelectedIndex: 0,
264 onChange: function onChange() {}
265};
266Tabs.contextTypes = {
267 muiTheme: _propTypes2.default.object.isRequired
268};
269Tabs.propTypes = process.env.NODE_ENV !== "production" ? {
270 /**
271 * Should be used to pass `Tab` components.
272 */
273 children: _propTypes2.default.node,
274 /**
275 * The css class name of the root element.
276 */
277 className: _propTypes2.default.string,
278 /**
279 * The css class name of the content's container.
280 */
281 contentContainerClassName: _propTypes2.default.string,
282 /**
283 * Override the inline-styles of the content's container.
284 */
285 contentContainerStyle: _propTypes2.default.object,
286 /**
287 * Specify initial visible tab index.
288 * If `initialSelectedIndex` is set but larger than the total amount of specified tabs,
289 * `initialSelectedIndex` will revert back to default.
290 * If `initialSelectedIndex` is set to any negative value, no tab will be selected intially.
291 */
292 initialSelectedIndex: _propTypes2.default.number,
293 /**
294 * Override the inline-styles of the InkBar.
295 */
296 inkBarStyle: _propTypes2.default.object,
297 /**
298 * Override the inline-styles of the InkBar container.
299 */
300 inkBarContainerStyle: _propTypes2.default.object,
301 /**
302 * Called when the selected value change.
303 */
304 onChange: _propTypes2.default.func,
305 /**
306 * Override the inline-styles of the root element.
307 */
308 style: _propTypes2.default.object,
309 /**
310 * Override the inline-styles of the tab-labels container.
311 */
312 tabItemContainerStyle: _propTypes2.default.object,
313 /**
314 * Override the default tab template used to wrap the content of each tab element.
315 */
316 tabTemplate: _propTypes2.default.func,
317 /**
318 * Override the inline-styles of the tab template.
319 */
320 tabTemplateStyle: _propTypes2.default.object,
321 /**
322 * Makes Tabs controllable and selects the tab whose value prop matches this prop.
323 */
324 value: _propTypes2.default.any
325} : {};
326exports.default = Tabs;
\No newline at end of file