forked from ptomasroos/react-native-tab-navigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabNavigator.js
185 lines (165 loc) · 4.5 KB
/
TabNavigator.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
'use strict';
import { Set } from 'immutable';
import React, {
PropTypes,
} from 'react';
import {
StyleSheet,
View,
} from 'react-native';
import Badge from './Badge';
import Layout from './Layout';
import StaticContainer from './StaticContainer';
import Tab from './Tab';
import TabBar from './TabBar';
import TabNavigatorItem from './TabNavigatorItem';
export default class TabNavigator extends React.Component {
static propTypes = {
...View.propTypes,
sceneStyle: View.propTypes.style,
tabBarStyle: TabBar.propTypes.style,
tabBarShadowStyle: TabBar.propTypes.shadowStyle,
hidesTabTouch: PropTypes.bool
};
constructor(props, context) {
super(props, context);
this.state = {
renderedSceneKeys: this._updateRenderedSceneKeys(props.children),
};
this._renderTab = this._renderTab.bind(this);
}
componentWillReceiveProps(nextProps) {
let { renderedSceneKeys } = this.state;
this.setState({
renderedSceneKeys: this._updateRenderedSceneKeys(
nextProps.children,
renderedSceneKeys,
),
});
}
_getSceneKey(item, index): string {
return `scene-${(item.key !== null) ? item.key : index}`;
}
_updateRenderedSceneKeys(children, oldSceneKeys = Set()): Set {
let newSceneKeys = Set().asMutable();
React.Children.forEach(children, (item, index) => {
let key = this._getSceneKey(item, index);
if (oldSceneKeys.has(key) || item.props.selected) {
newSceneKeys.add(key);
}
});
return newSceneKeys.asImmutable();
}
render() {
let { style, children, tabBarStyle, tabBarShadowStyle, sceneStyle, ...props } = this.props;
let scenes = [];
React.Children.forEach(children, (item, index) => {
let sceneKey = this._getSceneKey(item, index);
if (!this.state.renderedSceneKeys.has(sceneKey)) {
return;
}
let { selected } = item.props;
let scene =
<SceneContainer key={sceneKey} selected={selected} style={sceneStyle}>
{item}
</SceneContainer>;
scenes.push(scene);
});
return (
<View {...props} style={[styles.container, style]}>
{scenes}
<TabBar style={tabBarStyle} shadowStyle={tabBarShadowStyle}>
{React.Children.map(children, this._renderTab)}
</TabBar>
</View>
);
}
_renderTab(item) {
let icon;
if (item.props.selected) {
if (item.props.renderSelectedIcon) {
icon = item.props.renderSelectedIcon();
} else if (item.props.renderIcon) {
let defaultIcon = item.props.renderIcon();
icon = React.cloneElement(defaultIcon, {
style: [defaultIcon.props.style, styles.defaultSelectedIcon],
});
}
} else if (item.props.renderIcon) {
icon = item.props.renderIcon();
}
let badge;
if (item.props.renderBadge) {
badge = item.props.renderBadge();
} else if (item.props.badgeText) {
badge = <Badge>{item.props.badgeText}</Badge>;
}
return (
<Tab
testID={item.props.testID}
title={item.props.title}
allowFontScaling={item.props.allowFontScaling}
titleStyle={[
item.props.titleStyle,
item.props.selected ? [
styles.defaultSelectedTitle,
item.props.selectedTitleStyle,
] : null,
]}
badge={badge}
onPress={item.props.onPress}
hidesTabTouch={this.props.hidesTabTouch}
style={item.props.tabStyle}>
{icon}
</Tab>
);
}
}
class SceneContainer extends React.Component {
static propTypes = {
...View.propTypes,
selected: PropTypes.bool,
};
render() {
let { selected, ...props } = this.props;
return (
<View
{...props}
pointerEvents={selected ? 'auto' : 'none'}
removeClippedSubviews={!selected}
style={[
styles.sceneContainer,
selected ? null : styles.hiddenSceneContainer,
props.style,
]}>
<StaticContainer shouldUpdate={selected}>
{this.props.children}
</StaticContainer>
</View>
);
}
}
let styles = StyleSheet.create({
container: {
flex: 1,
},
sceneContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
paddingBottom: Layout.tabBarHeight,
},
hiddenSceneContainer: {
overflow: 'hidden',
opacity: 0,
},
defaultSelectedTitle: {
color: 'rgb(255, 0, 0)',
},
defaultSelectedIcon: {
tintColor: 'rgb(0, 122, 255)',
},
});
TabNavigator.Item = TabNavigatorItem;