-
Notifications
You must be signed in to change notification settings - Fork 317
/
index.js
162 lines (142 loc) · 3.99 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
import React, { Component } from 'react';
import {
StatusBar,
Text,
View,
Platform,
} from 'react-native';
import PropTypes from 'prop-types';
import ViewPropTypes from './lib';
import NavbarButton from './NavbarButton';
import styles from './styles';
const ButtonShape = {
title: PropTypes.string.isRequired,
style: ViewPropTypes.style,
handler: PropTypes.func,
disabled: PropTypes.bool,
};
const TitleShape = {
title: PropTypes.string.isRequired,
tintColor: PropTypes.string,
ellipsizeMode: PropTypes.string,
numberOfLines: PropTypes.number
};
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 getButtonElement(data, style) {
return (
<View style={styles.navBarButtonContainer}>
{(!data || data.props) ? data : (
<NavbarButton
title={data.title}
style={[data.style, style]}
tintColor={data.tintColor}
handler={data.handler}
accessible={data.accessible}
accessibilityLabel={data.accessibilityLabel}
/>
)}
</View>
);
}
function getTitleElement(data) {
if (!data || data.props) {
return <View style={styles.customTitle}>{data}</View>;
}
const colorStyle = data.tintColor ? { color: data.tintColor } : null;
return (
<View style={styles.navBarTitleContainer}>
<Text ellipsizeMode={data.ellipsizeMode} numberOfLines={data.numberOfLines} style={[styles.navBarTitleText, data.style, colorStyle]}>
{data.title}
</Text>
</View>
);
}
export default class NavigationBar extends Component {
static propTypes = {
style: ViewPropTypes.style,
tintColor: PropTypes.string,
statusBar: PropTypes.shape(StatusBarShape),
leftButton: PropTypes.oneOfType([
PropTypes.shape(ButtonShape),
PropTypes.element,
PropTypes.oneOf([null]),
]),
rightButton: PropTypes.oneOfType([
PropTypes.shape(ButtonShape),
PropTypes.element,
PropTypes.oneOf([null]),
]),
title: PropTypes.oneOfType([
PropTypes.shape(TitleShape),
PropTypes.element,
PropTypes.oneOf([null]),
]),
containerStyle: ViewPropTypes.style,
};
static defaultProps = {
style: {},
tintColor: '',
leftButton: null,
rightButton: null,
title: null,
statusBar: {
style: 'default',
hidden: false,
hideAnimation: 'slide',
showAnimation: 'slide',
},
containerStyle: {},
};
componentDidMount() {
this.customizeStatusBar();
}
componentWillReceiveProps() {
this.customizeStatusBar();
}
customizeStatusBar() {
const { statusBar } = this.props;
if (Platform.OS === 'ios') {
if (statusBar.style) {
StatusBar.setBarStyle(statusBar.style);
}
const animation = statusBar.hidden ?
statusBar.hideAnimation : statusBar.showAnimation;
StatusBar.showHideTransition = animation;
StatusBar.hidden = statusBar.hidden;
}
}
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;
if (Platform.OS === 'ios') {
statusBar = !this.props.statusBar.hidden ?
<View style={[styles.statusBar, customStatusBarTintColor]} /> : null;
}
return (
<View style={[styles.navBarContainer, containerStyle, customTintColor]}>
{statusBar}
<View style={[styles.navBar, style]}>
{getTitleElement(title)}
{getButtonElement(leftButton, { marginLeft: 8 })}
{getButtonElement(rightButton, { marginRight: 8 })}
</View>
</View>
);
}
}