-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabs.js
120 lines (111 loc) · 2.74 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
import React from "react";
const styles = {
tab: {
flex: 1,
alignSelf: "stretch",
justifyContent: "center"
},
flex: {
flex: 1
}
};
class TabList extends React.Component {
render() {
const { ContainerComponent, FooterComponent, FooterTabComponent, LinkComponent } = this.props;
const children = React.Children.map(this.props.children, (child, index) => {
return React.cloneElement(child, {
isActive: index === this.props.activeIndex,
onPress: () => {
this.props.onActivate(index);
},
FooterTabComponent,
ContainerComponent,
LinkComponent
});
});
return (
<FooterComponent style={styles.tabs}>
{children}
</FooterComponent>
);
}
}
class Tab extends React.Component {
render() {
const {
route,
ContainerComponent,
FooterTabComponent,
LinkComponent
} = this.props;
return (
<FooterTabComponent>
<LinkComponent
to={route}
underlayColor="transparent"
style={styles.flex}
>
<ContainerComponent style={styles.flex}>
{this.props.children}
</ContainerComponent>
</LinkComponent>
</FooterTabComponent>
);
}
}
class TabPanels extends React.Component {
render() {
const { ContainerComponent } = this.props;
return (
<ContainerComponent style={styles.tabPanels}>
{this.props.children}
</ContainerComponent>
);
}
}
class TabPanel extends React.Component {
render() {
const { ContainerComponent } = this.props;
return (
<ContainerComponent>
{this.props.children}
</ContainerComponent>
);
}
}
class Tabs extends React.Component {
constructor(props) {
super(props);
this.state = {
activeIndex: !!props.initialIndex ? props.initialIndex : 0
};
}
render() {
const { ContainerComponent, FooterComponent, FooterTabComponent, LinkComponent } = this.props;
const children = React.Children.map(this.props.children, (child, index) => {
if (child.type === TabPanels) {
return React.cloneElement(child, {
activeIndex: this.state.activeIndex,
ContainerComponent
});
} else if (child.type === TabList) {
return React.cloneElement(child, {
activeIndex: this.state.activeIndex,
onActivate: activeIndex => this.setState({ activeIndex }),
ContainerComponent,
FooterComponent,
FooterTabComponent,
LinkComponent
});
} else {
return child;
}
});
return (
<ContainerComponent>
{children}
</ContainerComponent>
);
}
}
export { TabList, TabPanels, TabPanel, Tab, Tabs };