Skip to content

Commit fcb037f

Browse files
author
alvaromb
committed
Added new route mapper options
1 parent 6a9e330 commit fcb037f

7 files changed

Lines changed: 91 additions & 36 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ inside it:
5858

5959
```js
6060
import React from 'react-native'
61-
import TopNavigatorWrapper from 'react-native-navigator-wrapper'
61+
import { TopNavigatorWrapper } from 'react-native-navigator-wrapper'
6262
import MyComponent from './MyComponent'
6363

6464
class MyApp extends React.Component {
@@ -83,15 +83,14 @@ If you just want to use the navigation bar inside a navigator, use the
8383

8484
```js
8585
import React from 'react-native'
86-
import NavigationWrapper from 'react-native-navigator-wrapper'
86+
import { NavigationWrapper } from 'react-native-navigator-wrapper'
8787

8888
class MyComponent extends React.Component {
8989
render () {
9090
return (
9191
<NavigationWrapper
9292
initialComponent={Component}
9393
title='Title'
94-
passProps={}
9594
/>
9695
)
9796
}
@@ -102,5 +101,16 @@ Every time you push a component that's inside the ``NavigationWrapper`` componen
102101
you will have a **navigator** prop, just like the top navigation option before,
103102
that will let you to keep pushing components in the stack.
104103

104+
### Custom ``routeMapper``
105+
The React Native ``Navigator.NavigatorBar`` component has an object called
106+
``routeMapper`` that configures the three components that can be displayed
107+
inside the navigation bar: ``LeftButton``, ``RightButton`` and ``Title``.
108+
This library auto-generates a default route mapper object that displays an iOS
109+
style back button, a title and accepts a right element to render.
110+
111+
It also provides functions to generate each of the route mapper components so
112+
you can build a completely custom navigation bar for each ``NavigatorWrapper``.
113+
See the source code for more information.
114+
105115
## License
106116
MIT.

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import NavBarBackButton from './lib/NavBarBackButton'
22
import NavBar from './lib/NavBar'
33
import NavigatorWrapper from './lib/NavigatorWrapper'
44
import TopNavigatorWrapper from './lib/TopNavigatorWrapper'
5+
import { defaultRouteMapper, leftButtonRouteMapperGenerator, rightButtonRouteMapperGenerator, titleRouteMapperGenerator } from './lib/RouteMapper'
56

6-
export default NavBarBackButton
7-
export default NavBar
8-
export default NavigatorWrapper
9-
export default TopNavigatorWrapper
7+
export default {
8+
NavBarBackButton,
9+
NavBar,
10+
NavigatorWrapper,
11+
TopNavigatorWrapper,
12+
defaultRouteMapper,
13+
leftButtonRouteMapperGenerator,
14+
rightButtonRouteMapperGenerator,
15+
titleRouteMapperGenerator,
16+
}

lib/NavBarBackButton.ios.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class NavBarBackButton extends React.Component {
77
constructor (props) {
88
super(props)
99
this.state = {
10-
tintColor: props.style.color || 'white'
10+
tintColor: props.tintColor || 'black'
1111
}
1212
}
1313

@@ -56,7 +56,6 @@ const styles = StyleSheet.create({
5656
icon: {
5757
paddingLeft: 8,
5858
paddingTop: 2,
59-
color: 'black', // TODO: fix this color
6059
},
6160
navText: {
6261
paddingLeft: 5,

lib/NavigatorWrapper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class NavigatorWrapper extends React.Component {
2929
}}
3030
navigationBar={
3131
<NavBar
32-
routeMapper={defaultRouteMapper}
32+
routeMapper={this.props.routeMapper || defaultRouteMapper()}
3333
style={this.props.navBarStyle}
3434
/>
3535
}
@@ -44,7 +44,8 @@ NavigatorWrapper.propTypes = {
4444
title: PropTypes.string,
4545
topNavigator: PropTypes.object,
4646
passProps: PropTypes.object,
47-
navBarStyle: StyleSheetPropType(ViewStylePropTypes)
47+
navBarStyle: StyleSheetPropType(ViewStylePropTypes),
48+
routeMapper: PropTypes.object,
4849
}
4950

5051
export default NavigatorWrapper

lib/RouteMapper.ios.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,52 @@
11
import React, { Text } from 'react-native'
22
import NavBarBackButton from './NavBarBackButton'
33

4-
export const defaultRouteMapper = {
5-
LeftButton: (route, navigator, index, navState) => {
6-
if (index === 0) {
7-
return null
4+
export function leftButtonRouteMapperGenerator (BackComponent, styles, tintColor, topNavigator) {
5+
return {
6+
LeftButton: (route, navigator, index, navState) => {
7+
if (index === 0) {
8+
return null
9+
}
10+
const previousRoute = navState.routeStack[index - 1]
11+
return (
12+
<BackComponent
13+
onPress={() => navigator.pop()}
14+
style={styles}
15+
tintColor={tintColor}>
16+
{previousRoute.title}
17+
</BackComponent>
18+
)
819
}
9-
const previousRoute = navState.routeStack[index - 1]
10-
return (
11-
<NavBarBackButton onPress={() => navigator.pop()}
12-
style={[styles.navFont, styles.back]}>
13-
{previousRoute.title}
14-
</NavBarBackButton>
15-
)
16-
},
17-
RightButton: (route, navigator, index, navState) => {
18-
if (route.rightElement) {
20+
}
21+
}
22+
23+
export function rightButtonRouteMapperGenerator (RightComponent, topNavigator) {
24+
return {
25+
RightButton: (route, navigator, index, navState) => {
26+
if (RightComponent) {
27+
return <RightComponent navigator={navigator} topNavigator={topNavigator} />
28+
}
1929
return route.rightElement
2030
}
21-
},
22-
Title: (route, navigator, index, navState) => {
23-
const title = route.title || ''
24-
return (
25-
<Text style={[styles.navFont, styles.navText]} numberOfLines={1}>
26-
{title}
27-
</Text>
28-
)
2931
}
3032
}
3133

32-
const styles = React.StyleSheet.create({
34+
export function titleRouteMapperGenerator (TitleComponent, styles, topNavigator) {
35+
return {
36+
Title: (route, navigator, index, navState) => {
37+
const title = route.title || ''
38+
const Component = TitleComponent || Text
39+
return (
40+
<Component style={styles}
41+
numberOfLines={1}>
42+
{title}
43+
</Component>
44+
)
45+
}
46+
}
47+
}
48+
49+
const defaultStyles = React.StyleSheet.create({
3350
back: {
3451
flex: 1,
3552
color: 'black',
@@ -44,3 +61,11 @@ const styles = React.StyleSheet.create({
4461
width: 200,
4562
},
4663
})
64+
65+
export function defaultRouteMapper () {
66+
return {
67+
...leftButtonRouteMapperGenerator(NavBarBackButton, [defaultStyles.navFont, defaultStyles.back], 'black'),
68+
...rightButtonRouteMapperGenerator(),
69+
...titleRouteMapperGenerator(Text, [defaultStyles.navFont, defaultStyles.navText])
70+
}
71+
}

lib/TopNavigatorWrapper.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { Navigator, PropTypes } from 'react-native'
22
import NavigatorWrapper from './NavigatorWrapper'
3+
import StyleSheetPropType from 'react-native/Libraries/StyleSheet/StyleSheetPropType'
4+
import ViewStylePropTypes from 'react-native/Libraries/Components/View/ViewStylePropTypes'
35

46
class TopNavigatorWrapper extends React.Component {
57
_renderScene (route, navigator) {
@@ -11,19 +13,26 @@ class TopNavigatorWrapper extends React.Component {
1113
initialComponent={this.props.initialComponent}
1214
title={this.props.title}
1315
topNavigator={navigator}
16+
navBarStyle={this.props.navBarStyle}
17+
routeMapper={this.props.routeMapper()}
1418
/>
1519
)
1620
}
1721

1822
// Render the modal component. This component serves as a FloatFromBottom
1923
// Navigator. Can have another navigator inside. ``passProps`` is sent into
2024
// the NavigatorWrapper in order to send props to the component pushed.
25+
//
26+
// By generating the routeMapper from a function, we can pass the outer
27+
// modal navigator into the route mapper.
2128
return (
2229
<NavigatorWrapper
2330
initialComponent={route.component}
2431
title={route.title}
2532
topNavigator={navigator}
2633
passProps={route.passProps}
34+
navBarStyle={this.props.modalNavBarStyle}
35+
routeMapper={this.props.modalRouteMapper(navigator)}
2736
/>
2837
)
2938
}
@@ -42,7 +51,11 @@ class TopNavigatorWrapper extends React.Component {
4251

4352
TopNavigatorWrapper.propTypes = {
4453
initialComponent: PropTypes.func.isRequired,
45-
title: PropTypes.string
54+
title: PropTypes.string,
55+
navBarStyle: StyleSheetPropType(ViewStylePropTypes),
56+
routeMapper: PropTypes.func,
57+
modalNavBarStyle: StyleSheetPropType(ViewStylePropTypes),
58+
modalRouteMapper: PropTypes.func,
4659
}
4760

4861
export default TopNavigatorWrapper

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-navigator-wrapper",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A React Native Navigator component wrapper that implements nested navigators for both push and modal transitions.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)