This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
TabbedArea.js
139 lines (116 loc) · 3.76 KB
/
TabbedArea.js
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
var React = require('react');
var BootstrapMixin = require('./BootstrapMixin');
var cloneWithProps = require('./utils/cloneWithProps');
var ValidComponentChildren = require('./utils/ValidComponentChildren');
var Nav = require('./Nav');
var NavItem = require('./NavItem');
function getDefaultActiveKeyFromChildren(children) {
var defaultActiveKey;
ValidComponentChildren.forEach(children, function(child) {
if (defaultActiveKey == null) {
defaultActiveKey = child.props.eventKey;
}
});
return defaultActiveKey;
}
var TabbedArea = React.createClass({displayName: "TabbedArea",
mixins: [BootstrapMixin],
propTypes: {
bsStyle: React.PropTypes.oneOf(['tabs','pills']),
animation: React.PropTypes.bool,
onSelect: React.PropTypes.func
},
getDefaultProps: function () {
return {
bsStyle: "tabs",
animation: true
};
},
getInitialState: function () {
var defaultActiveKey = this.props.defaultActiveKey != null ?
this.props.defaultActiveKey : getDefaultActiveKeyFromChildren(this.props.children);
// TODO: In __DEV__ mode warn via `console.warn` if no `defaultActiveKey` has
// been set by this point, invalid children or missing key properties are likely the cause.
return {
activeKey: defaultActiveKey,
previousActiveKey: null
};
},
componentWillReceiveProps: function (nextProps) {
if (nextProps.activeKey != null && nextProps.activeKey !== this.props.activeKey) {
this.setState({
previousActiveKey: this.props.activeKey
});
}
},
handlePaneAnimateOutEnd: function () {
this.setState({
previousActiveKey: null
});
},
render: function () {
var activeKey =
this.props.activeKey != null ? this.props.activeKey : this.state.activeKey;
function renderTabIfSet(child) {
return child.props.tab != null ? this.renderTab(child) : null;
}
var nav = (
React.createElement(Nav, React.__spread({}, this.props, {activeKey: activeKey, onSelect: this.handleSelect, ref: "tabs"}),
ValidComponentChildren.map(this.props.children, renderTabIfSet, this)
)
);
return (
React.createElement("div", null,
nav,
React.createElement("div", {id: this.props.id, className: "tab-content", ref: "panes"},
ValidComponentChildren.map(this.props.children, this.renderPane)
)
)
);
},
getActiveKey: function () {
return this.props.activeKey != null ? this.props.activeKey : this.state.activeKey;
},
renderPane: function (child, index) {
var activeKey = this.getActiveKey();
return cloneWithProps(
child,
{
active: (child.props.eventKey === activeKey &&
(this.state.previousActiveKey == null || !this.props.animation)),
ref: child.ref,
key: child.key ? child.key : index,
animation: this.props.animation,
onAnimateOutEnd: (this.state.previousActiveKey != null &&
child.props.eventKey === this.state.previousActiveKey) ? this.handlePaneAnimateOutEnd: null
}
);
},
renderTab: function (child) {
var key = child.props.eventKey;
return (
React.createElement(NavItem, {
ref: 'tab' + key,
eventKey: key},
child.props.tab
)
);
},
shouldComponentUpdate: function() {
// Defer any updates to this component during the `onSelect` handler.
return !this._isChanging;
},
handleSelect: function (key) {
if (this.props.onSelect) {
this._isChanging = true;
this.props.onSelect(key);
this._isChanging = false;
} else if (key !== this.getActiveKey()) {
this.setState({
activeKey: key,
previousActiveKey: this.getActiveKey()
});
}
}
});
module.exports = TabbedArea;