UNPKG

5.64 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _TabTemplate = require('./TabTemplate');
18
19var _TabTemplate2 = _interopRequireDefault(_TabTemplate);
20
21var _style = require('./style.scss');
22
23var _style2 = _interopRequireDefault(_style);
24
25var _bind = require('classnames/bind');
26
27var _bind2 = _interopRequireDefault(_bind);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
34
35function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
36
37var TabWrapper = function (_React$Component) {
38 _inherits(TabWrapper, _React$Component);
39
40 function TabWrapper(props) {
41 _classCallCheck(this, TabWrapper);
42
43 var _this = _possibleConstructorReturn(this, (TabWrapper.__proto__ || Object.getPrototypeOf(TabWrapper)).call(this, props));
44
45 _this.state = {
46 selectedIndex: _this.props.initialSelectedIndex < _this.props.children.length ? _this.props.initialSelectedIndex : 0
47 };
48
49 _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
50 if (nextProps.initialSelectedIndex !== _this.props.initialSelectedIndex) {
51 _this.setState({ selectedIndex: nextProps.initialSelectedIndex < nextProps.children.length ? nextProps.initialSelectedIndex : 0 });
52 }
53 };
54
55 _this.UNSAFE_componentWillMount = function () {
56 _this.getTabs().map(function (tab, index) {
57 if (tab.props.active) {
58 _this.setState({ selectedIndex: index });
59 }
60 });
61 };
62
63 _this.getTabs = function () {
64 var tabs = [];
65
66 _react2.default.Children.forEach(_this.props.children, function (tab) {
67 if (_react2.default.isValidElement(tab)) {
68 tabs.push(tab);
69 }
70 });
71 return tabs;
72 };
73
74 _this.isActive = function (index) {
75 return _this.state.selectedIndex === index;
76 };
77
78 _this.handleClick = function (event, tab) {
79 var tabIndex = tab.props.tabIndex;
80
81 if (_this.state.selectedIndex !== tabIndex) {
82 _this.setState({ selectedIndex: tabIndex });
83 tab.active = true;
84 }
85
86 if (_this.props.onSelect) {
87 _this.props.onSelect(tabIndex);
88 }
89 };
90
91 return _this;
92 }
93
94 _createClass(TabWrapper, [{
95 key: 'render',
96 value: function render() {
97 var _this2 = this;
98
99 var cx = _bind2.default.bind(_style2.default);
100 var tabWrapperClasses = cx(_style2.default['tab-wrapper'], this.props.optClass);
101 var tabPanel = [];
102
103 var tabs = this.getTabs().map(function (tab, index) {
104 tabPanel.push(tab.props.children ? _react2.default.createElement(_TabTemplate2.default, {
105 key: index,
106 active: _this2.isActive(index),
107 class: tab.props.optTabContentClass
108 }, tab.props.children) : undefined);
109
110 return _react2.default.cloneElement(tab, {
111 key: index,
112 active: _this2.isActive(index),
113 tabIndex: index,
114 onClick: _this2.handleClick
115 });
116 });
117
118 return _react2.default.createElement(
119 'div',
120 { className: tabWrapperClasses },
121 _react2.default.createElement(
122 'div',
123 { className: _style2.default['tabs-list'] },
124 tabs
125 ),
126 _react2.default.createElement(
127 'div',
128 { className: _style2.default['tab-panels'] },
129 tabPanel
130 )
131 );
132 }
133 }]);
134
135 return TabWrapper;
136}(_react2.default.Component);
137
138TabWrapper.defaultProps = {
139 initialSelectedIndex: 0
140};
141TabWrapper.propTypes = {
142 /**
143 * Specify initial visible tab index. If initialSelectedIndex is set but larger than the total amount of specified tabs, it will revert back to default.
144 */
145 initialSelectedIndex: _propTypes2.default.number,
146 /**
147 * Called when a tab is selected.
148 */
149 onSelect: _propTypes2.default.func,
150 /**
151 * Optional styles to add to the tab wrapper component. Use 'secondary' to apply secondary tab wrapper styles.
152 */
153 optClass: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.string])
154};
155exports.default = TabWrapper;
\No newline at end of file