1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _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 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _propTypes = require('prop-types');
|
14 |
|
15 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
16 |
|
17 | var _TabTemplate = require('./TabTemplate');
|
18 |
|
19 | var _TabTemplate2 = _interopRequireDefault(_TabTemplate);
|
20 |
|
21 | var _style = require('./style.scss');
|
22 |
|
23 | var _style2 = _interopRequireDefault(_style);
|
24 |
|
25 | var _bind = require('classnames/bind');
|
26 |
|
27 | var _bind2 = _interopRequireDefault(_bind);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | function _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 |
|
35 | function _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 |
|
37 | var 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 |
|
138 | TabWrapper.defaultProps = {
|
139 | initialSelectedIndex: 0
|
140 | };
|
141 | TabWrapper.propTypes = {
|
142 | |
143 |
|
144 |
|
145 | initialSelectedIndex: _propTypes2.default.number,
|
146 | |
147 |
|
148 |
|
149 | onSelect: _propTypes2.default.func,
|
150 | |
151 |
|
152 |
|
153 | optClass: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.string])
|
154 | };
|
155 | exports.default = TabWrapper; |
\ | No newline at end of file |