UNPKG

6.38 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 _bootstrapUtils = require("./utils/bootstrapUtils");
25
26var propTypes = {
27 componentClass: _elementType.default,
28
29 /**
30 * Sets a default animation strategy for all children `<TabPane>`s. Use
31 * `false` to disable, `true` to enable the default `<Fade>` animation or
32 * a react-transition-group v2 `<Transition/>` component.
33 */
34 animation: _propTypes.default.oneOfType([_propTypes.default.bool, _elementType.default]),
35
36 /**
37 * Wait until the first "enter" transition to mount tabs (add them to the DOM)
38 */
39 mountOnEnter: _propTypes.default.bool,
40
41 /**
42 * Unmount tabs (remove it from the DOM) when they are no longer visible
43 */
44 unmountOnExit: _propTypes.default.bool
45};
46var defaultProps = {
47 componentClass: 'div',
48 animation: true,
49 mountOnEnter: false,
50 unmountOnExit: false
51};
52var contextTypes = {
53 $bs_tabContainer: _propTypes.default.shape({
54 activeKey: _propTypes.default.any
55 })
56};
57var childContextTypes = {
58 $bs_tabContent: _propTypes.default.shape({
59 bsClass: _propTypes.default.string,
60 animation: _propTypes.default.oneOfType([_propTypes.default.bool, _elementType.default]),
61 activeKey: _propTypes.default.any,
62 mountOnEnter: _propTypes.default.bool,
63 unmountOnExit: _propTypes.default.bool,
64 onPaneEnter: _propTypes.default.func.isRequired,
65 onPaneExited: _propTypes.default.func.isRequired,
66 exiting: _propTypes.default.bool.isRequired
67 })
68};
69
70var TabContent =
71/*#__PURE__*/
72function (_React$Component) {
73 (0, _inheritsLoose2.default)(TabContent, _React$Component);
74
75 function TabContent(props, context) {
76 var _this;
77
78 _this = _React$Component.call(this, props, context) || this;
79 _this.handlePaneEnter = _this.handlePaneEnter.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
80 _this.handlePaneExited = _this.handlePaneExited.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this))); // Active entries in state will be `null` unless `animation` is set. Need
81 // to track active child in case keys swap and the active child changes
82 // but the active key does not.
83
84 _this.state = {
85 activeKey: null,
86 activeChild: null
87 };
88 return _this;
89 }
90
91 var _proto = TabContent.prototype;
92
93 _proto.getChildContext = function getChildContext() {
94 var _this$props = this.props,
95 bsClass = _this$props.bsClass,
96 animation = _this$props.animation,
97 mountOnEnter = _this$props.mountOnEnter,
98 unmountOnExit = _this$props.unmountOnExit;
99 var stateActiveKey = this.state.activeKey;
100 var containerActiveKey = this.getContainerActiveKey();
101 var activeKey = stateActiveKey != null ? stateActiveKey : containerActiveKey;
102 var exiting = stateActiveKey != null && stateActiveKey !== containerActiveKey;
103 return {
104 $bs_tabContent: {
105 bsClass: bsClass,
106 animation: animation,
107 activeKey: activeKey,
108 mountOnEnter: mountOnEnter,
109 unmountOnExit: unmountOnExit,
110 onPaneEnter: this.handlePaneEnter,
111 onPaneExited: this.handlePaneExited,
112 exiting: exiting
113 }
114 };
115 };
116
117 _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
118 if (!nextProps.animation && this.state.activeChild) {
119 this.setState({
120 activeKey: null,
121 activeChild: null
122 });
123 }
124 };
125
126 _proto.componentWillUnmount = function componentWillUnmount() {
127 this.isUnmounted = true;
128 };
129
130 _proto.getContainerActiveKey = function getContainerActiveKey() {
131 var tabContainer = this.context.$bs_tabContainer;
132 return tabContainer && tabContainer.activeKey;
133 };
134
135 _proto.handlePaneEnter = function handlePaneEnter(child, childKey) {
136 if (!this.props.animation) {
137 return false;
138 } // It's possible that this child should be transitioning out.
139
140
141 if (childKey !== this.getContainerActiveKey()) {
142 return false;
143 }
144
145 this.setState({
146 activeKey: childKey,
147 activeChild: child
148 });
149 return true;
150 };
151
152 _proto.handlePaneExited = function handlePaneExited(child) {
153 // This might happen as everything is unmounting.
154 if (this.isUnmounted) {
155 return;
156 }
157
158 this.setState(function (_ref) {
159 var activeChild = _ref.activeChild;
160
161 if (activeChild !== child) {
162 return null;
163 }
164
165 return {
166 activeKey: null,
167 activeChild: null
168 };
169 });
170 };
171
172 _proto.render = function render() {
173 var _this$props2 = this.props,
174 Component = _this$props2.componentClass,
175 className = _this$props2.className,
176 props = (0, _objectWithoutPropertiesLoose2.default)(_this$props2, ["componentClass", "className"]);
177
178 var _splitBsPropsAndOmit = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['animation', 'mountOnEnter', 'unmountOnExit']),
179 bsProps = _splitBsPropsAndOmit[0],
180 elementProps = _splitBsPropsAndOmit[1];
181
182 return _react.default.createElement(Component, (0, _extends2.default)({}, elementProps, {
183 className: (0, _classnames.default)(className, (0, _bootstrapUtils.prefix)(bsProps, 'content'))
184 }));
185 };
186
187 return TabContent;
188}(_react.default.Component);
189
190TabContent.propTypes = propTypes;
191TabContent.defaultProps = defaultProps;
192TabContent.contextTypes = contextTypes;
193TabContent.childContextTypes = childContextTypes;
194
195var _default = (0, _bootstrapUtils.bsClass)('tab', TabContent);
196
197exports.default = _default;
198module.exports = exports["default"];
\No newline at end of file