forked from Kureev/react-native-navbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (123 loc) · 3.57 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
import React, { Component, PropTypes } from 'react';
import {
PixelRatio,
StatusBar,
Text,
View,
Platform,
} from 'react-native';
import NavbarButton from './NavbarButton';
import styles from './styles';
const ButtonShape = {
title: PropTypes.string.isRequired,
style: PropTypes.any,
handler: PropTypes.func,
disabled: PropTypes.bool,
};
const TitleShape = {
title: PropTypes.string.isRequired,
tintColor: PropTypes.string,
};
const StatusBarShape = {
style: PropTypes.oneOf(['light-content', 'default', ]),
hidden: PropTypes.bool,
tintColor: PropTypes.string,
hideAnimation: PropTypes.oneOf(['fade', 'slide', 'none', ]),
showAnimation: PropTypes.oneOf(['fade', 'slide', 'none', ]),
};
function customizeStatusBar(data) {
if (Platform.OS === 'ios') {
if (data.style) {
StatusBar.setBarStyle(data.style);
}
const animation = data.hidden ?
(data.hideAnimation || NavigationBar.defaultProps.statusBar.hideAnimation) :
(data.showAnimation || NavigationBar.defaultProps.statusBar.showAnimation);
StatusBar.showHideTransition = animation;
StatusBar.hidden = data.hidden;
}
}
class NavigationBar extends Component {
componentDidMount() {
customizeStatusBar(this.props.statusBar);
}
componentWillReceiveProps(props) {
customizeStatusBar(props.statusBar);
}
getButtonElement(data = {}, style) {
return (
<View style={styles.navBarButtonContainer}>
{(!!data.props) ? data : (
<NavbarButton
title={data.title}
style={[data.style, style, ]}
tintColor={data.tintColor}
handler={data.handler}/>
)}
</View>
);
}
getTitleElement(data) {
if (!!data.props) {
return <View style={styles.customTitle}>{data}</View>;
}
const colorStyle = data.tintColor ? { color: data.tintColor, } : null;
return (
<View style={styles.navBarTitleContainer}>
<Text
style={[styles.navBarTitleText, colorStyle, ]}>
{data.title}
</Text>
</View>
);
}
render() {
const { containerStyle, tintColor, title, leftButton, rightButton, style, } = this.props;
const customTintColor = tintColor ? { backgroundColor: tintColor, } : null;
const customStatusBarTintColor = this.props.statusBar.tintColor ?
{ backgroundColor: this.props.statusBar.tintColor, } : null;
let statusBar = null;
statusBar = !this.props.statusBar.hidden ?
<View style={[styles.statusBar, customStatusBarTintColor, ]} /> : null;
return (
<View style={[styles.navBarContainer, containerStyle, customTintColor, ]}>
{statusBar}
<View style={[styles.navBar, style, ]}>
{this.getTitleElement(title)}
{this.getButtonElement(leftButton, { marginLeft: 8, })}
{this.getButtonElement(rightButton, { marginRight: 8, })}
</View>
</View>
);
}
static propTypes = {
tintColor: PropTypes.string,
statusBar: PropTypes.shape(StatusBarShape),
leftButton: PropTypes.oneOfType([
PropTypes.shape(ButtonShape),
PropTypes.element,
]),
rightButton: PropTypes.oneOfType([
PropTypes.shape(ButtonShape),
PropTypes.element,
]),
title: PropTypes.oneOfType([
PropTypes.shape(TitleShape),
PropTypes.element,
]),
containerStyle: PropTypes.any,
};
static defaultProps = {
statusBar: {
style: 'default',
hidden: false,
hideAnimation: 'slide',
showAnimation: 'slide',
},
title: {
title: '',
},
containerStyle: {},
};
}
module.exports = NavigationBar;