UNPKG

3.43 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import classNames from 'classnames';
4import React, { useContext } from 'react';
5import { useBootstrapPrefix } from './ThemeProvider';
6import TabContext from './TabContext';
7import SelectableContext, { makeEventKey } from './SelectableContext';
8import Fade from './Fade';
9
10function useTabContext(props) {
11 var context = useContext(TabContext);
12 if (!context) return props;
13
14 var activeKey = context.activeKey,
15 getControlledId = context.getControlledId,
16 getControllerId = context.getControllerId,
17 rest = _objectWithoutPropertiesLoose(context, ["activeKey", "getControlledId", "getControllerId"]);
18
19 var shouldTransition = props.transition !== false && rest.transition !== false;
20 var key = makeEventKey(props.eventKey);
21 return _extends({}, props, {
22 active: props.active == null && key != null ? makeEventKey(activeKey) === key : props.active,
23 id: getControlledId(props.eventKey),
24 'aria-labelledby': getControllerId(props.eventKey),
25 transition: shouldTransition && (props.transition || rest.transition || Fade),
26 mountOnEnter: props.mountOnEnter != null ? props.mountOnEnter : rest.mountOnEnter,
27 unmountOnExit: props.unmountOnExit != null ? props.unmountOnExit : rest.unmountOnExit
28 });
29}
30
31var TabPane = /*#__PURE__*/React.forwardRef(function (props, ref) {
32 var _useTabContext = useTabContext(props),
33 bsPrefix = _useTabContext.bsPrefix,
34 className = _useTabContext.className,
35 active = _useTabContext.active,
36 onEnter = _useTabContext.onEnter,
37 onEntering = _useTabContext.onEntering,
38 onEntered = _useTabContext.onEntered,
39 onExit = _useTabContext.onExit,
40 onExiting = _useTabContext.onExiting,
41 onExited = _useTabContext.onExited,
42 mountOnEnter = _useTabContext.mountOnEnter,
43 unmountOnExit = _useTabContext.unmountOnExit,
44 Transition = _useTabContext.transition,
45 _useTabContext$as = _useTabContext.as,
46 Component = _useTabContext$as === void 0 ? 'div' : _useTabContext$as,
47 _ = _useTabContext.eventKey,
48 rest = _objectWithoutPropertiesLoose(_useTabContext, ["bsPrefix", "className", "active", "onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited", "mountOnEnter", "unmountOnExit", "transition", "as", "eventKey"]);
49
50 var prefix = useBootstrapPrefix(bsPrefix, 'tab-pane');
51 if (!active && !Transition && unmountOnExit) return null;
52 var pane = /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
53 ref: ref,
54 role: "tabpanel",
55 "aria-hidden": !active,
56 className: classNames(className, prefix, {
57 active: active
58 })
59 }));
60 if (Transition) pane = /*#__PURE__*/React.createElement(Transition, {
61 in: active,
62 onEnter: onEnter,
63 onEntering: onEntering,
64 onEntered: onEntered,
65 onExit: onExit,
66 onExiting: onExiting,
67 onExited: onExited,
68 mountOnEnter: mountOnEnter,
69 unmountOnExit: unmountOnExit
70 }, pane); // We provide an empty the TabContext so `<Nav>`s in `<TabPane>`s don't
71 // conflict with the top level one.
72
73 return /*#__PURE__*/React.createElement(TabContext.Provider, {
74 value: null
75 }, /*#__PURE__*/React.createElement(SelectableContext.Provider, {
76 value: null
77 }, pane));
78});
79TabPane.displayName = 'TabPane';
80export default TabPane;
\No newline at end of file