forked from reduxjs/redux-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tabs.js
95 lines (84 loc) · 2.21 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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import TabsHeader from './TabsHeader';
import { TabsContainer } from './styles/common';
export default class Tabs extends Component {
constructor(props) {
super(props);
this.updateTabs(props);
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.selected !== this.props.selected) {
this.updateTabs(nextProps);
}
}
onMouseUp = e => {
e.target.blur();
};
onClick = e => {
this.props.onClick(e.target.value);
};
updateTabs(props) {
const tabs = props.tabs;
const selected = props.selected;
this.tabsHeader = tabs.map((tab, i) => {
let isSelected;
const value = typeof tab.value !== 'undefined' ? tab.value : tab.name;
if (value === selected) {
isSelected = true;
if (tab.component) {
this.SelectedComponent = tab.component;
if (tab.selector) this.selector = () => tab.selector(tab);
}
}
return (
<button
key={i}
value={value}
data-selected={isSelected}
onMouseUp={this.onMouseUp}
onClick={this.onClick}
>
{tab.name}
</button>
);
});
}
render() {
const tabsHeader = (
<TabsHeader
tabs={this.tabsHeader}
items={this.props.tabs}
main={this.props.main}
collapsible={this.props.collapsible}
onClick={this.props.onClick}
selected={this.props.selected}
position={this.props.position}
/>
);
if (!this.SelectedComponent) {
return (
<TabsContainer position={this.props.position}>
{tabsHeader}
</TabsContainer>
);
}
return (
<TabsContainer position={this.props.position}>
{tabsHeader}
<div>
<this.SelectedComponent {...this.selector && this.selector()} />
</div>
</TabsContainer>
);
}
}
Tabs.propTypes = {
tabs: PropTypes.array.isRequired,
selected: PropTypes.string,
main: PropTypes.bool,
onClick: PropTypes.func.isRequired,
collapsible: PropTypes.bool,
position: PropTypes.oneOf(['left', 'right', 'center'])
};
Tabs.defaultProps = { position: 'left' };