UNPKG

8.37 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 _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12
13function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
14
15function _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; }
16
17var _react = require('react');
18
19var _react2 = _interopRequireDefault(_react);
20
21var _reactLibUpdate = require('react/lib/update');
22
23var _reactLibUpdate2 = _interopRequireDefault(_reactLibUpdate);
24
25var _DragTab = require('./DragTab');
26
27var _DragTab2 = _interopRequireDefault(_DragTab);
28
29var _Tab = require('./Tab');
30
31var _Tab2 = _interopRequireDefault(_Tab);
32
33var _FunctionTab = require('./FunctionTab');
34
35var _FunctionTab2 = _interopRequireDefault(_FunctionTab);
36
37var _classnames = require('classnames');
38
39var _classnames2 = _interopRequireDefault(_classnames);
40
41var Tabs = (function (_React$Component) {
42 _inherits(Tabs, _React$Component);
43
44 function Tabs(props) {
45 _classCallCheck(this, Tabs);
46
47 _get(Object.getPrototypeOf(Tabs.prototype), 'constructor', this).call(this, props);
48 this.handleTabClick = this.handleTabClick.bind(this);
49 this.handleAddFrontClick = this.handleAddFrontClick.bind(this);
50 this.handleAddBackClick = this.handleAddBackClick.bind(this);
51 this.handleTabDeleteButton = this.handleTabDeleteButton.bind(this);
52 this.moveTab = this.moveTab.bind(this);
53 this._getPanel = this._getPanel.bind(this);
54
55 this.state = {
56 activeKey: props.activeKey || props.defaultActiveKey,
57 style: props.style || props.defaultStyle,
58 children: props.children,
59 status: 'static' // || 'dragging'
60 };
61 }
62
63 _createClass(Tabs, [{
64 key: 'componentWillReceiveProps',
65 value: function componentWillReceiveProps(nextProps) {
66 if (nextProps.activeKey !== this.state.activeKey) {
67 this.setState({
68 activeKey: nextProps.activeKey
69 });
70 }
71 if (nextProps.children !== this.state.children) {
72 this.setState({
73 children: nextProps.children
74 });
75 }
76 }
77 }, {
78 key: 'handleTabClick',
79 value: function handleTabClick(activeKey) {
80 if (this.props.handleTabClick) {
81 this.props.handleTabClick(activeKey);
82 }
83
84 this.setState({
85 activeKey: activeKey,
86 panelUpdateKey: -1
87 });
88 }
89 }, {
90 key: 'handleAddFrontClick',
91 value: function handleAddFrontClick() {
92 this.props.handleAddFrontClick();
93 this.setState({
94 panelUpdateKey: 0
95 });
96 }
97 }, {
98 key: 'handleAddBackClick',
99 value: function handleAddBackClick() {
100 this.props.handleAddBackClick();
101 }
102 }, {
103 key: 'handleTabDeleteButton',
104 value: function handleTabDeleteButton(key) {
105 this.props.handleTabDeleteButton();
106 this.setState({
107 panelUpdateKey: key
108 });
109 }
110 }, {
111 key: 'moveTab',
112 value: function moveTab(dragIndex, hoverIndex) {
113 var dragTab = this.state.children[dragIndex];
114 this.props.setMoveData(dragIndex, hoverIndex);
115 }
116 }, {
117 key: '_getPanel',
118 value: function _getPanel() {
119 var that = this;
120 var tab = [];
121 var panel = [];
122 _react2['default'].Children.forEach(this.state.children, function (children, index) {
123 // add tabs
124 var status, className;
125 if (index === that.state.activeKey) {
126 status = 'active';
127 } else {
128 status = 'inactive';
129 }
130 var props = {
131 key: 'tab' + index,
132 tabKey: index,
133 title: children.props.title,
134 status: status,
135 style: that.state.style,
136 handleTabClick: that.handleTabClick,
137 tabDeleteButton: that.props.tabDeleteButton,
138 handleTabDeleteButton: that.handleTabDeleteButton,
139 beginDrag: that.props.beginDrag,
140 moveTab: that.moveTab
141 };
142 if (that.props.draggable) {
143 tab.push(_react2['default'].createElement(_DragTab2['default'], props));
144 } else {
145 tab.push(_react2['default'].createElement(_Tab2['default'], props));
146 }
147 if (!children.props.lazy || children.props.lazy && index === that.state.activeKey) {
148 var props = { className: (0, _classnames2['default'])(that.state.style + 'panel', status), status: status, key: index };
149 if (that.state.panelUpdateKey === index) {
150 props.update = true;
151 }
152 panel.push(_react2['default'].cloneElement(children, props));
153 }
154 });
155 if (this.props.addFrontTab && tab.length > 0) {
156 //if the tab more than one, show add button
157 tab.unshift(_react2['default'].createElement(_FunctionTab2['default'], { key: 'ADDFront',
158 tabKey: 'ADD',
159 title: '+',
160 style: that.state.style,
161 handleTabClick: that.handleAddFrontClick }));
162 }
163 if (this.props.addBackTab && tab.length > 0) {
164 //if the tab more than one, show add button
165 tab.push(_react2['default'].createElement(_FunctionTab2['default'], { key: 'ADDBack',
166 tabKey: 'ADD',
167 title: '+',
168 style: that.state.style,
169 handleTabClick: that.handleAddBackClick }));
170 }
171
172 return { tab: tab, panel: panel };
173 }
174 }, {
175 key: 'render',
176 value: function render() {
177 var deleteButtonTmpl;
178 var opt = this._getPanel();
179 var wrapper = this.state.style + "wrapper";
180 var tabWrapper = this.state.style + "tab__wrapper";
181 var panelWrapper = this.state.style + "panel__wrapper";
182 var noneName = this.props.deleteAllButtonName;
183 if (this.props.deleteAllButton && opt.tab.length > 0) {
184 var className = this.state.style + "delete " + this.props.deleteAllClassname;
185 deleteButtonTmpl = _react2['default'].createElement(
186 'button',
187 { className: className, onClick: this.props.handleDeleteAllButton },
188 noneName ? noneName : 'None'
189 );
190 }
191
192 return _react2['default'].createElement(
193 'div',
194 { className: wrapper },
195 deleteButtonTmpl,
196 _react2['default'].createElement(
197 'div',
198 { className: tabWrapper },
199 opt.tab
200 ),
201 _react2['default'].createElement(
202 'div',
203 { className: panelWrapper },
204 opt.panel
205 )
206 );
207 }
208 }], [{
209 key: 'defaultProps',
210 value: {
211 defaultActiveKey: 0,
212 defaultStyle: "tabtab__default__"
213 },
214 enumerable: true
215 }]);
216
217 return Tabs;
218})(_react2['default'].Component);
219
220exports['default'] = Tabs;
221module.exports = exports['default'];
\No newline at end of file