UNPKG

14.5 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
10
11var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
12
13var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
14
15var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
16
17var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
18
19var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
20
21var _inherits2 = require('babel-runtime/helpers/inherits');
22
23var _inherits3 = _interopRequireDefault(_inherits2);
24
25var _class, _temp;
26
27var _react = require('react');
28
29var _react2 = _interopRequireDefault(_react);
30
31var _propTypes = require('prop-types');
32
33var _propTypes2 = _interopRequireDefault(_propTypes);
34
35var _classnames2 = require('classnames');
36
37var _classnames3 = _interopRequireDefault(_classnames2);
38
39var _reactLifecyclesCompat = require('react-lifecycles-compat');
40
41var _util = require('../util');
42
43var _nav = require('./tabs/nav');
44
45var _nav2 = _interopRequireDefault(_nav);
46
47var _content = require('./tabs/content');
48
49var _content2 = _interopRequireDefault(_content);
50
51var _utils = require('./tabs/utils');
52
53var _zhCn = require('../locale/zh-cn');
54
55var _zhCn2 = _interopRequireDefault(_zhCn);
56
57function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
58
59var noop = function noop() {};
60
61/** Tab */
62var Tab = (_temp = _class = function (_Component) {
63 (0, _inherits3.default)(Tab, _Component);
64
65 function Tab(props, context) {
66 (0, _classCallCheck3.default)(this, Tab);
67
68 var _this = (0, _possibleConstructorReturn3.default)(this, _Component.call(this, props, context));
69
70 _this.handleTriggerEvent = function (eventType, key) {
71 var _this$props = _this.props,
72 triggerType = _this$props.triggerType,
73 onClick = _this$props.onClick,
74 onChange = _this$props.onChange;
75
76 if (triggerType === eventType) {
77 onClick(key);
78 _this.setActiveKey(key);
79 if (_this.state.activeKey !== key) {
80 onChange(key);
81 }
82 }
83 };
84
85 _this.onNavKeyDown = function (e) {
86 var keyCode = e.keyCode;
87 var disableKeyboard = _this.props.disableKeyboard;
88
89
90 if (disableKeyboard) {
91 return;
92 }
93
94 if (keyCode >= _util.KEYCODE.LEFT && keyCode <= _util.KEYCODE.DOWN) {
95 e.preventDefault();
96 }
97
98 var newKey = void 0;
99 if (keyCode === _util.KEYCODE.RIGHT || keyCode === _util.KEYCODE.DOWN) {
100 newKey = _this.getNextActiveKey(true);
101 _this.handleTriggerEvent(_this.props.triggerType, newKey);
102 } else if (keyCode === _util.KEYCODE.LEFT || keyCode === _util.KEYCODE.UP) {
103 newKey = _this.getNextActiveKey(false);
104 _this.handleTriggerEvent(_this.props.triggerType, newKey);
105 }
106 };
107
108 _this.state = {
109 activeKey: _this.getDefaultActiveKey(props)
110 };
111 return _this;
112 }
113
114 Tab.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
115 if (props.activeKey !== undefined && state.activeKey !== '' + props.activeKey) {
116 return {
117 activeKey: '' + props.activeKey
118 };
119 }
120
121 return {};
122 };
123
124 Tab.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
125 var preChildLen = prevProps.children && prevProps.children.length || 0;
126 var curChildLen = this.props.children && this.props.children.length || 0;
127 if (preChildLen !== 0 && curChildLen !== 0 && !('activeKey' in this.props) & !this.isActiveKeyExist(this.state.activeKey)) {
128 var activeKey = this.getDefaultActiveKey(this.props);
129 if (activeKey) {
130 // eslint-disable-next-line react/no-did-update-set-state
131 this.setState({
132 activeKey: activeKey
133 });
134 }
135 }
136 };
137
138 Tab.prototype.getDefaultActiveKey = function getDefaultActiveKey(props) {
139 var activeKey = props.activeKey === undefined ? props.defaultActiveKey : props.activeKey;
140
141 if (activeKey === undefined) {
142 _react2.default.Children.forEach(props.children, function (child, index) {
143 if (activeKey !== undefined) return;
144 if (_react2.default.isValidElement(child)) {
145 if (!child.props.disabled) {
146 activeKey = child.key || index;
147 }
148 }
149 });
150 }
151
152 return activeKey !== undefined ? '' + activeKey : undefined;
153 };
154
155 Tab.prototype.getNextActiveKey = function getNextActiveKey(isNext) {
156 var _this2 = this;
157
158 var children = [];
159 _react2.default.Children.forEach(this.props.children, function (child) {
160 if (_react2.default.isValidElement(child)) {
161 if (!child.props.disabled) {
162 if (isNext) {
163 children.push(child);
164 } else {
165 children.unshift(child);
166 }
167 }
168 }
169 });
170
171 var length = children.length;
172 var key = length && children[0].key;
173 children.forEach(function (child, i) {
174 if (child.key === _this2.state.activeKey) {
175 if (i === length - 1) {
176 key = children[0].key;
177 } else {
178 key = children[i + 1].key;
179 }
180 }
181 });
182 return key;
183 };
184
185 Tab.prototype.isActiveKeyExist = function isActiveKeyExist(activeKey) {
186 var exist = false;
187 _react2.default.Children.forEach(this.props.children, function (child, index) {
188 if (exist) return;
189 if (_react2.default.isValidElement(child)) {
190 if (!child.props.disabled) {
191 var key = child.key || index;
192 if (activeKey === '' + key) {
193 exist = true;
194 }
195 }
196 }
197 });
198 return exist;
199 };
200
201 Tab.prototype.setActiveKey = function setActiveKey(key) {
202 var activeKey = this.state.activeKey;
203
204 // 如果 key 没变,或者受控状态下,则跳过
205
206 if (key === activeKey || 'activeKey' in this.props) {
207 return;
208 }
209 this.setState({
210 activeKey: key
211 });
212 };
213
214 Tab.prototype.render = function render() {
215 var _classnames;
216
217 var _props = this.props,
218 prefix = _props.prefix,
219 animation = _props.animation,
220 shape = _props.shape,
221 size = _props.size,
222 extra = _props.extra,
223 excessMode = _props.excessMode,
224 tabPosition = _props.tabPosition,
225 tabRender = _props.tabRender,
226 triggerType = _props.triggerType,
227 lazyLoad = _props.lazyLoad,
228 unmountInactiveTabs = _props.unmountInactiveTabs,
229 popupProps = _props.popupProps,
230 navStyle = _props.navStyle,
231 navClassName = _props.navClassName,
232 contentStyle = _props.contentStyle,
233 contentClassName = _props.contentClassName,
234 className = _props.className,
235 onClose = _props.onClose,
236 children = _props.children,
237 rtl = _props.rtl,
238 device = _props.device,
239 locale = _props.locale,
240 icons = _props.icons,
241 others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'animation', 'shape', 'size', 'extra', 'excessMode', 'tabPosition', 'tabRender', 'triggerType', 'lazyLoad', 'unmountInactiveTabs', 'popupProps', 'navStyle', 'navClassName', 'contentStyle', 'contentClassName', 'className', 'onClose', 'children', 'rtl', 'device', 'locale', 'icons']);
242 var activeKey = this.state.activeKey;
243
244
245 var tabs = (0, _utils.toArray)(children);
246 var newPosition = tabPosition;
247 if (rtl && ['left', 'right'].indexOf(tabPosition) >= 0) {
248 newPosition = tabPosition === 'left' ? 'right' : 'left';
249 }
250 var classNames = (0, _classnames3.default)((_classnames = {}, _classnames[prefix + 'tabs'] = true, _classnames[prefix + 'tabs-' + shape] = shape, _classnames[prefix + 'tabs-vertical'] = shape === 'wrapped' && ['left', 'right'].indexOf(tabPosition) >= 0, _classnames[prefix + 'tabs-scrollable'] = true, _classnames[prefix + 'tabs-' + newPosition] = shape === 'wrapped', _classnames['' + (prefix + size)] = size, _classnames), className);
251
252 var navProps = {
253 prefix: prefix,
254 rtl: rtl,
255 animation: animation,
256 activeKey: activeKey,
257 excessMode: excessMode,
258 extra: extra,
259 tabs: tabs,
260 tabPosition: tabPosition,
261 tabRender: tabRender,
262 triggerType: triggerType,
263 popupProps: popupProps,
264 onClose: onClose,
265 onTriggerEvent: this.handleTriggerEvent,
266 onKeyDown: this.onNavKeyDown,
267 style: navStyle,
268 className: navClassName,
269 locale: locale,
270 icons: icons
271 };
272
273 var contentProps = {
274 prefix: prefix,
275 activeKey: activeKey,
276 lazyLoad: lazyLoad,
277 unmountInactiveTabs: unmountInactiveTabs,
278 style: contentStyle,
279 className: contentClassName
280 };
281
282 var tabChildren = [_react2.default.createElement(_nav2.default, (0, _extends3.default)({ key: 'tab-nav' }, navProps)), _react2.default.createElement(
283 _content2.default,
284 (0, _extends3.default)({ key: 'tab-content' }, contentProps),
285 tabs
286 )];
287
288 if (tabPosition === 'bottom') {
289 tabChildren.reverse();
290 }
291
292 return _react2.default.createElement(
293 'div',
294 (0, _extends3.default)({ dir: rtl ? 'rtl' : undefined, className: classNames }, _util.obj.pickOthers(Tab.propTypes, others)),
295 tabChildren
296 );
297 };
298
299 return Tab;
300}(_react.Component), _class.propTypes = {
301 prefix: _propTypes2.default.string,
302 rtl: _propTypes2.default.bool,
303 device: _propTypes2.default.oneOf(['tablet', 'desktop', 'phone']),
304 /**
305 * 被激活的选项卡的 key, 赋值则tab为受控组件, 用户无法切换
306 */
307 activeKey: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
308 /**
309 * 初始化时被激活的选项卡的 key
310 */
311 defaultActiveKey: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string]),
312 /**
313 * 外观形态
314 */
315 shape: _propTypes2.default.oneOf(['pure', 'wrapped', 'text', 'capsule']),
316 /**
317 * 是否开启动效
318 */
319 animation: _propTypes2.default.bool,
320 /**
321 * 选项卡过多时的滑动模式
322 */
323 excessMode: _propTypes2.default.oneOf(['slide', 'dropdown']),
324 /**
325 * 导航选项卡的位置,只适用于包裹型(wrapped)选项卡
326 */
327 tabPosition: _propTypes2.default.oneOf(['top', 'bottom', 'left', 'right']),
328 /**
329 * 尺寸
330 */
331 size: _propTypes2.default.oneOf(['small', 'medium']),
332 /**
333 * 激活选项卡的触发方式
334 */
335 triggerType: _propTypes2.default.oneOf(['hover', 'click']),
336 /**
337 * 是否延迟加载 TabItem 的内容, 默认开启, 即不提前渲染
338 */
339 lazyLoad: _propTypes2.default.bool,
340 /**
341 * 是否自动卸载未处于激活状态的选项卡
342 */
343 unmountInactiveTabs: _propTypes2.default.bool,
344 /**
345 * 导航条的自定义样式
346 */
347 navStyle: _propTypes2.default.object,
348 /**
349 * 导航条的自定义样式类
350 */
351 navClassName: _propTypes2.default.string,
352 /**
353 * 内容区容器的自定义样式
354 */
355 contentStyle: _propTypes2.default.object,
356 /**
357 * 内容区容器的自定义样式类
358 */
359 contentClassName: _propTypes2.default.string,
360 /**
361 * 导航栏附加内容
362 */
363 extra: _propTypes2.default.node,
364 /**
365 * 禁止键盘事件,设置后无法通过键盘的上下左右按键,切换当前选中的tab
366 */
367 disableKeyboard: _propTypes2.default.bool,
368 /**
369 * 点击单个选项卡时触发的回调
370 */
371 onClick: _propTypes2.default.func,
372 /**
373 * 选项卡发生切换时的事件回调
374 * @param {String|Number} key 改变后的 key
375 */
376 onChange: _propTypes2.default.func,
377 /**
378 * 选项卡被关闭时的事件回调
379 * @param {String|Number} key 关闭的选项卡的 key
380 */
381 onClose: _propTypes2.default.func,
382 /**
383 * 自定义选项卡模板渲染函数
384 * @param {String} key 当前 Tab.Item 的 key 值
385 * @param {Object} props 传给 Tab.Item 的所有属性键值对
386 * @return {ReactNode} 返回自定义组件
387 */
388 tabRender: _propTypes2.default.func,
389 /**
390 * 弹层属性透传, 只有当 excessMode 为 dropdown 时生效
391 */
392 popupProps: _propTypes2.default.object,
393 children: _propTypes2.default.any,
394 className: _propTypes2.default.string,
395 locale: _propTypes2.default.object,
396 /**
397 * 自定义组件内 icon
398 */
399 icons: _propTypes2.default.shape({
400 prev: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.string]),
401 next: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.string]),
402 dropdown: _propTypes2.default.oneOfType([_propTypes2.default.node, _propTypes2.default.string])
403 })
404}, _class.defaultProps = {
405 prefix: 'next-',
406 shape: 'pure',
407 size: 'medium',
408 animation: true,
409 tabPosition: 'top',
410 excessMode: 'slide',
411 triggerType: 'click',
412 lazyLoad: true,
413 unmountInactiveTabs: false,
414 disableKeyboard: false,
415 onClick: noop,
416 onChange: noop,
417 onClose: noop,
418 locale: _zhCn2.default.Tab,
419 icons: {}
420}, _temp);
421Tab.displayName = 'Tab';
422exports.default = (0, _reactLifecyclesCompat.polyfill)(Tab);
423module.exports = exports['default'];
\No newline at end of file