-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabs.js
More file actions
130 lines (111 loc) · 3.37 KB
/
Tabs.js
File metadata and controls
130 lines (111 loc) · 3.37 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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {debounce, noop} from './helpers';
import TabsHorz from './TabsHorz';
import TabsVert from './TabsVert';
import './tabs.css';
/**
* Tabbed UI component
* Individual tabs are created by wrapping content in <TabPanel>
* Tab navigation is dynamically generated based on a label prop passed into each <TabPanel>
* The first tab is selected by default, but can be overriden by adding a selected prop
* with the index of the desired tab
* @class Tabs
* @returns {JSX}
*/
class Tabs extends Component {
constructor(props) {
super(props);
this.checkLayoutHandler = this
.checkLayout
.bind(this);
this.state = {
selected: this.props.defaultSelected,
isVert: false
};
}
componentDidMount() {
if (this.props.isResponsive) {
this.checkLayout();
window.addEventListener('resize', debounce(this.checkLayoutHandler, 100));
}
}
componentDidUpdate(prevProps, prevState) {
if (prevState.selected !== this.state.selected) {
this.props.onTabChange(this.state.selected);
}
}
checkLayout() {
if (window.matchMedia(`(min-width: ${this.props.responsiveWidth}px)`).matches) {
/* the viewport is at least 500 pixels wide */
if (this.state.isVert) {
this.updateLayout(false);
} else {
return false;
}
} else {
/* the viewport is less than 500 pixels wide */
if (!this.state.isVert) {
this.updateLayout(true);
} else {
return true;
}
}
}
updateLayout(isVert) {
return this.setState({isVert: isVert});
}
/**
* Click handler for tabs navigation
* @method handleNavClick
* @param {Number} index clicked tab's index value
* @returns {Function} sets selected state
*/
handleNavClick = index => {
return event => {
event.preventDefault();
if (this.state.isVert) {
this.setState(prevState => {
return {selected: prevState.selected === index ? -1 : index}
});
} else {
this.setState(prevState => {
return {selected: index}
});
}
};
}
render() {
if (this.state.isVert) {
return <div className="tabs tabs--isVert">
<TabsVert
onClick={this.handleNavClick}
selected={this.state.selected}
tabs={this.props.children}
showIconsVert={this.props.showIconsVert}
/>
</div>
} else {
return <div className="tabs">
<TabsHorz
onClick={this.handleNavClick}
selected={this.state.selected}
tabs={this.props.children}
showIconsHorz={this.props.showIconsHorz}
/>
</div>
}
}
}
Tabs.propTypes = {
children: PropTypes.node.isRequired,
defaultSelected: PropTypes.number,
onTabChange: PropTypes.func,
responsiveBreakpoint: PropTypes.string
};
Tabs.defaultProps = {
defaultSelected: 0,
onTabChange: noop,
responsiveBreakpoint: '500'
};
export default Tabs;