This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
NavBar.js
99 lines (91 loc) · 2.71 KB
/
NavBar.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
/* @flow */
import React from 'react'
import { Platform } from 'react-native'
import { Header, HeaderTitle, HeaderBackButton } from 'react-navigation'
import type { CardSubViewProps } from './TypeDefinitions'
import * as StackUtils from './StackUtils'
type Props = CardSubViewProps
class NavBar extends React.Component<void, Props, void> {
props: Props
renderLeftComponent = (sceneProps: CardSubViewProps) => {
// Custom left component
if (sceneProps.renderLeftButton) {
return sceneProps.renderLeftButton(sceneProps)
}
// Hide back button
if (
sceneProps.scene.index === 0 ||
!sceneProps.onNavigateBack ||
sceneProps.hideBackButton
) {
return null
}
// Get previous title
const previousRoute = StackUtils.get(
sceneProps.cards,
sceneProps.scenes[Math.max(0, sceneProps.scene.index - 1)].route,
)
const previousTitle =
sceneProps.backButtonTitle || (previousRoute && previousRoute.title)
// Return default <BackButton /> component
return (
<HeaderBackButton
title={previousTitle}
tintColor={sceneProps.backButtonTintColor}
onPress={sceneProps.onNavigateBack}
/>
)
}
renderTitleComponent = (sceneProps: CardSubViewProps) => {
// Render custom title component
if (sceneProps.renderTitle) {
return sceneProps.renderTitle(sceneProps)
}
// Return <ReactNavigation.HeaderTitle /> component
return (
<HeaderTitle style={sceneProps.titleStyle}>
{sceneProps.title}
</HeaderTitle>
)
}
renderRightComponent = (sceneProps: CardSubViewProps) => {
// Render cusqtom right component
if (sceneProps.renderRightButton) {
return sceneProps.renderRightButton(sceneProps)
}
// Else return null =)
return null
}
render() {
return (
<Header
{...this.props}
mode={Platform.OS === 'ios' ? 'float' : 'screen'}
getScreenDetails={scene => {
const sceneProps = StackUtils.get(this.props.cards, scene.route)
const props = { ...this.props, scene }
return {
options: {
headerStyle:
(sceneProps && sceneProps.navBarStyle) ||
this.props.navBarStyle,
headerLeft: StackUtils.renderSubView(
this.renderLeftComponent,
props,
)(),
headerTitle: StackUtils.renderSubView(
this.renderTitleComponent,
props,
)(),
headerRight: StackUtils.renderSubView(
this.renderRightComponent,
props,
)(),
},
}
}}
/>
)
}
}
export default NavBar