Skip to content

Commit 8acd239

Browse files
committed
Merge pull request #406 from Froelund/master
Added DrawerExample
2 parents 07655e8 + 50c381d commit 8acd239

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

Example/Example.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Error from './components/Error'
88
import Home from './components/Home'
99
import TabView from './components/TabView'
1010
import EchoView from './components/EchoView'
11+
import NavigationDrawer from './components/NavigationDrawer'
1112

1213
class TabIcon extends React.Component {
1314
render(){
@@ -47,7 +48,8 @@ export default class Example extends React.Component {
4748
render() {
4849
return <Router createReducer={reducerCreate} sceneStyle={{backgroundColor:'#F7F7F7'}}>
4950
<Scene key="modal" component={Modal} >
50-
<Scene key="root" hideNavBar={true}>
51+
<Scene key="drawer" component={NavigationDrawer}>
52+
<Scene key="root">
5153
<Scene key="echo" clone component={EchoView} />
5254
<Scene key="register" component={Register} title="Register"/>
5355
<Scene key="register2" component={Register} title="Register2" duration={1}/>
@@ -70,6 +72,7 @@ export default class Example extends React.Component {
7072
<Scene key="tab4" component={TabView} title="Tab #4" hideNavBar={true} icon={TabIcon}/>
7173
<Scene key="tab5" component={TabView} title="Tab #5" icon={TabIcon} renderRightButton={()=><Right/>}/>
7274
</Scene>
75+
</Scene>
7376
</Scene>
7477
<Scene key="error" component={Error}/>
7578
</Scene>
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React,{
2+
Component
3+
} from 'react-native'
4+
import Launch from './Launch'
5+
import Drawer from 'react-native-drawer'
6+
import {DefaultRenderer} from 'react-native-router-flux';
7+
8+
export default class Navigation extends Component {
9+
render(){
10+
console.log(this.context);
11+
const navigationState = this.props.navigationState;
12+
let selected = navigationState.children[navigationState.index];
13+
// var activeScene = (selected.name == "root" : selected[0] ? selected);
14+
return (
15+
<Drawer
16+
ref="navigation"
17+
type="displace"
18+
content={<Launch />}
19+
tapToClose={true}
20+
openDrawerOffset={0.2}
21+
panCloseMask={0.2}
22+
negotiatePan={true}
23+
tweenHandler={(ratio) => ({
24+
main: { opacity:Math.max(0.54,1-ratio) }
25+
})}>
26+
<DefaultRenderer navigationState={selected} key={selected.key} {...selected} />
27+
</Drawer>
28+
);
29+
}
30+
}

Example/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"react-native-button": "^1.2.1",
1111
"react": "^0.14.7",
1212
"react-native-router-flux": "file:../",
13-
"react-native-modalbox": "^1.3.0"
13+
"react-native-modalbox": "^1.3.0",
14+
"react-native-drawer": "^1.16.7"
1415
}
1516
}

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class App extends React.Component {
6868
* Actions.ACTION_NAME(PARAMS) will call appropriate action and params will be passed to the scene.
6969
* Actions.pop() will pop the current screen.
7070
* Actions.refresh(PARAMS) will update the properties of current screen.
71-
71+
7272
## Available imports
7373
- Router
7474
- Scene
@@ -86,7 +86,7 @@ class App extends React.Component {
8686
| Property | Type | Default | Description |
8787
|---------------|----------|--------------|----------------------------------------------------------------|
8888
| reducer | function | | optional user-defined reducer for scenes, you may want to use it to intercept all actions and put your custom logic |
89-
| createReducer | function | | function that returns a reducer function for {initialState, scenes} param, you may wrap Reducer(param) with your custom reducer, check Flux usage section below|
89+
| createReducer | function | | function that returns a reducer function for {initialState, scenes} param, you may wrap Reducer(param) with your custom reducer, check Flux usage section below|
9090
| other props | | | all properties that will be passed to all your scenes |
9191
| children | | required (if no scenes property passed)| Scene root element |
9292
| scenes | object | optional | scenes for Router created with Actions.create. This will allow to create all actions BEFORE React processing. If you don't need it you may pass Scene root element as children |
@@ -100,7 +100,7 @@ class App extends React.Component {
100100
| tabs| bool | false | Defines 'TabBar' scene container, so child scenes will be displayed as 'tabs'. If no `component` is defined, built-in `TabBar` is used as renderer. All child scenes are wrapped into own navbar.
101101
| initial | bool | false | Set to `true` if this is the initial scene |
102102
| duration | number | 250 | Duration of transition (in ms) |
103-
| direction | string | 'horizontal' | direction of animation horizontal/vertical |
103+
| direction | string | 'horizontal' | direction of animation horizontal/vertical |
104104
| title | string | null | The title to be displayed in the navigation bar |
105105
| navBar | React.Component | | optional custom NavBar for the scene. Check built-in NavBar of the component for reference |
106106
| hideNavBar | bool | false | hides navigation bar for this scene |
@@ -227,7 +227,7 @@ To display a modal use `Modal` as root renderer, so it will render first element
227227
This component doesn't depend from any redux/flux library. It uses new React Native Navigation API and provide own reducer for its navigation state.
228228
You may provide own one if you need. To avoid creation of initial state, you may pass reducer creator. Example to print all actions:
229229
```javascript
230-
// remember to add the 'Reducer' to your imports along with Router, Scene, ... like so
230+
// remember to add the 'Reducer' to your imports along with Router, Scene, ... like so
231231
// import { Reducer } from 'react-native-router-flux'
232232
const reducerCreate = params=>{
233233
const defaultReducer = Reducer(params);
@@ -258,7 +258,7 @@ Following example chooses scene depending from sessionID using Redux:
258258
```
259259
260260
## Drawer (side menu) integration
261-
Example of Drawer custom renderer based on react-native-drawer. Note that you have to include drawer to static contextTypes of your NavBar to enable show/hide/toggle side menu:
261+
Example of Drawer custom renderer based on react-native-drawer. Note that the build-in NavBar component supports toggling of drawer. The Drawer implementation just needs to have a function: toggle();
262262
263263
```javascript
264264
import React from 'react-native';
@@ -268,7 +268,8 @@ import {DefaultRenderer} from 'react-native-router-flux';
268268

269269
export default class extends React.Component {
270270
render(){
271-
const children = this.props.navigationState.children;
271+
const navigationState = this.props.navigationState;
272+
let selected = navigationState.children[navigationState.index];
272273
return (
273274
//Material Design Style Drawer
274275
<Drawer
@@ -282,7 +283,7 @@ export default class extends React.Component {
282283
tweenHandler={(ratio) => ({
283284
main: { opacity:Math.max(0.54,1-ratio) }
284285
})}>
285-
<DefaultRenderer navigationState={children[0]} />
286+
<DefaultRenderer navigationState={selected} key={selected.key} {...selected} />
286287
</Drawer>
287288

288289
);

src/NavBar.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const {
3232
Header: NavigationHeader,
3333
} = NavigationExperimental;
3434

35-
export default class extends React.Component {
35+
export default class NavBar extends React.Component {
3636
componentWillMount(){
3737
const state = this.props.navigationState;
3838
this._renderRightButton = this._renderRightButton.bind(this);
@@ -78,7 +78,18 @@ export default class extends React.Component {
7878

7979
_renderBackButton() {
8080
if (this.props.navigationState.index === 0) {
81+
if(!!this.context.drawer && typeof this.context.drawer.toggle === 'function'){
82+
return (
83+
<TouchableOpacity style={[styles.backButton, this.props.navigationState.leftButtonStyle]} onPress={() => {
84+
var drawer = this.context.drawer;
85+
drawer.toggle();
86+
}}>
87+
<Image source={require('./menu_burger.png')} style={[styles.backButtonImage, this.props.navigationState.barButtonIconStyle]}/>
88+
</TouchableOpacity>
89+
);
90+
}else{
8191
return null;
92+
}
8293
}
8394
return (
8495
<TouchableOpacity style={[styles.backButton, this.props.navigationState.leftButtonStyle]} onPress={Actions.pop}>
@@ -153,6 +164,11 @@ export default class extends React.Component {
153164

154165
}
155166

167+
168+
NavBar.contextTypes = {
169+
drawer: React.PropTypes.object
170+
}
171+
156172
const styles = StyleSheet.create({
157173
title: {
158174
textAlign: 'center',

src/menu_burger.png

706 Bytes
Loading

0 commit comments

Comments
 (0)