UNPKG

4.35 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6import React from 'react';
7import PropTypes from 'prop-types';
8import requiredForA11y from 'prop-types-extra/lib/isRequiredForA11y';
9import uncontrollable from 'uncontrollable';
10
11import Nav from './Nav';
12import NavItem from './NavItem';
13import UncontrolledTabContainer from './TabContainer';
14import TabContent from './TabContent';
15import { bsClass as setBsClass } from './utils/bootstrapUtils';
16import ValidComponentChildren from './utils/ValidComponentChildren';
17
18var TabContainer = UncontrolledTabContainer.ControlledComponent;
19
20var propTypes = {
21 /**
22 * Mark the Tab with a matching `eventKey` as active.
23 *
24 * @controllable onSelect
25 */
26 activeKey: PropTypes.any,
27
28 /**
29 * Navigation style
30 */
31 bsStyle: PropTypes.oneOf(['tabs', 'pills']),
32
33 animation: PropTypes.bool,
34
35 id: requiredForA11y(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
36
37 /**
38 * Callback fired when a Tab is selected.
39 *
40 * ```js
41 * function (
42 * Any eventKey,
43 * SyntheticEvent event?
44 * )
45 * ```
46 *
47 * @controllable activeKey
48 */
49 onSelect: PropTypes.func,
50
51 /**
52 * Wait until the first "enter" transition to mount tabs (add them to the DOM)
53 */
54 mountOnEnter: PropTypes.bool,
55
56 /**
57 * Unmount tabs (remove it from the DOM) when it is no longer visible
58 */
59 unmountOnExit: PropTypes.bool
60};
61
62var defaultProps = {
63 bsStyle: 'tabs',
64 animation: true,
65 mountOnEnter: false,
66 unmountOnExit: false
67};
68
69function getDefaultActiveKey(children) {
70 var defaultActiveKey = void 0;
71 ValidComponentChildren.forEach(children, function (child) {
72 if (defaultActiveKey == null) {
73 defaultActiveKey = child.props.eventKey;
74 }
75 });
76
77 return defaultActiveKey;
78}
79
80var Tabs = function (_React$Component) {
81 _inherits(Tabs, _React$Component);
82
83 function Tabs() {
84 _classCallCheck(this, Tabs);
85
86 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
87 }
88
89 Tabs.prototype.renderTab = function renderTab(child) {
90 var _child$props = child.props,
91 title = _child$props.title,
92 eventKey = _child$props.eventKey,
93 disabled = _child$props.disabled,
94 tabClassName = _child$props.tabClassName;
95
96 if (title == null) {
97 return null;
98 }
99
100 return React.createElement(
101 NavItem,
102 { eventKey: eventKey, disabled: disabled, className: tabClassName },
103 title
104 );
105 };
106
107 Tabs.prototype.render = function render() {
108 var _props = this.props,
109 id = _props.id,
110 onSelect = _props.onSelect,
111 animation = _props.animation,
112 mountOnEnter = _props.mountOnEnter,
113 unmountOnExit = _props.unmountOnExit,
114 bsClass = _props.bsClass,
115 className = _props.className,
116 style = _props.style,
117 children = _props.children,
118 _props$activeKey = _props.activeKey,
119 activeKey = _props$activeKey === undefined ? getDefaultActiveKey(children) : _props$activeKey,
120 props = _objectWithoutProperties(_props, ['id', 'onSelect', 'animation', 'mountOnEnter', 'unmountOnExit', 'bsClass', 'className', 'style', 'children', 'activeKey']);
121
122 return React.createElement(
123 TabContainer,
124 {
125 id: id,
126 activeKey: activeKey,
127 onSelect: onSelect,
128 className: className,
129 style: style
130 },
131 React.createElement(
132 'div',
133 null,
134 React.createElement(
135 Nav,
136 _extends({}, props, { role: 'tablist' }),
137 ValidComponentChildren.map(children, this.renderTab)
138 ),
139 React.createElement(
140 TabContent,
141 {
142 bsClass: bsClass,
143 animation: animation,
144 mountOnEnter: mountOnEnter,
145 unmountOnExit: unmountOnExit
146 },
147 children
148 )
149 )
150 );
151 };
152
153 return Tabs;
154}(React.Component);
155
156Tabs.propTypes = propTypes;
157Tabs.defaultProps = defaultProps;
158
159setBsClass('tab', Tabs);
160
161export default uncontrollable(Tabs, { activeKey: 'onSelect' });
\No newline at end of file