UNPKG

2.35 kBJavaScriptView Raw
1import * as React from 'react';
2import { useUncontrolled } from 'uncontrollable';
3import BaseTabs from '@restart/ui/Tabs';
4import Nav from './Nav';
5import NavLink from './NavLink';
6import NavItem from './NavItem';
7import TabContent from './TabContent';
8import TabPane from './TabPane';
9import { forEach, map } from './ElementChildren';
10import getTabTransitionComponent from './getTabTransitionComponent';
11import { jsx as _jsx } from "react/jsx-runtime";
12import { jsxs as _jsxs } from "react/jsx-runtime";
13function getDefaultActiveKey(children) {
14 let defaultActiveKey;
15 forEach(children, child => {
16 if (defaultActiveKey == null) {
17 defaultActiveKey = child.props.eventKey;
18 }
19 });
20 return defaultActiveKey;
21}
22function renderTab(child) {
23 const {
24 title,
25 eventKey,
26 disabled,
27 tabClassName,
28 tabAttrs,
29 id
30 } = child.props;
31 if (title == null) {
32 return null;
33 }
34 return /*#__PURE__*/_jsx(NavItem, {
35 as: "li",
36 role: "presentation",
37 children: /*#__PURE__*/_jsx(NavLink, {
38 as: "button",
39 type: "button",
40 eventKey: eventKey,
41 disabled: disabled,
42 id: id,
43 className: tabClassName,
44 ...tabAttrs,
45 children: title
46 })
47 });
48}
49const Tabs = props => {
50 const {
51 id,
52 onSelect,
53 transition,
54 mountOnEnter = false,
55 unmountOnExit = false,
56 variant = 'tabs',
57 children,
58 activeKey = getDefaultActiveKey(children),
59 ...controlledProps
60 } = useUncontrolled(props, {
61 activeKey: 'onSelect'
62 });
63 return /*#__PURE__*/_jsxs(BaseTabs, {
64 id: id,
65 activeKey: activeKey,
66 onSelect: onSelect,
67 transition: getTabTransitionComponent(transition),
68 mountOnEnter: mountOnEnter,
69 unmountOnExit: unmountOnExit,
70 children: [/*#__PURE__*/_jsx(Nav, {
71 ...controlledProps,
72 role: "tablist",
73 as: "ul",
74 variant: variant,
75 children: map(children, renderTab)
76 }), /*#__PURE__*/_jsx(TabContent, {
77 children: map(children, child => {
78 const childProps = {
79 ...child.props
80 };
81 delete childProps.title;
82 delete childProps.disabled;
83 delete childProps.tabClassName;
84 delete childProps.tabAttrs;
85 return /*#__PURE__*/_jsx(TabPane, {
86 ...childProps
87 });
88 })
89 })]
90 });
91};
92Tabs.displayName = 'Tabs';
93export default Tabs;
\No newline at end of file