UNPKG

2.85 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3var _excluded = ["id", "onSelect", "transition", "mountOnEnter", "unmountOnExit", "children", "activeKey"];
4import React from 'react';
5import requiredForA11y from 'prop-types-extra/lib/isRequiredForA11y';
6import { useUncontrolled } from 'uncontrollable';
7import Nav from './Nav';
8import NavLink from './NavLink';
9import NavItem from './NavItem';
10import TabContainer from './TabContainer';
11import TabContent from './TabContent';
12import TabPane from './TabPane';
13import { forEach, map } from './ElementChildren';
14var defaultProps = {
15 variant: 'tabs',
16 mountOnEnter: false,
17 unmountOnExit: false
18};
19
20function getDefaultActiveKey(children) {
21 var defaultActiveKey;
22 forEach(children, function (child) {
23 if (defaultActiveKey == null) {
24 defaultActiveKey = child.props.eventKey;
25 }
26 });
27 return defaultActiveKey;
28}
29
30function renderTab(child) {
31 var _child$props = child.props,
32 title = _child$props.title,
33 eventKey = _child$props.eventKey,
34 disabled = _child$props.disabled,
35 tabClassName = _child$props.tabClassName,
36 id = _child$props.id;
37
38 if (title == null) {
39 return null;
40 }
41
42 return /*#__PURE__*/React.createElement(NavItem, {
43 as: NavLink,
44 eventKey: eventKey,
45 disabled: disabled,
46 id: id,
47 className: tabClassName
48 }, title);
49}
50
51var Tabs = function Tabs(props) {
52 var _useUncontrolled = useUncontrolled(props, {
53 activeKey: 'onSelect'
54 }),
55 id = _useUncontrolled.id,
56 onSelect = _useUncontrolled.onSelect,
57 transition = _useUncontrolled.transition,
58 mountOnEnter = _useUncontrolled.mountOnEnter,
59 unmountOnExit = _useUncontrolled.unmountOnExit,
60 children = _useUncontrolled.children,
61 _useUncontrolled$acti = _useUncontrolled.activeKey,
62 activeKey = _useUncontrolled$acti === void 0 ? getDefaultActiveKey(children) : _useUncontrolled$acti,
63 controlledProps = _objectWithoutPropertiesLoose(_useUncontrolled, _excluded);
64
65 return /*#__PURE__*/React.createElement(TabContainer, {
66 id: id,
67 activeKey: activeKey,
68 onSelect: onSelect,
69 transition: transition,
70 mountOnEnter: mountOnEnter,
71 unmountOnExit: unmountOnExit
72 }, /*#__PURE__*/React.createElement(Nav, _extends({}, controlledProps, {
73 role: "tablist",
74 as: "nav"
75 }), map(children, renderTab)), /*#__PURE__*/React.createElement(TabContent, null, map(children, function (child) {
76 var childProps = _extends({}, child.props);
77
78 delete childProps.title;
79 delete childProps.disabled;
80 delete childProps.tabClassName;
81 return /*#__PURE__*/React.createElement(TabPane, childProps);
82 })));
83};
84
85Tabs.defaultProps = defaultProps;
86Tabs.displayName = 'Tabs';
87export default Tabs;
\No newline at end of file