-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (151 loc) · 4.12 KB
/
index.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
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
Dimensions,
Animated,
ViewPropTypes
} from "react-native";
import PropTypes from "prop-types";
const { width } = Dimensions.get("window");
export default class RNAnimatedTabs extends Component {
static propTypes = {
tabTitles: PropTypes.array.isRequired,
onChangeTab: PropTypes.func.isRequired,
initialActiveTabIndex: PropTypes.number,
top: PropTypes.bool,
height: PropTypes.number,
currentTab: PropTypes.number,
containerStyle: ViewPropTypes.style,
tabButtonStyle: ViewPropTypes.style,
tabTextStyle: ViewPropTypes.style,
renderTabContent: PropTypes.func, // function that returns an element
activeTabOpacity: PropTypes.number,
activeTabIndicatorHeight: PropTypes.number,
activeTabIndicatorColor: PropTypes.string
};
static defaultProps = {
initialActiveTabIndex: 0,
top: false, // Defaults active tab indicator to bottom
height: 60,
currentTab: null,
containerStyle: {},
tabButtonStyle: {},
tabTextStyle: {},
activeTabOpacity: 0.8,
activeTabIndicatorHeight: 3,
activeTabIndicatorColor: "#FE5F55",
renderTabContent: undefined
};
constructor(props) {
super(props);
this.state = {
left: new Animated.Value(0),
tabWidth: width / this.props.tabTitles.length
};
}
componentDidMount() {
// Give option to make component controlled with currentTab
const { currentTab, initialActiveTabIndex } = this.props;
this.moveTo(currentTab == null ? initialActiveTabIndex : currentTab);
}
componentWillReceiveProps(nextProps) {
const { currentTab } = nextProps;
if (currentTab && currentTab !== this.props.currentTab) {
this.moveTo(currentTab);
}
}
moveTo = index => {
if (this.props.onChangeTab) {
this.props.onChangeTab(index);
}
Animated.timing(this.state.left, {
toValue: this.state.tabWidth * index
}).start();
};
renderTabContent = (title, index) => {
const { renderTabContent, tabTextStyle } = this.props;
if (renderTabContent) return renderTabContent(title, index);
return <Text style={[styles.tabText, tabTextStyle]}>{title}</Text>;
};
renderTabs = () => {
const { tabButtonStyle, activeTabOpacity } = this.props;
return this.props.tabTitles.map((title, index) => (
<TouchableOpacity
key={`customTab${index}`}
style={[styles.tabButton, tabButtonStyle]}
onPress={() => this.moveTo(index)}
activeOpacity={activeTabOpacity}
>
{this.renderTabContent(title, index)}
</TouchableOpacity>
));
};
render() {
const {
height,
top,
activeTabIndicatorHeight,
activeTabIndicatorColor,
containerStyle
} = this.props;
const activeLineDirection = top ? { top: 0 } : { bottom: 0 }; // Stick to bottom or top
return (
<View style={[styles.tabView, { width, height }, containerStyle]}>
<View style={styles.tabs}>{this.renderTabs()}</View>
<View
style={[
styles.animatedLineContainer,
{ height: activeTabIndicatorHeight },
activeLineDirection
]}
>
<Animated.View
style={[
{
height: activeTabIndicatorHeight,
backgroundColor: activeTabIndicatorColor,
marginLeft: this.state.left,
width: this.state.tabWidth
}
]}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
tabView: {
backgroundColor: "#fff",
position: "relative",
left: 0,
right: 0
},
tabs: {
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "center"
},
tabButton: {
flex: 1,
height: 60,
alignItems: "center",
justifyContent: "center"
},
animatedLineContainer: {
position: "absolute",
left: 0, // ignore parent padding
right: 0, // ignore parent padding
flexDirection: "row"
},
tabText: {
color: "#6B6868",
fontSize: 16,
fontWeight: "500",
padding: 2
}
});