forked from Mindinventory/React-Native-top-navbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.tsx
198 lines (169 loc) · 6.3 KB
/
Header.tsx
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
import React, { Component } from 'react'
import { StyleSheet, View, Platform, StatusBar, TextStyle, ViewStyle, ImageStyle } from 'react-native'
import { getStatusBarHeight } from 'react-native-status-bar-height';
var updatedChildren : Array<JSX.Element> | JSX.Element | undefined
interface headerProps {
style: object;
statusBarBackground: string;
children: Array<JSX.Element> | JSX.Element | undefined;
barStyle: string;
}
export type leftProps = {
style : object;
children: JSX.Element
}
export type bodyProps = {
style : object;
children: JSX.Element
}
export type rightProps = {
style : object;
children: JSX.Element
}
const Left: React.FC<leftProps> = (props) => {
const { children, style } = props
let leftStyle = [styles.sideBar, style]
return (
<View style={leftStyle}>
{children}
</View>
)
}
const Right: React.FC<rightProps> = (props) => {
const { children, style } = props
let rightStyle = [styles.sideBar, style]
return (
<View style={rightStyle}>
{children}
</View>
)
}
const Body: React.FC<bodyProps> = (props) => {
const { children, style } = props
let bodyStyle = [styles.body, style]
return (
<View style={bodyStyle}>
{children}
</View>
)
}
class Header extends Component <headerProps>{
static Left = Left
static Right = Right
static Body = Body
spaceManager = (spaceData : Array<JSX.Element> ) => {
let tempArr : Array<JSX.Element> = []
spaceData.filter((item) => {
const { props, type } = item
if (type.name === 'Right' || type.name === 'Left') {
if (props.style && props.style.width) {
tempArr = [...tempArr,item]
}
}
})
if (tempArr.length > 1) {
const maxPeak = tempArr.reduce((p, c) => p.props.style.width > c.props.style.width ? p : c);
let converInNumber = maxPeak.props.style.width.substring(0, maxPeak.props.style.width.length - 1)
let maxSize = Number(converInNumber)
let multiplySize = Number(maxSize) * 2
let availableSpace = 100 - multiplySize
spaceData.map((val) => {
const { props, type } = val
if (type.name === 'Right' || type.name === 'Left') {
val.props.style.width = maxSize <= 15 ? '15%' : maxSize <= 20 ? maxSize.toString().concat('%') : '20%'
}else{
val.props.style.width = maxSize <= 15 ? '70%' : maxSize <= 20 ? availableSpace.toString().concat('%') : '60%'
}
updatedChildren = spaceData
})
}else {
updatedChildren = spaceData
}
// updatedChildren = spaceData
}
headerManager = (children : Array<JSX.Element> | JSX.Element | undefined) => {
let newArr : Array<string> = []
if (children !== undefined && Array.isArray(children)) {
children.map((val) => {
newArr.push(val.type.name)
})
if (newArr.includes('Body') && newArr.includes('Right')) {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'flex-end' }
updatedChildren =children
} else if (newArr.includes('Left') && newArr.includes('Right')) {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'space-between' }
updatedChildren =children
}
else if (newArr.includes('Left') && newArr.includes('Body')) {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'flex-start' }
updatedChildren =children
}
this.spaceManager(children)
}
else {
if (children !== undefined && children.type.name == 'Body') {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'center' }
styles.body = { ...styles.body}
updatedChildren =children
} else if (children !== undefined && children.type.name == 'Right') {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'flex-end' }
updatedChildren =children
}
else if (children !== undefined && children.type.name == 'Left') {
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'flex-start' }
updatedChildren =children
}
else{
styles.mainContainer = { ...styles.mainContainer, justifyContent: 'center' }
updatedChildren =children
}
}
}
render() {
const { children, style, statusBarBackground } = this.props
this.headerManager(children)
let headerStyle = [styles.mainContainer, style]
return (
<>
<View style={[styles.statusBar, { backgroundColor: statusBarBackground }]} >
<StatusBar translucent backgroundColor={statusBarBackground} {...this.props} />
</View>
<View {...this.props} style={headerStyle}>
{updatedChildren}
</View>
</>
)
}
}
export default Header
type Style = {
mainContainer: ViewStyle;
statusBar: ViewStyle;
sideBar: ViewStyle;
body: ViewStyle
};
const styles = StyleSheet.create<Style>({
mainContainer: {
height: Platform.OS === 'ios' ? 44 : 48,
backgroundColor: '#009999',
alignItems: 'center',
flexDirection: 'row'
},
statusBar: {
height: getStatusBarHeight()
},
sideBar: {
width: '15%',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
padding: 5
},
body: {
width: '70%',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
alignSelf: 'center'
}
})