-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Tabs.js
255 lines (220 loc) · 6.89 KB
/
Tabs.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import React, {Component, createElement, cloneElement, Children, isValidElement} from 'react';
import PropTypes from 'prop-types';
import warning from 'warning';
import TabTemplate from './TabTemplate';
import InkBar from './InkBar';
function getStyles(props, context) {
const {tabs} = context.muiTheme;
return {
tabItemContainer: {
width: '100%',
backgroundColor: tabs.backgroundColor,
whiteSpace: 'nowrap',
display: 'flex',
},
};
}
class Tabs extends Component {
static propTypes = {
/**
* Should be used to pass `Tab` components.
*/
children: PropTypes.node,
/**
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* The css class name of the content's container.
*/
contentContainerClassName: PropTypes.string,
/**
* Override the inline-styles of the content's container.
*/
contentContainerStyle: PropTypes.object,
/**
* Specify initial visible tab index.
* If `initialSelectedIndex` is set but larger than the total amount of specified tabs,
* `initialSelectedIndex` will revert back to default.
* If `initialSelectedIndex` is set to any negative value, no tab will be selected intially.
*/
initialSelectedIndex: PropTypes.number,
/**
* Override the inline-styles of the InkBar.
*/
inkBarStyle: PropTypes.object,
/**
* Override the inline-styles of the InkBar container.
*/
inkBarContainerStyle: PropTypes.object,
/**
* Called when the selected value change.
*/
onChange: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* Override the inline-styles of the tab-labels container.
*/
tabItemContainerStyle: PropTypes.object,
/**
* Override the default tab template used to wrap the content of each tab element.
*/
tabTemplate: PropTypes.func,
/**
* Override the inline-styles of the tab template.
*/
tabTemplateStyle: PropTypes.object,
/**
* Makes Tabs controllable and selects the tab whose value prop matches this prop.
*/
value: PropTypes.any,
};
static defaultProps = {
initialSelectedIndex: 0,
onChange: () => {},
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {selectedIndex: 0};
componentWillMount() {
const valueLink = this.getValueLink(this.props);
const initialIndex = this.props.initialSelectedIndex;
this.setState({
selectedIndex: valueLink.value !== undefined ?
this.getSelectedIndex(this.props) :
initialIndex < this.getTabCount() ?
initialIndex :
0,
});
}
componentWillReceiveProps(newProps, nextContext) {
const valueLink = this.getValueLink(newProps);
const newState = {
muiTheme: nextContext.muiTheme || this.context.muiTheme,
};
if (valueLink.value !== undefined) {
newState.selectedIndex = this.getSelectedIndex(newProps);
}
this.setState(newState);
}
getTabs(props = this.props) {
const tabs = [];
Children.forEach(props.children, (tab) => {
if (isValidElement(tab)) {
tabs.push(tab);
}
});
return tabs;
}
getTabCount() {
return this.getTabs().length;
}
// Do not use outside of this component, it will be removed once valueLink is deprecated
getValueLink(props) {
return props.valueLink || {
value: props.value,
requestChange: props.onChange,
};
}
getSelectedIndex(props) {
const valueLink = this.getValueLink(props);
let selectedIndex = -1;
this.getTabs(props).forEach((tab, index) => {
if (valueLink.value === tab.props.value) {
selectedIndex = index;
}
});
return selectedIndex;
}
handleTabClick = (value, event, tab) => {
const valueLink = this.getValueLink(this.props);
const index = tab.props.index;
if ((valueLink.value && valueLink.value !== value) ||
this.state.selectedIndex !== index) {
valueLink.requestChange(value, event, tab);
}
this.setState({selectedIndex: index});
if (tab.props.onActive) {
tab.props.onActive(tab);
}
};
getSelected(tab, index) {
const valueLink = this.getValueLink(this.props);
return valueLink.value ? valueLink.value === tab.props.value :
this.state.selectedIndex === index;
}
render() {
const {
contentContainerClassName,
contentContainerStyle,
initialSelectedIndex, // eslint-disable-line no-unused-vars
inkBarStyle,
inkBarContainerStyle,
onChange, // eslint-disable-line no-unused-vars
style,
tabItemContainerStyle,
tabTemplate,
tabTemplateStyle,
...other
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
const valueLink = this.getValueLink(this.props);
const tabValue = valueLink.value;
const tabContent = [];
const width = 100 / this.getTabCount();
const tabs = this.getTabs().map((tab, index) => {
warning(tab.type && tab.type.muiName === 'Tab',
`Material-UI: Tabs only accepts Tab Components as children.
Found ${tab.type.muiName || tab.type} as child number ${index + 1} of Tabs`);
warning(!tabValue || tab.props.value !== undefined,
`Material-UI: Tabs value prop has been passed, but Tab ${index}
does not have a value prop. Needs value if Tabs is going
to be a controlled component.`);
tabContent.push(tab.props.children ?
createElement(tabTemplate || TabTemplate, {
key: index,
selected: this.getSelected(tab, index),
style: tabTemplateStyle,
}, tab.props.children) : undefined);
return cloneElement(tab, {
key: index,
index: index,
selected: this.getSelected(tab, index),
width: `${width}%`,
onClick: this.handleTabClick,
});
});
const realSelectedIndex = valueLink.value ? this.getSelectedIndex(this.props) : this.state.selectedIndex;
const inkBar = realSelectedIndex !== -1 ? (
<InkBar
left={`${width * realSelectedIndex}%`}
width={`${width}%`}
style={inkBarStyle}
/>
) : null;
const inkBarContainerWidth = tabItemContainerStyle ?
tabItemContainerStyle.width : '100%';
return (
<div style={prepareStyles(Object.assign({}, style))} {...other}>
<div style={prepareStyles(Object.assign(styles.tabItemContainer, tabItemContainerStyle))}>
{tabs}
</div>
<div style={prepareStyles(Object.assign({width: inkBarContainerWidth}, inkBarContainerStyle))}>
{inkBar}
</div>
<div
style={prepareStyles(Object.assign({}, contentContainerStyle))}
className={contentContainerClassName}
>
{tabContent}
</div>
</div>
);
}
}
export default Tabs;