Skip to content

Commit

Permalink
Merge pull request #406 from Froelund/master
Browse files Browse the repository at this point in the history
Added DrawerExample
  • Loading branch information
aksonov committed Mar 27, 2016
2 parents 07655e8 + 50c381d commit 8acd239
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Example/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Error from './components/Error'
import Home from './components/Home'
import TabView from './components/TabView'
import EchoView from './components/EchoView'
import NavigationDrawer from './components/NavigationDrawer'

class TabIcon extends React.Component {
render(){
Expand Down Expand Up @@ -47,7 +48,8 @@ export default class Example extends React.Component {
render() {
return <Router createReducer={reducerCreate} sceneStyle={{backgroundColor:'#F7F7F7'}}>
<Scene key="modal" component={Modal} >
<Scene key="root" hideNavBar={true}>
<Scene key="drawer" component={NavigationDrawer}>
<Scene key="root">
<Scene key="echo" clone component={EchoView} />
<Scene key="register" component={Register} title="Register"/>
<Scene key="register2" component={Register} title="Register2" duration={1}/>
Expand All @@ -70,6 +72,7 @@ export default class Example extends React.Component {
<Scene key="tab4" component={TabView} title="Tab #4" hideNavBar={true} icon={TabIcon}/>
<Scene key="tab5" component={TabView} title="Tab #5" icon={TabIcon} renderRightButton={()=><Right/>}/>
</Scene>
</Scene>
</Scene>
<Scene key="error" component={Error}/>
</Scene>
Expand Down
30 changes: 30 additions & 0 deletions Example/components/NavigationDrawer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React,{
Component
} from 'react-native'
import Launch from './Launch'
import Drawer from 'react-native-drawer'
import {DefaultRenderer} from 'react-native-router-flux';

export default class Navigation extends Component {
render(){
console.log(this.context);
const navigationState = this.props.navigationState;
let selected = navigationState.children[navigationState.index];
// var activeScene = (selected.name == "root" : selected[0] ? selected);
return (
<Drawer
ref="navigation"
type="displace"
content={<Launch />}
tapToClose={true}
openDrawerOffset={0.2}
panCloseMask={0.2}
negotiatePan={true}
tweenHandler={(ratio) => ({
main: { opacity:Math.max(0.54,1-ratio) }
})}>
<DefaultRenderer navigationState={selected} key={selected.key} {...selected} />
</Drawer>
);
}
}
3 changes: 2 additions & 1 deletion Example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"react-native-button": "^1.2.1",
"react": "^0.14.7",
"react-native-router-flux": "file:../",
"react-native-modalbox": "^1.3.0"
"react-native-modalbox": "^1.3.0",
"react-native-drawer": "^1.16.7"
}
}
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class App extends React.Component {
* Actions.ACTION_NAME(PARAMS) will call appropriate action and params will be passed to the scene.
* Actions.pop() will pop the current screen.
* Actions.refresh(PARAMS) will update the properties of current screen.
## Available imports
- Router
- Scene
Expand All @@ -86,7 +86,7 @@ class App extends React.Component {
| Property | Type | Default | Description |
|---------------|----------|--------------|----------------------------------------------------------------|
| reducer | function | | optional user-defined reducer for scenes, you may want to use it to intercept all actions and put your custom logic |
| 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|
| 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|
| other props | | | all properties that will be passed to all your scenes |
| children | | required (if no scenes property passed)| Scene root element |
| 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 |
Expand All @@ -100,7 +100,7 @@ class App extends React.Component {
| 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.
| initial | bool | false | Set to `true` if this is the initial scene |
| duration | number | 250 | Duration of transition (in ms) |
| direction | string | 'horizontal' | direction of animation horizontal/vertical |
| direction | string | 'horizontal' | direction of animation horizontal/vertical |
| title | string | null | The title to be displayed in the navigation bar |
| navBar | React.Component | | optional custom NavBar for the scene. Check built-in NavBar of the component for reference |
| hideNavBar | bool | false | hides navigation bar for this scene |
Expand Down Expand Up @@ -227,7 +227,7 @@ To display a modal use `Modal` as root renderer, so it will render first element
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.
You may provide own one if you need. To avoid creation of initial state, you may pass reducer creator. Example to print all actions:
```javascript
// remember to add the 'Reducer' to your imports along with Router, Scene, ... like so
// remember to add the 'Reducer' to your imports along with Router, Scene, ... like so
// import { Reducer } from 'react-native-router-flux'
const reducerCreate = params=>{
const defaultReducer = Reducer(params);
Expand Down Expand Up @@ -258,7 +258,7 @@ Following example chooses scene depending from sessionID using Redux:
```
## Drawer (side menu) integration
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:
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();
```javascript
import React from 'react-native';
Expand All @@ -268,7 +268,8 @@ import {DefaultRenderer} from 'react-native-router-flux';

export default class extends React.Component {
render(){
const children = this.props.navigationState.children;
const navigationState = this.props.navigationState;
let selected = navigationState.children[navigationState.index];
return (
//Material Design Style Drawer
<Drawer
Expand All @@ -282,7 +283,7 @@ export default class extends React.Component {
tweenHandler={(ratio) => ({
main: { opacity:Math.max(0.54,1-ratio) }
})}>
<DefaultRenderer navigationState={children[0]} />
<DefaultRenderer navigationState={selected} key={selected.key} {...selected} />
</Drawer>

);
Expand Down
18 changes: 17 additions & 1 deletion src/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
Header: NavigationHeader,
} = NavigationExperimental;

export default class extends React.Component {
export default class NavBar extends React.Component {
componentWillMount(){
const state = this.props.navigationState;
this._renderRightButton = this._renderRightButton.bind(this);
Expand Down Expand Up @@ -78,7 +78,18 @@ export default class extends React.Component {

_renderBackButton() {
if (this.props.navigationState.index === 0) {
if(!!this.context.drawer && typeof this.context.drawer.toggle === 'function'){
return (
<TouchableOpacity style={[styles.backButton, this.props.navigationState.leftButtonStyle]} onPress={() => {
var drawer = this.context.drawer;
drawer.toggle();
}}>
<Image source={require('./menu_burger.png')} style={[styles.backButtonImage, this.props.navigationState.barButtonIconStyle]}/>
</TouchableOpacity>
);
}else{
return null;
}
}
return (
<TouchableOpacity style={[styles.backButton, this.props.navigationState.leftButtonStyle]} onPress={Actions.pop}>
Expand Down Expand Up @@ -153,6 +164,11 @@ export default class extends React.Component {

}


NavBar.contextTypes = {
drawer: React.PropTypes.object
}

const styles = StyleSheet.create({
title: {
textAlign: 'center',
Expand Down
Binary file added src/menu_burger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8acd239

Please sign in to comment.