UNPKG

4.4 kBJavaScriptView Raw
1import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
3import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
4import React from 'react';
5import PropTypes from 'prop-types';
6import requiredForA11y from 'prop-types-extra/lib/isRequiredForA11y';
7import uncontrollable from 'uncontrollable';
8import elementType from 'prop-types-extra/lib/elementType';
9import Nav from './Nav';
10import NavItem from './NavItem';
11import UncontrolledTabContainer from './TabContainer';
12import TabContent from './TabContent';
13import { bsClass as setBsClass } from './utils/bootstrapUtils';
14import ValidComponentChildren from './utils/ValidComponentChildren';
15var TabContainer = UncontrolledTabContainer.ControlledComponent;
16var propTypes = {
17 /**
18 * Mark the Tab with a matching `eventKey` as active.
19 *
20 * @controllable onSelect
21 */
22 activeKey: PropTypes.any,
23
24 /**
25 * Navigation style
26 */
27 bsStyle: PropTypes.oneOf(['tabs', 'pills']),
28
29 /**
30 * Sets a default animation strategy. Use `false` to disable, `true`
31 * to enable the default `<Fade>` animation, or a react-transition-group
32 * v2 `<Transition/>` component.
33 */
34 animation: PropTypes.oneOfType([PropTypes.bool, elementType]),
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};
61var defaultProps = {
62 bsStyle: 'tabs',
63 animation: true,
64 mountOnEnter: false,
65 unmountOnExit: false
66};
67
68function getDefaultActiveKey(children) {
69 var defaultActiveKey;
70 ValidComponentChildren.forEach(children, function (child) {
71 if (defaultActiveKey == null) {
72 defaultActiveKey = child.props.eventKey;
73 }
74 });
75 return defaultActiveKey;
76}
77
78var Tabs =
79/*#__PURE__*/
80function (_React$Component) {
81 _inheritsLoose(Tabs, _React$Component);
82
83 function Tabs() {
84 return _React$Component.apply(this, arguments) || this;
85 }
86
87 var _proto = Tabs.prototype;
88
89 _proto.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(NavItem, {
101 eventKey: eventKey,
102 disabled: disabled,
103 className: tabClassName
104 }, title);
105 };
106
107 _proto.render = function render() {
108 var _this$props = this.props,
109 id = _this$props.id,
110 onSelect = _this$props.onSelect,
111 animation = _this$props.animation,
112 mountOnEnter = _this$props.mountOnEnter,
113 unmountOnExit = _this$props.unmountOnExit,
114 bsClass = _this$props.bsClass,
115 className = _this$props.className,
116 style = _this$props.style,
117 children = _this$props.children,
118 _this$props$activeKey = _this$props.activeKey,
119 activeKey = _this$props$activeKey === void 0 ? getDefaultActiveKey(children) : _this$props$activeKey,
120 props = _objectWithoutPropertiesLoose(_this$props, ["id", "onSelect", "animation", "mountOnEnter", "unmountOnExit", "bsClass", "className", "style", "children", "activeKey"]);
121
122 return React.createElement(TabContainer, {
123 id: id,
124 activeKey: activeKey,
125 onSelect: onSelect,
126 className: className,
127 style: style
128 }, React.createElement("div", null, React.createElement(Nav, _extends({}, props, {
129 role: "tablist"
130 }), ValidComponentChildren.map(children, this.renderTab)), React.createElement(TabContent, {
131 bsClass: bsClass,
132 animation: animation,
133 mountOnEnter: mountOnEnter,
134 unmountOnExit: unmountOnExit
135 }, children)));
136 };
137
138 return Tabs;
139}(React.Component);
140
141Tabs.propTypes = propTypes;
142Tabs.defaultProps = defaultProps;
143setBsClass('tab', Tabs);
144export default uncontrollable(Tabs, {
145 activeKey: 'onSelect'
146});
\No newline at end of file