UNPKG

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