-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
241 lines (224 loc) · 5.27 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Copyright 2016 Reza (github.com/rghorbani)
*
* @flow
*/
'use strict';
const React = require('react');
const PropTypes = require('prop-types');
const { Platform, StyleSheet } = require('react-native');
const {
BaseComponent,
Constants,
Colors,
Shadows,
Text,
View,
} = require('react-native-ui-lib');
const ItemWrapper = require('./ItemWrapper');
class Header extends BaseComponent {
static displayName = 'Header';
static propTypes = {
/**
* header height
*/
height: PropTypes.number,
/**
* header background color
*/
backgroundColor: PropTypes.string,
/**
* title of header
*/
title: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
/**
* color of title
*/
titleColor: PropTypes.string,
/**
* style of title
*/
titleStyle: Text.propTypes.style,
/**
* leftItems
*/
leftItems: PropTypes.oneOfType([
PropTypes.shape(ItemWrapper.propTypes),
PropTypes.arrayOf(PropTypes.shape(ItemWrapper.propTypes)),
]),
/**
* rightItems
*/
rightItems: PropTypes.oneOfType([
PropTypes.shape(ItemWrapper.propTypes),
PropTypes.arrayOf(PropTypes.shape(ItemWrapper.propTypes)),
]),
/**
* extraItems
*/
extraItems: PropTypes.oneOfType([
PropTypes.shape(ItemWrapper.propTypes),
PropTypes.arrayOf(PropTypes.shape(ItemWrapper.propTypes)),
]),
/**
* color of items
*/
itemsColor: PropTypes.string,
/**
* style the action bar
*/
style: PropTypes.oneOfType([
PropTypes.object,
PropTypes.number,
PropTypes.array,
]),
/**
* use safe area
*/
useSafeArea: PropTypes.bool,
/**
* Use to identify the button in tests
*/
testID: PropTypes.string,
};
static defaultProps = {
useSafeArea: true,
backgroundColor: Colors.white,
titleColor: Colors.yellow,
itemsColor: Colors.black,
};
constructor(props) {
super(props);
}
generateStyles() {
let statusBarHeight = Constants.statusBarHeight;
if (
Platform.OS === 'android' &&
Platform.Version &&
Platform.Version < 21
) {
statusBarHeight = 0;
}
let height = Platform.OS === 'ios' ? 45 : 50;
if (this.props.height) {
height = this.props.height;
}
height += statusBarHeight;
this.styles = createStyles({
height: height,
statusBarHeight,
backgroundColor: this.props.backgroundColor,
leftItems: this.props.leftItems,
rightItems: this.props.rightItems,
});
}
render() {
let {
titleColor,
titleStyle,
leftItems,
rightItems,
itemsColor,
useSafeArea,
style,
} = this.props;
if (!Array.isArray(leftItems)) {
leftItems = [leftItems];
}
if (!Array.isArray(rightItems)) {
rightItems = [rightItems];
}
const content =
React.Children.count(this.props.children) === 0 ? (
<Text
text70
numberOfLines={1}
style={[this.styles.title, { color: titleColor }, titleStyle]}
>
{this.props.title}
</Text>
) : (
this.props.children
);
let wrapper = (
<View style={[this.styles.container, style]}>
<View style={this.styles.leftItems}>
{leftItems.map((leftItem, index) => {
return (
<ItemWrapper key={index} color={itemsColor} item={leftItem} />
);
})}
</View>
<View
accessible={true}
accessibilityLabel={this.props.title}
accessibilityTraits="header"
style={this.styles.centerItems}
>
{content}
</View>
<View style={this.styles.rightItems}>
{rightItems.map((rightItem, index) => {
return (
<ItemWrapper key={index} color={itemsColor} item={rightItem} />
);
})}
</View>
</View>
);
if (Constants.isIOS && useSafeArea) {
wrapper = <View useSafeArea={useSafeArea}>{wrapper}</View>;
}
return wrapper;
}
}
function createStyles({
height,
statusBarHeight,
backgroundColor,
leftItems,
rightItems,
}) {
const leftMore = Array.isArray(leftItems) && leftItems.length > 2;
const rightMore = Array.isArray(rightItems) && rightItems.length > 2;
return StyleSheet.create({
toolbarContainer: {
paddingTop: statusBarHeight,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: 'rgba(0, 0, 0, .3)',
},
toolbar: {
height: height - statusBarHeight,
},
container: {
height,
paddingTop: statusBarHeight,
paddingHorizontal: 10,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
backgroundColor,
elevation: 4,
...Shadows.white10.bottom,
},
leftItems: {
flex: leftMore ? 0 : 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
},
centerItems: {
flex: 2,
alignItems: 'center',
justifyContent: 'center',
},
rightItems: {
flex: rightMore ? 0 : 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-end',
},
title: {},
});
}
module.exports = Header;