Skip to content
This repository was archived by the owner on May 8, 2019. It is now read-only.

Commit 454e12f

Browse files
vnovickczystyl
authored andcommitted
Tab navigator (#9)
* Prepare example for tab navigator * adding basic implementation of tab navigator with screen props, but without navigation inside tab yet * adding screen props * Reformat code * Update exmple * Comment onPress option * experiments with navigation + nesting tab navigator
1 parent 068a0b6 commit 454e12f

File tree

12 files changed

+368
-137
lines changed

12 files changed

+368
-137
lines changed

example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"react-navigation": "^3.0.5",
2323
"react-navigation-stack": "~1.0.5",
2424
"@react-navigation/core": "^3.0.1",
25-
"@react-navigation/native": "^3.0.3"
25+
"@react-navigation/native": "^3.0.3",
26+
"react-navigation-tabs": "1.0.1"
2627
},
2728
"private": true
2829
}

example/src/App.re

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,6 @@
1-
open BsReactNavigation;
2-
3-
/**
4-
* Route and Navigation types are kept in a separate module to
5-
* handle circular references between modules
6-
*/
7-
open Config;
8-
9-
module Stack =
10-
StackNavigator.Create({
11-
open StackNavigator;
12-
13-
/**
14-
* StackNavigator requires `route` type to be defined.
15-
*/
16-
type route = Config.route;
17-
18-
/**
19-
* Initial route to start with. Has to be one of `route` variants.
20-
*/
21-
let initialRoute = Home;
22-
23-
/**
24-
* Returns a screen for a given route and its options.
25-
*
26-
* To declare options, you call `StackNavigator.screenOptions` and provide
27-
* same keys as `navigationOptions` of React Navigation.
28-
*
29-
* This is to allow optional fields to be provided.
30-
*/
31-
let getScreen = (route, navigation) =>
32-
switch (route) {
33-
| Home => (<Screen navigation />, screenOptions(~title="Home", ()))
34-
| UserDetails(userId) => (
35-
<Screen navigation text={"Browsing profile of: " ++ userId} />,
36-
screenOptions(~title="Hello " ++ userId, ()),
37-
)
38-
};
39-
});
40-
41-
let app = Stack.render;
1+
let stack = Stack.render;
2+
let tab = TabExample.render;
3+
let drawer = DrawerExample.render;
424

435
module Switch =
446
SwitchNavigator.Create({
@@ -56,5 +18,4 @@ module Switch =
5618

5719
let switchNavigator = Switch.render;
5820

59-
let drawer = DrawerExample.render;
60-
let app = drawer;
21+
let app = drawer;

example/src/Config.re

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ open BsReactNavigation;
22

33
type route =
44
| Home
5-
| UserDetails(string);
5+
| UserDetails(string)
6+
| TabExample;
7+
8+
9+
type navigationProp = StackNavigator.navigation(route);
10+
11+
type tabs =
12+
| Info
13+
| Profile
14+
| Settings;
15+
16+
type tabNavigationProp = TabNavigator.navigation;
617

718
type navigation = StackNavigator.navigation(route);
819

@@ -16,4 +27,4 @@ type item =
1627
| Dashbord
1728
| Settings;
1829

19-
type drawerNavigationProp = DrawerNavigation.navigation(route);
30+
type drawerNavigationProp = DrawerNavigation.navigation(route);

example/src/Screen.re

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module Styles = {
1313

1414
let component = ReasonReact.statelessComponent("App");
1515

16-
let make = (~navigation: Config.navigation, ~text: string="Hi!", _children) => {
16+
let make =
17+
(~navigation: Config.navigationProp, ~text: string="Hi!", _children) => {
1718
...component,
1819
render: _self =>
1920
<SafeAreaView>
@@ -23,6 +24,10 @@ let make = (~navigation: Config.navigation, ~text: string="Hi!", _children) => {
2324
title="Go to details screen "
2425
onPress={() => navigation.push(UserDetails("Mike Grabowski"))}
2526
/>
27+
<Button
28+
title="Go to tab example"
29+
onPress={() => navigation.push(TabExample)}
30+
/>
2631
</View>
2732
</SafeAreaView>,
2833
};

example/src/Stack.re

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
open BsReactNavigation;
2+
3+
/**
4+
* Route and Navigation types are kept in a separate module to
5+
* handle circular references between modules
6+
*/
7+
open Config;
8+
9+
module Stack =
10+
11+
StackNavigator.Create({
12+
open StackNavigator;
13+
14+
/**
15+
* StackNavigator requires `route` type to be defined.
16+
*/
17+
type route = Config.route;
18+
19+
/**
20+
* Initial route to start with. Has to be one of `route` variants.
21+
*/
22+
let initialRoute = Home;
23+
24+
25+
/**
26+
* Returns a screen for a given route and its options.
27+
*
28+
* To declare options, you call `StackNavigator.screenOptions` and provide
29+
* same keys as `navigationOptions` of React Navigation.
30+
*
31+
* This is to allow optional fields to be provided.
32+
*/
33+
let getScreen = (route, navigation) =>
34+
switch (route) {
35+
| Home => (<Screen navigation />, screenOptions(~title="Home", ()))
36+
| UserDetails(userId) => (
37+
<Screen navigation text={"Browsing profile of: " ++ userId} />,
38+
screenOptions(~title="Hello " ++ userId, ()),
39+
)
40+
| TabExample => (<TabExample navigation/>, screenOptions())
41+
};
42+
});
43+
44+
let render = Stack.render;

example/src/TabExample.re

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
open BsReactNavigation;
2+
open Config;
3+
4+
module Tabs =
5+
TabNavigator.Create({
6+
type tabs = Config.tabs;
7+
type order = list(tabs);
8+
9+
let tabBarOptions =
10+
TabNavigator.tabBarOptions(~activeTintColor="#847", ());
11+
12+
let order = [Info, Profile, Settings];
13+
14+
let getTab = tab => {
15+
switch (tab) {
16+
| Info => (
17+
"Info",
18+
((navigation) => <Tabs.Info navigation/>),
19+
TabNavigator.screenOptions(~title="Info", ()),
20+
)
21+
| Profile => (
22+
"Profile",
23+
((navigation) => <Tabs.Profile navigation/>),
24+
TabNavigator.screenOptions(~title="Profile", ()),
25+
)
26+
| Settings => (
27+
"Settings",
28+
((navigation) => <Tabs.Settings navigation/>),
29+
TabNavigator.screenOptions(~title="Settings", ()),
30+
)
31+
};
32+
};
33+
});
34+
35+
let render = Tabs.render;
36+
37+
let make = (~navigation, _children) => {
38+
...(ReasonReact.statelessComponent("TabExample")),
39+
render: _ => Tabs.render
40+
}

example/src/Tabs.re

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
open BsReactNative;
2+
open Config;
3+
let str = ReasonReact.string;
4+
5+
module Styles = {
6+
open Style;
7+
let container =
8+
style([flex(1.), alignItems(Center), justifyContent(Center)]);
9+
let profileContainer =
10+
style([backgroundColor(String("rgb(76, 217, 100)"))]);
11+
let infoContainer = style([backgroundColor(String("rgb(90, 200, 250)"))]);
12+
let settingsContainer =
13+
style([backgroundColor(String("rgb(255, 149, 0)"))]);
14+
15+
let titile = style([fontSize(Float(30.))]);
16+
};
17+
18+
module Profile = {
19+
let component = ReasonReact.statelessComponent("Profile");
20+
21+
let make = (~navigation: tabNavigationProp, _children) => {
22+
...component,
23+
render: _self =>
24+
<SafeAreaView
25+
style={StyleSheet.flatten([
26+
Styles.container,
27+
Styles.profileContainer,
28+
])}>
29+
<View> <Text style=Styles.titile> {str("Profile")} </Text> </View>
30+
</SafeAreaView>,
31+
};
32+
};
33+
34+
module Info = {
35+
let component = ReasonReact.statelessComponent("Info");
36+
37+
let make = (~navigation: tabNavigationProp, _children) => {
38+
...component,
39+
render: _self =>
40+
<SafeAreaView
41+
style={StyleSheet.flatten([Styles.container, Styles.infoContainer])}>
42+
<View> <Text style=Styles.titile> {str("Info")} </Text> </View>
43+
</SafeAreaView>,
44+
};
45+
};
46+
47+
module Settings = {
48+
let component = ReasonReact.statelessComponent("Settings");
49+
50+
let make = (~navigation: tabNavigationProp, _children) => {
51+
...component,
52+
render: _self =>
53+
<SafeAreaView
54+
style={StyleSheet.flatten([
55+
Styles.container,
56+
Styles.settingsContainer,
57+
])}>
58+
<View> <Text style=Styles.titile> {str("Settings")} </Text> </View>
59+
<Button onPress={() => navigation.navigate("Profile")} title="info"/>
60+
</SafeAreaView>,
61+
};
62+
};

0 commit comments

Comments
 (0)