forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabContent.js
More file actions
163 lines (135 loc) · 3.88 KB
/
TabContent.js
File metadata and controls
163 lines (135 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import classNames from 'classnames';
import React, { PropTypes } from 'react';
import elementType from 'react-prop-types/lib/elementType';
import { bsClass as setBsClass, prefix, splitBsPropsAndOmit }
from './utils/bootstrapUtils';
const propTypes = {
componentClass: elementType,
/**
* Sets a default animation strategy for all children `<TabPane>`s. Use
* `false` to disable, `true` to enable the default `<Fade>` animation or any
* `<Transition>` component.
*/
animation: PropTypes.oneOfType([
PropTypes.bool, elementType,
]),
/**
* Unmount tabs (remove it from the DOM) when they are no longer visible
*/
unmountOnExit: PropTypes.bool,
};
const defaultProps = {
componentClass: 'div',
animation: true,
unmountOnExit: false,
};
const contextTypes = {
$bs_tabContainer: PropTypes.shape({
activeKey: PropTypes.any,
}),
};
const childContextTypes = {
$bs_tabContent: PropTypes.shape({
bsClass: PropTypes.string,
animation: PropTypes.oneOfType([
PropTypes.bool, elementType,
]),
activeKey: PropTypes.any,
unmountOnExit: PropTypes.bool,
onPaneEnter: PropTypes.func.isRequired,
onPaneExited: PropTypes.func.isRequired,
exiting: PropTypes.bool.isRequired,
}),
};
class TabContent extends React.Component {
constructor(props, context) {
super(props, context);
this.handlePaneEnter = this.handlePaneEnter.bind(this);
this.handlePaneExited = this.handlePaneExited.bind(this);
// Active entries in state will be `null` unless `animation` is set. Need
// to track active child in case keys swap and the active child changes
// but the active key does not.
this.state = {
activeKey: null,
activeChild: null,
};
}
getChildContext() {
const { bsClass, animation, unmountOnExit } = this.props;
const stateActiveKey = this.state.activeKey;
const containerActiveKey = this.getContainerActiveKey();
const activeKey =
stateActiveKey != null ? stateActiveKey : containerActiveKey;
const exiting =
stateActiveKey != null && stateActiveKey !== containerActiveKey;
return {
$bs_tabContent: {
bsClass,
animation,
activeKey,
unmountOnExit,
onPaneEnter: this.handlePaneEnter,
onPaneExited: this.handlePaneExited,
exiting,
},
};
}
componentWillReceiveProps(nextProps) {
if (!nextProps.animation && this.state.activeChild) {
this.setState({ activeKey: null, activeChild: null });
}
}
componentWillUnmount() {
this.isUnmounted = true;
}
handlePaneEnter(child, childKey) {
if (!this.props.animation) {
return false;
}
// It's possible that this child should be transitioning out.
if (childKey !== this.getContainerActiveKey()) {
return false;
}
this.setState({
activeKey: childKey,
activeChild: child,
});
return true;
}
handlePaneExited(child) {
// This might happen as everything is unmounting.
if (this.isUnmounted) {
return;
}
this.setState(({ activeChild }) => {
if (activeChild !== child) {
return null;
}
return {
activeKey: null,
activeChild: null,
};
});
}
getContainerActiveKey() {
const tabContainer = this.context.$bs_tabContainer;
return tabContainer && tabContainer.activeKey;
}
render() {
const { componentClass: Component, className, ...props } = this.props;
const [bsProps, elementProps] = splitBsPropsAndOmit(props, [
'animation', 'unmountOnExit',
]);
return (
<Component
{...elementProps}
className={classNames(className, prefix(bsProps, 'content'))}
/>
);
}
}
TabContent.propTypes = propTypes;
TabContent.defaultProps = defaultProps;
TabContent.contextTypes = contextTypes;
TabContent.childContextTypes = childContextTypes;
export default setBsClass('tab', TabContent);