1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
5 | import _inherits from 'babel-runtime/helpers/inherits';
|
6 |
|
7 | var _class, _temp;
|
8 |
|
9 | import React, { Component } from 'react';
|
10 | import PropTypes from 'prop-types';
|
11 | import classnames from 'classnames';
|
12 | import { polyfill } from 'react-lifecycles-compat';
|
13 | import { KEYCODE, obj } from '../util';
|
14 | import TabNav from './tabs/nav';
|
15 | import TabContent from './tabs/content';
|
16 | import { toArray } from './tabs/utils';
|
17 | import zhCN from '../locale/zh-cn';
|
18 |
|
19 | var noop = function noop() {};
|
20 |
|
21 |
|
22 | var Tab = (_temp = _class = function (_Component) {
|
23 | _inherits(Tab, _Component);
|
24 |
|
25 | function Tab(props, context) {
|
26 | _classCallCheck(this, Tab);
|
27 |
|
28 | var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
|
29 |
|
30 | _this.handleTriggerEvent = function (eventType, key) {
|
31 | var _this$props = _this.props,
|
32 | triggerType = _this$props.triggerType,
|
33 | onClick = _this$props.onClick,
|
34 | onChange = _this$props.onChange;
|
35 |
|
36 | if (triggerType === eventType) {
|
37 | onClick(key);
|
38 | _this.setActiveKey(key);
|
39 | if (_this.state.activeKey !== key) {
|
40 | onChange(key);
|
41 | }
|
42 | }
|
43 | };
|
44 |
|
45 | _this.onNavKeyDown = function (e) {
|
46 | var keyCode = e.keyCode;
|
47 | var disableKeyboard = _this.props.disableKeyboard;
|
48 |
|
49 |
|
50 | if (disableKeyboard) {
|
51 | return;
|
52 | }
|
53 |
|
54 | if (keyCode >= KEYCODE.LEFT && keyCode <= KEYCODE.DOWN) {
|
55 | e.preventDefault();
|
56 | }
|
57 |
|
58 | var newKey = void 0;
|
59 | if (keyCode === KEYCODE.RIGHT || keyCode === KEYCODE.DOWN) {
|
60 | newKey = _this.getNextActiveKey(true);
|
61 | _this.handleTriggerEvent(_this.props.triggerType, newKey);
|
62 | } else if (keyCode === KEYCODE.LEFT || keyCode === KEYCODE.UP) {
|
63 | newKey = _this.getNextActiveKey(false);
|
64 | _this.handleTriggerEvent(_this.props.triggerType, newKey);
|
65 | }
|
66 | };
|
67 |
|
68 | _this.state = {
|
69 | activeKey: _this.getDefaultActiveKey(props)
|
70 | };
|
71 | return _this;
|
72 | }
|
73 |
|
74 | Tab.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
|
75 | if (props.activeKey !== undefined && state.activeKey !== '' + props.activeKey) {
|
76 | return {
|
77 | activeKey: '' + props.activeKey
|
78 | };
|
79 | }
|
80 |
|
81 | return {};
|
82 | };
|
83 |
|
84 | Tab.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
|
85 | var preChildLen = prevProps.children && prevProps.children.length || 0;
|
86 | var curChildLen = this.props.children && this.props.children.length || 0;
|
87 | if (preChildLen !== 0 && curChildLen !== 0 && !('activeKey' in this.props) & !this.isActiveKeyExist(this.state.activeKey)) {
|
88 | var activeKey = this.getDefaultActiveKey(this.props);
|
89 | if (activeKey) {
|
90 |
|
91 | this.setState({
|
92 | activeKey: activeKey
|
93 | });
|
94 | }
|
95 | }
|
96 | };
|
97 |
|
98 | Tab.prototype.getDefaultActiveKey = function getDefaultActiveKey(props) {
|
99 | var activeKey = props.activeKey === undefined ? props.defaultActiveKey : props.activeKey;
|
100 |
|
101 | if (activeKey === undefined) {
|
102 | React.Children.forEach(props.children, function (child, index) {
|
103 | if (activeKey !== undefined) return;
|
104 | if (React.isValidElement(child)) {
|
105 | if (!child.props.disabled) {
|
106 | activeKey = child.key || index;
|
107 | }
|
108 | }
|
109 | });
|
110 | }
|
111 |
|
112 | return activeKey !== undefined ? '' + activeKey : undefined;
|
113 | };
|
114 |
|
115 | Tab.prototype.getNextActiveKey = function getNextActiveKey(isNext) {
|
116 | var _this2 = this;
|
117 |
|
118 | var children = [];
|
119 | React.Children.forEach(this.props.children, function (child) {
|
120 | if (React.isValidElement(child)) {
|
121 | if (!child.props.disabled) {
|
122 | if (isNext) {
|
123 | children.push(child);
|
124 | } else {
|
125 | children.unshift(child);
|
126 | }
|
127 | }
|
128 | }
|
129 | });
|
130 |
|
131 | var length = children.length;
|
132 | var key = length && children[0].key;
|
133 | children.forEach(function (child, i) {
|
134 | if (child.key === _this2.state.activeKey) {
|
135 | if (i === length - 1) {
|
136 | key = children[0].key;
|
137 | } else {
|
138 | key = children[i + 1].key;
|
139 | }
|
140 | }
|
141 | });
|
142 | return key;
|
143 | };
|
144 |
|
145 | Tab.prototype.isActiveKeyExist = function isActiveKeyExist(activeKey) {
|
146 | var exist = false;
|
147 | React.Children.forEach(this.props.children, function (child, index) {
|
148 | if (exist) return;
|
149 | if (React.isValidElement(child)) {
|
150 | if (!child.props.disabled) {
|
151 | var key = child.key || index;
|
152 | if (activeKey === '' + key) {
|
153 | exist = true;
|
154 | }
|
155 | }
|
156 | }
|
157 | });
|
158 | return exist;
|
159 | };
|
160 |
|
161 | Tab.prototype.setActiveKey = function setActiveKey(key) {
|
162 | var activeKey = this.state.activeKey;
|
163 |
|
164 |
|
165 |
|
166 | if (key === activeKey || 'activeKey' in this.props) {
|
167 | return;
|
168 | }
|
169 | this.setState({
|
170 | activeKey: key
|
171 | });
|
172 | };
|
173 |
|
174 | Tab.prototype.render = function render() {
|
175 | var _classnames;
|
176 |
|
177 | var _props = this.props,
|
178 | prefix = _props.prefix,
|
179 | animation = _props.animation,
|
180 | shape = _props.shape,
|
181 | size = _props.size,
|
182 | extra = _props.extra,
|
183 | excessMode = _props.excessMode,
|
184 | tabPosition = _props.tabPosition,
|
185 | tabRender = _props.tabRender,
|
186 | triggerType = _props.triggerType,
|
187 | lazyLoad = _props.lazyLoad,
|
188 | unmountInactiveTabs = _props.unmountInactiveTabs,
|
189 | popupProps = _props.popupProps,
|
190 | navStyle = _props.navStyle,
|
191 | navClassName = _props.navClassName,
|
192 | contentStyle = _props.contentStyle,
|
193 | contentClassName = _props.contentClassName,
|
194 | className = _props.className,
|
195 | onClose = _props.onClose,
|
196 | children = _props.children,
|
197 | rtl = _props.rtl,
|
198 | device = _props.device,
|
199 | locale = _props.locale,
|
200 | icons = _props.icons,
|
201 | others = _objectWithoutProperties(_props, ['prefix', 'animation', 'shape', 'size', 'extra', 'excessMode', 'tabPosition', 'tabRender', 'triggerType', 'lazyLoad', 'unmountInactiveTabs', 'popupProps', 'navStyle', 'navClassName', 'contentStyle', 'contentClassName', 'className', 'onClose', 'children', 'rtl', 'device', 'locale', 'icons']);
|
202 |
|
203 | var activeKey = this.state.activeKey;
|
204 |
|
205 |
|
206 | var tabs = toArray(children);
|
207 | var newPosition = tabPosition;
|
208 | if (rtl && ['left', 'right'].indexOf(tabPosition) >= 0) {
|
209 | newPosition = tabPosition === 'left' ? 'right' : 'left';
|
210 | }
|
211 | var classNames = classnames((_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);
|
212 |
|
213 | var navProps = {
|
214 | prefix: prefix,
|
215 | rtl: rtl,
|
216 | animation: animation,
|
217 | activeKey: activeKey,
|
218 | excessMode: excessMode,
|
219 | extra: extra,
|
220 | tabs: tabs,
|
221 | tabPosition: tabPosition,
|
222 | tabRender: tabRender,
|
223 | triggerType: triggerType,
|
224 | popupProps: popupProps,
|
225 | onClose: onClose,
|
226 | onTriggerEvent: this.handleTriggerEvent,
|
227 | onKeyDown: this.onNavKeyDown,
|
228 | style: navStyle,
|
229 | className: navClassName,
|
230 | locale: locale,
|
231 | icons: icons
|
232 | };
|
233 |
|
234 | var contentProps = {
|
235 | prefix: prefix,
|
236 | activeKey: activeKey,
|
237 | lazyLoad: lazyLoad,
|
238 | unmountInactiveTabs: unmountInactiveTabs,
|
239 | style: contentStyle,
|
240 | className: contentClassName
|
241 | };
|
242 |
|
243 | var tabChildren = [React.createElement(TabNav, _extends({ key: 'tab-nav' }, navProps)), React.createElement(
|
244 | TabContent,
|
245 | _extends({ key: 'tab-content' }, contentProps),
|
246 | tabs
|
247 | )];
|
248 |
|
249 | if (tabPosition === 'bottom') {
|
250 | tabChildren.reverse();
|
251 | }
|
252 |
|
253 | return React.createElement(
|
254 | 'div',
|
255 | _extends({ dir: rtl ? 'rtl' : undefined, className: classNames }, obj.pickOthers(Tab.propTypes, others)),
|
256 | tabChildren
|
257 | );
|
258 | };
|
259 |
|
260 | return Tab;
|
261 | }(Component), _class.propTypes = {
|
262 | prefix: PropTypes.string,
|
263 | rtl: PropTypes.bool,
|
264 | device: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
|
265 | |
266 |
|
267 |
|
268 | activeKey: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
269 | |
270 |
|
271 |
|
272 | defaultActiveKey: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
273 | |
274 |
|
275 |
|
276 | shape: PropTypes.oneOf(['pure', 'wrapped', 'text', 'capsule']),
|
277 | |
278 |
|
279 |
|
280 | animation: PropTypes.bool,
|
281 | |
282 |
|
283 |
|
284 | excessMode: PropTypes.oneOf(['slide', 'dropdown']),
|
285 | |
286 |
|
287 |
|
288 | tabPosition: PropTypes.oneOf(['top', 'bottom', 'left', 'right']),
|
289 | |
290 |
|
291 |
|
292 | size: PropTypes.oneOf(['small', 'medium']),
|
293 | |
294 |
|
295 |
|
296 | triggerType: PropTypes.oneOf(['hover', 'click']),
|
297 | |
298 |
|
299 |
|
300 | lazyLoad: PropTypes.bool,
|
301 | |
302 |
|
303 |
|
304 | unmountInactiveTabs: PropTypes.bool,
|
305 | |
306 |
|
307 |
|
308 | navStyle: PropTypes.object,
|
309 | |
310 |
|
311 |
|
312 | navClassName: PropTypes.string,
|
313 | |
314 |
|
315 |
|
316 | contentStyle: PropTypes.object,
|
317 | |
318 |
|
319 |
|
320 | contentClassName: PropTypes.string,
|
321 | |
322 |
|
323 |
|
324 | extra: PropTypes.node,
|
325 | |
326 |
|
327 |
|
328 | disableKeyboard: PropTypes.bool,
|
329 | |
330 |
|
331 |
|
332 | onClick: PropTypes.func,
|
333 | |
334 |
|
335 |
|
336 |
|
337 | onChange: PropTypes.func,
|
338 | |
339 |
|
340 |
|
341 |
|
342 | onClose: PropTypes.func,
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 | tabRender: PropTypes.func,
|
350 | |
351 |
|
352 |
|
353 | popupProps: PropTypes.object,
|
354 | children: PropTypes.any,
|
355 | className: PropTypes.string,
|
356 | locale: PropTypes.object,
|
357 | |
358 |
|
359 |
|
360 | icons: PropTypes.shape({
|
361 | prev: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
|
362 | next: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
|
363 | dropdown: PropTypes.oneOfType([PropTypes.node, PropTypes.string])
|
364 | })
|
365 | }, _class.defaultProps = {
|
366 | prefix: 'next-',
|
367 | shape: 'pure',
|
368 | size: 'medium',
|
369 | animation: true,
|
370 | tabPosition: 'top',
|
371 | excessMode: 'slide',
|
372 | triggerType: 'click',
|
373 | lazyLoad: true,
|
374 | unmountInactiveTabs: false,
|
375 | disableKeyboard: false,
|
376 | onClick: noop,
|
377 | onChange: noop,
|
378 | onClose: noop,
|
379 | locale: zhCN.Tab,
|
380 | icons: {}
|
381 | }, _temp);
|
382 | Tab.displayName = 'Tab';
|
383 |
|
384 |
|
385 | export default polyfill(Tab); |
\ | No newline at end of file |