UNPKG

11.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Tabs = undefined;
7
8var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
9
10var _react = require("react");
11
12var _react2 = _interopRequireDefault(_react);
13
14var _KeyCode = require("./KeyCode");
15
16var _KeyCode2 = _interopRequireDefault(_KeyCode);
17
18var _TabPane = require("./TabPane");
19
20var _TabPane2 = _interopRequireDefault(_TabPane);
21
22var _classnames2 = require("classnames");
23
24var _classnames3 = _interopRequireDefault(_classnames2);
25
26var _TabContent = require("./TabContent");
27
28var _TabContent2 = _interopRequireDefault(_TabContent);
29
30var _ScrollableInkTabBar = require("./ScrollableInkTabBar");
31
32var _ScrollableInkTabBar2 = _interopRequireDefault(_ScrollableInkTabBar);
33
34var _propTypes = require("prop-types");
35
36var _propTypes2 = _interopRequireDefault(_propTypes);
37
38var _createReactClass = require("create-react-class");
39
40var _createReactClass2 = _interopRequireDefault(_createReactClass);
41
42var _beeIcon = require("bee-icon");
43
44var _beeIcon2 = _interopRequireDefault(_beeIcon);
45
46function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
47
48function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
49
50function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } /**
51 * This source code is quoted from rc-tabs.
52 * homepage: https://github.com/react-component/tabs
53 */
54
55
56function noop() {}
57
58function getDefaultActiveKey(props) {
59 var activeKey = void 0;
60 _react2["default"].Children.forEach(props.children, function (child) {
61 if (child && !activeKey && !child.props.disabled) {
62 activeKey = child.key;
63 }
64 });
65 return activeKey;
66}
67
68var Tabs = (0, _createReactClass2["default"])({
69 propTypes: {
70 destroyInactiveTabPane: _propTypes2["default"].bool,
71 renderTabBar: _propTypes2["default"].func.isRequired,
72 renderTabContent: _propTypes2["default"].func.isRequired,
73 onChange: _propTypes2["default"].func,
74 children: _propTypes2["default"].any,
75 clsPrefix: _propTypes2["default"].string,
76 className: _propTypes2["default"].string,
77 tabBarPosition: _propTypes2["default"].string,
78 style: _propTypes2["default"].object,
79 tabBarStyle: _propTypes2["default"].oneOf(["simple", "fill", "primary", "upborder", "fade", "downborder", "trapezoid", "editable-card"])
80 },
81
82 getDefaultProps: function getDefaultProps() {
83 return {
84 clsPrefix: "u-tabs",
85 destroyInactiveTabPane: false,
86 onChange: noop,
87 tabBarPosition: "top",
88 style: {},
89 renderTabContent: function renderTabContent() {
90 return _react2["default"].createElement(_TabContent2["default"], null);
91 },
92 renderTabBar: function renderTabBar() {
93 return _react2["default"].createElement(_ScrollableInkTabBar2["default"], null);
94 },
95 tabBarStyle: "simple",
96 animated: true
97 };
98 },
99 getInitialState: function getInitialState() {
100 var props = this.props;
101 var activeKey = void 0;
102 if ("activeKey" in props) {
103 activeKey = props.activeKey;
104 } else if ("defaultActiveKey" in props) {
105 activeKey = props.defaultActiveKey;
106 } else {
107 activeKey = getDefaultActiveKey(props);
108 }
109 return {
110 activeKey: activeKey
111 };
112 },
113 componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
114 if ("activeKey" in nextProps) {
115 this.setState({
116 activeKey: nextProps.activeKey
117 });
118 }
119 },
120 onTabClick: function onTabClick(activeKey) {
121 this.props.onTabClick && this.props.onTabClick(activeKey);
122 if (this.tabBar.props.onTabClick) {
123 this.tabBar.props.onTabClick(activeKey);
124 }
125 this.setActiveKey(activeKey);
126 },
127 onNavKeyDown: function onNavKeyDown(e) {
128 var eventKeyCode = e.keyCode;
129 if (eventKeyCode === _KeyCode2["default"].RIGHT || eventKeyCode === _KeyCode2["default"].DOWN) {
130 e.preventDefault();
131 var nextKey = this.getNextActiveKey(true);
132 this.onTabClick(nextKey);
133 } else if (eventKeyCode === _KeyCode2["default"].LEFT || eventKeyCode === _KeyCode2["default"].UP) {
134 e.preventDefault();
135 var previousKey = this.getNextActiveKey(false);
136 this.onTabClick(previousKey);
137 }
138 },
139 setActiveKey: function setActiveKey(activeKey) {
140 if (this.state.activeKey !== activeKey) {
141 if (!("activeKey" in this.props)) {
142 this.setState({
143 activeKey: activeKey
144 });
145 }
146 this.props.onChange(activeKey);
147 }
148 },
149 getNextActiveKey: function getNextActiveKey(next) {
150 var activeKey = this.state.activeKey;
151 var children = [];
152 _react2["default"].Children.forEach(this.props.children, function (c) {
153 if (c && !c.props.disabled) {
154 if (next) {
155 children.push(c);
156 } else {
157 children.unshift(c);
158 }
159 }
160 });
161 var length = children.length;
162 var ret = length && children[0].key;
163 children.forEach(function (child, i) {
164 if (child.key === activeKey) {
165 if (i === length - 1) {
166 ret = children[0].key;
167 } else {
168 ret = children[i + 1].key;
169 }
170 }
171 });
172 return ret;
173 },
174 onPrevClick: function onPrevClick(e) {
175 this.props.onPrevClick && this.props.onPrevClick(e);
176 },
177 onNextClick: function onNextClick(e) {
178 this.props.onNextClick && this.props.onNextClick(e);
179 },
180 createNewTab: function createNewTab(targetKey) {
181 var onEdit = this.props.onEdit;
182
183 if (onEdit) {
184 onEdit(targetKey, 'add');
185 }
186 },
187 removeTab: function removeTab(targetKey, e) {
188 e.stopPropagation();
189 if (!targetKey) {
190 return;
191 }
192
193 var onEdit = this.props.onEdit;
194
195 if (onEdit) {
196 onEdit(targetKey, 'remove');
197 }
198 },
199 render: function render() {
200 var _classnames,
201 _this = this;
202
203 var props = this.props;
204
205 var activeKey = props.activeKey,
206 defaultActiveKey = props.defaultActiveKey,
207 clsPrefix = props.clsPrefix,
208 tabBarPosition = props.tabBarPosition,
209 className = props.className,
210 renderTabContent = props.renderTabContent,
211 renderTabBar = props.renderTabBar,
212 tabBarStyle = props.tabBarStyle,
213 extraContent = props.extraContent,
214 animated = props.animated,
215 tabIndex = props.tabIndex,
216 children = props.children,
217 hideAdd = props.hideAdd,
218 scrollAnimated = props.scrollAnimated,
219 inkBarAnimated = props.inkBarAnimated,
220 useTransform3d = props.useTransform3d,
221 destroyInactiveTabPane = props.destroyInactiveTabPane,
222 onTabClick = props.onTabClick,
223 onEdit = props.onEdit,
224 onNextClick = props.onNextClick,
225 onPrevClick = props.onPrevClick,
226 onChange = props.onChange,
227 others = _objectWithoutProperties(props, ["activeKey", "defaultActiveKey", "clsPrefix", "tabBarPosition", "className", "renderTabContent", "renderTabBar", "tabBarStyle", "extraContent", "animated", "tabIndex", "children", "hideAdd", "scrollAnimated", "inkBarAnimated", "useTransform3d", "destroyInactiveTabPane", "onTabClick", "onEdit", "onNextClick", "onPrevClick", "onChange"]);
228
229 var cls = (0, _classnames3["default"])((_classnames = {}, _defineProperty(_classnames, clsPrefix, true), _defineProperty(_classnames, clsPrefix + "-" + tabBarPosition, true), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, clsPrefix + "-" + tabBarStyle, true), _classnames));
230
231 var renderProps = _extends({}, this.props, {
232 children: null,
233 inkBarAnimated: inkBarAnimated,
234 extraContent: extraContent,
235 className: cls
236 });
237 if (renderTabBar) {
238 this.tabBar = renderTabBar(renderProps, _ScrollableInkTabBar2["default"]);
239 } else {
240 this.tabBar = _react2["default"].createElement(_ScrollableInkTabBar2["default"], renderProps);
241 }
242
243 // only card type tabs can be added and closed
244 var childrenWithClose = [],
245 tabBarExtraContent = extraContent;
246 if (tabBarStyle === 'editable-card') {
247 childrenWithClose = [];
248 _react2["default"].Children.forEach(children, function (child, index) {
249 if (!_react2["default"].isValidElement(child)) return child;
250 var closable = child.props.closable;
251
252 closable = typeof closable === 'undefined' ? true : closable;
253 var closeIcon = closable ? _react2["default"].createElement(_beeIcon2["default"], {
254 type: "uf-close",
255 className: clsPrefix + "-close-x",
256 onClick: function onClick(e) {
257 return _this.removeTab(child.key, e);
258 }
259 }) : null;
260 childrenWithClose.push(_react2["default"].cloneElement(child, {
261 tab: _react2["default"].createElement(
262 "div",
263 { className: closable ? undefined : clsPrefix + "-tab-unclosable" },
264 child.props.tab,
265 closeIcon
266 ),
267 key: child.key || index
268 }));
269 });
270 // Add new tab handler
271 if (!hideAdd) {
272 tabBarExtraContent = _react2["default"].createElement(
273 "span",
274 null,
275 _react2["default"].createElement(_beeIcon2["default"], { type: "uf-add-s-o", className: clsPrefix + "-new-tab", onClick: this.createNewTab }),
276 extraContent
277 );
278 }
279 }
280
281 var contents = [_react2["default"].cloneElement(this.tabBar, {
282 clsPrefix: clsPrefix,
283 key: "tabBar",
284 onKeyDown: this.onNavKeyDown,
285 tabBarPosition: tabBarPosition,
286 extraContent: tabBarExtraContent,
287 onTabClick: this.onTabClick,
288 panels: childrenWithClose.length > 0 ? childrenWithClose : children,
289 activeKey: this.state.activeKey,
290 tabIndex: tabIndex,
291 onPrevClick: this.onPrevClick,
292 onNextClick: this.onNextClick
293 }), _react2["default"].cloneElement(renderTabContent(), {
294 clsPrefix: clsPrefix,
295 tabBarPosition: tabBarPosition,
296 animated: animated,
297 activeKey: this.state.activeKey,
298 destroyInactiveTabPane: props.destroyInactiveTabPane,
299 children: props.children,
300 // style: { height: '100%' },
301 onChange: this.setActiveKey,
302 key: "tabContent"
303 })];
304 if (tabBarPosition === "bottom") {
305 contents.reverse();
306 }
307 return _react2["default"].createElement(
308 "div",
309 _extends({ className: cls, style: props.style }, others),
310 contents
311 );
312 }
313});
314
315Tabs.TabPane = _TabPane2["default"];
316
317exports.Tabs = Tabs;
\No newline at end of file