UNPKG

9.49 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports.default = void 0;
7
8var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
9
10var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutPropertiesLoose"));
11
12var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/inheritsLoose"));
13
14var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/assertThisInitialized"));
15
16var _classnames = _interopRequireDefault(require("classnames"));
17
18var _react = _interopRequireDefault(require("react"));
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _elementType = _interopRequireDefault(require("prop-types-extra/lib/elementType"));
23
24var _warning = _interopRequireDefault(require("warning"));
25
26var _bootstrapUtils = require("./utils/bootstrapUtils");
27
28var _createChainedFunction = _interopRequireDefault(require("./utils/createChainedFunction"));
29
30var _Fade = _interopRequireDefault(require("./Fade"));
31
32var propTypes = {
33 /**
34 * Uniquely identify the `<TabPane>` among its siblings.
35 */
36 eventKey: _propTypes.default.any,
37
38 /**
39 * Use animation when showing or hiding `<TabPane>`s. Use `false` to disable,
40 * `true` to enable the default `<Fade>` animation or
41 * a react-transition-group v2 `<Transition/>` component.
42 */
43 animation: _propTypes.default.oneOfType([_propTypes.default.bool, _elementType.default]),
44
45 /** @private * */
46 id: _propTypes.default.string,
47
48 /** @private * */
49 'aria-labelledby': _propTypes.default.string,
50
51 /**
52 * If not explicitly specified and rendered in the context of a
53 * `<TabContent>`, the `bsClass` of the `<TabContent>` suffixed by `-pane`.
54 * If otherwise not explicitly specified, `tab-pane`.
55 */
56 bsClass: _propTypes.default.string,
57
58 /**
59 * Transition onEnter callback when animation is not `false`
60 */
61 onEnter: _propTypes.default.func,
62
63 /**
64 * Transition onEntering callback when animation is not `false`
65 */
66 onEntering: _propTypes.default.func,
67
68 /**
69 * Transition onEntered callback when animation is not `false`
70 */
71 onEntered: _propTypes.default.func,
72
73 /**
74 * Transition onExit callback when animation is not `false`
75 */
76 onExit: _propTypes.default.func,
77
78 /**
79 * Transition onExiting callback when animation is not `false`
80 */
81 onExiting: _propTypes.default.func,
82
83 /**
84 * Transition onExited callback when animation is not `false`
85 */
86 onExited: _propTypes.default.func,
87
88 /**
89 * Wait until the first "enter" transition to mount the tab (add it to the DOM)
90 */
91 mountOnEnter: _propTypes.default.bool,
92
93 /**
94 * Unmount the tab (remove it from the DOM) when it is no longer visible
95 */
96 unmountOnExit: _propTypes.default.bool
97};
98var contextTypes = {
99 $bs_tabContainer: _propTypes.default.shape({
100 getTabId: _propTypes.default.func,
101 getPaneId: _propTypes.default.func
102 }),
103 $bs_tabContent: _propTypes.default.shape({
104 bsClass: _propTypes.default.string,
105 animation: _propTypes.default.oneOfType([_propTypes.default.bool, _elementType.default]),
106 activeKey: _propTypes.default.any,
107 mountOnEnter: _propTypes.default.bool,
108 unmountOnExit: _propTypes.default.bool,
109 onPaneEnter: _propTypes.default.func.isRequired,
110 onPaneExited: _propTypes.default.func.isRequired,
111 exiting: _propTypes.default.bool.isRequired
112 })
113};
114/**
115 * We override the `<TabContainer>` context so `<Nav>`s in `<TabPane>`s don't
116 * conflict with the top level one.
117 */
118
119var childContextTypes = {
120 $bs_tabContainer: _propTypes.default.oneOf([null])
121};
122
123var TabPane =
124/*#__PURE__*/
125function (_React$Component) {
126 (0, _inheritsLoose2.default)(TabPane, _React$Component);
127
128 function TabPane(props, context) {
129 var _this;
130
131 _this = _React$Component.call(this, props, context) || this;
132 _this.handleEnter = _this.handleEnter.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
133 _this.handleExited = _this.handleExited.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
134 _this.in = false;
135 return _this;
136 }
137
138 var _proto = TabPane.prototype;
139
140 _proto.getChildContext = function getChildContext() {
141 return {
142 $bs_tabContainer: null
143 };
144 };
145
146 _proto.componentDidMount = function componentDidMount() {
147 if (this.shouldBeIn()) {
148 // In lieu of the action event firing.
149 this.handleEnter();
150 }
151 };
152
153 _proto.componentDidUpdate = function componentDidUpdate() {
154 if (this.in) {
155 if (!this.shouldBeIn()) {
156 // We shouldn't be active any more. Notify the parent.
157 this.handleExited();
158 }
159 } else if (this.shouldBeIn()) {
160 // We are the active child. Notify the parent.
161 this.handleEnter();
162 }
163 };
164
165 _proto.componentWillUnmount = function componentWillUnmount() {
166 if (this.in) {
167 // In lieu of the action event firing.
168 this.handleExited();
169 }
170 };
171
172 _proto.getAnimation = function getAnimation() {
173 if (this.props.animation != null) {
174 return this.props.animation;
175 }
176
177 var tabContent = this.context.$bs_tabContent;
178 return tabContent && tabContent.animation;
179 };
180
181 _proto.handleEnter = function handleEnter() {
182 var tabContent = this.context.$bs_tabContent;
183
184 if (!tabContent) {
185 return;
186 }
187
188 this.in = tabContent.onPaneEnter(this, this.props.eventKey);
189 };
190
191 _proto.handleExited = function handleExited() {
192 var tabContent = this.context.$bs_tabContent;
193
194 if (!tabContent) {
195 return;
196 }
197
198 tabContent.onPaneExited(this);
199 this.in = false;
200 };
201
202 _proto.isActive = function isActive() {
203 var tabContent = this.context.$bs_tabContent;
204 var activeKey = tabContent && tabContent.activeKey;
205 return this.props.eventKey === activeKey;
206 };
207
208 _proto.shouldBeIn = function shouldBeIn() {
209 return this.getAnimation() && this.isActive();
210 };
211
212 _proto.render = function render() {
213 var _this$props = this.props,
214 eventKey = _this$props.eventKey,
215 className = _this$props.className,
216 onEnter = _this$props.onEnter,
217 onEntering = _this$props.onEntering,
218 onEntered = _this$props.onEntered,
219 onExit = _this$props.onExit,
220 onExiting = _this$props.onExiting,
221 onExited = _this$props.onExited,
222 propsMountOnEnter = _this$props.mountOnEnter,
223 propsUnmountOnExit = _this$props.unmountOnExit,
224 props = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["eventKey", "className", "onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited", "mountOnEnter", "unmountOnExit"]);
225 var _this$context = this.context,
226 tabContent = _this$context.$bs_tabContent,
227 tabContainer = _this$context.$bs_tabContainer;
228
229 var _splitBsPropsAndOmit = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['animation']),
230 bsProps = _splitBsPropsAndOmit[0],
231 elementProps = _splitBsPropsAndOmit[1];
232
233 var active = this.isActive();
234 var animation = this.getAnimation();
235 var mountOnEnter = propsMountOnEnter != null ? propsMountOnEnter : tabContent && tabContent.mountOnEnter;
236 var unmountOnExit = propsUnmountOnExit != null ? propsUnmountOnExit : tabContent && tabContent.unmountOnExit;
237
238 if (!active && !animation && unmountOnExit) {
239 return null;
240 }
241
242 var Transition = animation === true ? _Fade.default : animation || null;
243
244 if (tabContent) {
245 bsProps.bsClass = (0, _bootstrapUtils.prefix)(tabContent, 'pane');
246 }
247
248 var classes = (0, _extends2.default)({}, (0, _bootstrapUtils.getClassSet)(bsProps), {
249 active: active
250 });
251
252 if (tabContainer) {
253 process.env.NODE_ENV !== "production" ? (0, _warning.default)(!elementProps.id && !elementProps['aria-labelledby'], 'In the context of a `<TabContainer>`, `<TabPanes>` are given ' + 'generated `id` and `aria-labelledby` attributes for the sake of ' + 'proper component accessibility. Any provided ones will be ignored. ' + 'To control these attributes directly provide a `generateChildId` ' + 'prop to the parent `<TabContainer>`.') : void 0;
254 elementProps.id = tabContainer.getPaneId(eventKey);
255 elementProps['aria-labelledby'] = tabContainer.getTabId(eventKey);
256 }
257
258 var pane = _react.default.createElement("div", (0, _extends2.default)({}, elementProps, {
259 role: "tabpanel",
260 "aria-hidden": !active,
261 className: (0, _classnames.default)(className, classes)
262 }));
263
264 if (Transition) {
265 var exiting = tabContent && tabContent.exiting;
266 return _react.default.createElement(Transition, {
267 in: active && !exiting,
268 onEnter: (0, _createChainedFunction.default)(this.handleEnter, onEnter),
269 onEntering: onEntering,
270 onEntered: onEntered,
271 onExit: onExit,
272 onExiting: onExiting,
273 onExited: (0, _createChainedFunction.default)(this.handleExited, onExited),
274 mountOnEnter: mountOnEnter,
275 unmountOnExit: unmountOnExit
276 }, pane);
277 }
278
279 return pane;
280 };
281
282 return TabPane;
283}(_react.default.Component);
284
285TabPane.propTypes = propTypes;
286TabPane.contextTypes = contextTypes;
287TabPane.childContextTypes = childContextTypes;
288
289var _default = (0, _bootstrapUtils.bsClass)('tab-pane', TabPane);
290
291exports.default = _default;
292module.exports = exports["default"];
\No newline at end of file