UNPKG

6.66 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _extends2 = require('babel-runtime/helpers/extends');
6
7var _extends3 = _interopRequireDefault(_extends2);
8
9var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
10
11var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
12
13var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
14
15var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
16
17var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
18
19var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
20
21var _inherits2 = require('babel-runtime/helpers/inherits');
22
23var _inherits3 = _interopRequireDefault(_inherits2);
24
25var _classnames = require('classnames');
26
27var _classnames2 = _interopRequireDefault(_classnames);
28
29var _react = require('react');
30
31var _react2 = _interopRequireDefault(_react);
32
33var _propTypes = require('prop-types');
34
35var _propTypes2 = _interopRequireDefault(_propTypes);
36
37var _elementType = require('prop-types-extra/lib/elementType');
38
39var _elementType2 = _interopRequireDefault(_elementType);
40
41var _bootstrapUtils = require('./utils/bootstrapUtils');
42
43function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
44
45var propTypes = {
46 componentClass: _elementType2.default,
47
48 /**
49 * Sets a default animation strategy for all children `<TabPane>`s. Use
50 * `false` to disable, `true` to enable the default `<Fade>` animation or
51 * a react-transition-group v2 `<Transition/>` component.
52 */
53 animation: _propTypes2.default.oneOfType([_propTypes2.default.bool, _elementType2.default]),
54
55 /**
56 * Wait until the first "enter" transition to mount tabs (add them to the DOM)
57 */
58 mountOnEnter: _propTypes2.default.bool,
59
60 /**
61 * Unmount tabs (remove it from the DOM) when they are no longer visible
62 */
63 unmountOnExit: _propTypes2.default.bool
64};
65
66var defaultProps = {
67 componentClass: 'div',
68 animation: true,
69 mountOnEnter: false,
70 unmountOnExit: false
71};
72
73var contextTypes = {
74 $bs_tabContainer: _propTypes2.default.shape({
75 activeKey: _propTypes2.default.any
76 })
77};
78
79var childContextTypes = {
80 $bs_tabContent: _propTypes2.default.shape({
81 bsClass: _propTypes2.default.string,
82 animation: _propTypes2.default.oneOfType([_propTypes2.default.bool, _elementType2.default]),
83 activeKey: _propTypes2.default.any,
84 mountOnEnter: _propTypes2.default.bool,
85 unmountOnExit: _propTypes2.default.bool,
86 onPaneEnter: _propTypes2.default.func.isRequired,
87 onPaneExited: _propTypes2.default.func.isRequired,
88 exiting: _propTypes2.default.bool.isRequired
89 })
90};
91
92var TabContent = function (_React$Component) {
93 (0, _inherits3.default)(TabContent, _React$Component);
94
95 function TabContent(props, context) {
96 (0, _classCallCheck3.default)(this, TabContent);
97
98 var _this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call(this, props, context));
99
100 _this.handlePaneEnter = _this.handlePaneEnter.bind(_this);
101 _this.handlePaneExited = _this.handlePaneExited.bind(_this);
102
103 // Active entries in state will be `null` unless `animation` is set. Need
104 // to track active child in case keys swap and the active child changes
105 // but the active key does not.
106 _this.state = {
107 activeKey: null,
108 activeChild: null
109 };
110 return _this;
111 }
112
113 TabContent.prototype.getChildContext = function getChildContext() {
114 var _props = this.props,
115 bsClass = _props.bsClass,
116 animation = _props.animation,
117 mountOnEnter = _props.mountOnEnter,
118 unmountOnExit = _props.unmountOnExit;
119
120
121 var stateActiveKey = this.state.activeKey;
122 var containerActiveKey = this.getContainerActiveKey();
123
124 var activeKey = stateActiveKey != null ? stateActiveKey : containerActiveKey;
125 var exiting = stateActiveKey != null && stateActiveKey !== containerActiveKey;
126
127 return {
128 $bs_tabContent: {
129 bsClass: bsClass,
130 animation: animation,
131 activeKey: activeKey,
132 mountOnEnter: mountOnEnter,
133 unmountOnExit: unmountOnExit,
134 onPaneEnter: this.handlePaneEnter,
135 onPaneExited: this.handlePaneExited,
136 exiting: exiting
137 }
138 };
139 };
140
141 TabContent.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
142 if (!nextProps.animation && this.state.activeChild) {
143 this.setState({ activeKey: null, activeChild: null });
144 }
145 };
146
147 TabContent.prototype.componentWillUnmount = function componentWillUnmount() {
148 this.isUnmounted = true;
149 };
150
151 TabContent.prototype.getContainerActiveKey = function getContainerActiveKey() {
152 var tabContainer = this.context.$bs_tabContainer;
153 return tabContainer && tabContainer.activeKey;
154 };
155
156 TabContent.prototype.handlePaneEnter = function handlePaneEnter(child, childKey) {
157 if (!this.props.animation) {
158 return false;
159 }
160
161 // It's possible that this child should be transitioning out.
162 if (childKey !== this.getContainerActiveKey()) {
163 return false;
164 }
165
166 this.setState({
167 activeKey: childKey,
168 activeChild: child
169 });
170
171 return true;
172 };
173
174 TabContent.prototype.handlePaneExited = function handlePaneExited(child) {
175 // This might happen as everything is unmounting.
176 if (this.isUnmounted) {
177 return;
178 }
179
180 this.setState(function (_ref) {
181 var activeChild = _ref.activeChild;
182
183 if (activeChild !== child) {
184 return null;
185 }
186
187 return {
188 activeKey: null,
189 activeChild: null
190 };
191 });
192 };
193
194 TabContent.prototype.render = function render() {
195 var _props2 = this.props,
196 Component = _props2.componentClass,
197 className = _props2.className,
198 props = (0, _objectWithoutProperties3.default)(_props2, ['componentClass', 'className']);
199
200 var _splitBsPropsAndOmit = (0, _bootstrapUtils.splitBsPropsAndOmit)(props, ['animation', 'mountOnEnter', 'unmountOnExit']),
201 bsProps = _splitBsPropsAndOmit[0],
202 elementProps = _splitBsPropsAndOmit[1];
203
204 return _react2.default.createElement(Component, (0, _extends3.default)({}, elementProps, {
205 className: (0, _classnames2.default)(className, (0, _bootstrapUtils.prefix)(bsProps, 'content'))
206 }));
207 };
208
209 return TabContent;
210}(_react2.default.Component);
211
212TabContent.propTypes = propTypes;
213TabContent.defaultProps = defaultProps;
214TabContent.contextTypes = contextTypes;
215TabContent.childContextTypes = childContextTypes;
216
217exports.default = (0, _bootstrapUtils.bsClass)('tab', TabContent);
218module.exports = exports['default'];
\No newline at end of file