Skip to content

Commit fee8981

Browse files
committed
Merge pull request #400 from joenoon/jn-pushToCurrent2
clone option on Scene to dynamically push to current parent
2 parents 38985d6 + 2f1ce43 commit fee8981

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

Example/Example.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Scene, Reducer, Router, Switch, TabBar, Modal, Schema, Actions} from 're
77
import Error from './components/Error'
88
import Home from './components/Home'
99
import TabView from './components/TabView'
10+
import EchoView from './components/EchoView'
1011

1112
class TabIcon extends React.Component {
1213
render(){
@@ -47,6 +48,7 @@ export default class Example extends React.Component {
4748
return <Router createReducer={reducerCreate} sceneStyle={{backgroundColor:'#F7F7F7'}}>
4849
<Scene key="modal" component={Modal} >
4950
<Scene key="root" hideNavBar={true}>
51+
<Scene key="echo" clone component={EchoView} />
5052
<Scene key="register" component={Register} title="Register"/>
5153
<Scene key="register2" component={Register} title="Register2" duration={1}/>
5254
<Scene key="home" component={Home} title="Replace" type="replace"/>

Example/components/EchoView.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
var React = require('react-native');
4+
var {View, Text, StyleSheet} = React;
5+
var Button = require('react-native-button');
6+
var Actions = require('react-native-router-flux').Actions;
7+
8+
9+
class EchoView extends React.Component {
10+
render(){
11+
return (
12+
<View style={[styles.container, this.props.sceneStyle]}>
13+
<Text style={styles.instructions}>key: {this.props.navigationState.key}</Text>
14+
<Text style={styles.instructions}>sceneKey: {this.props.navigationState.sceneKey}</Text>
15+
<Button onPress={Actions.echo}>push new scene</Button>
16+
<Button onPress={Actions.pop}>pop</Button>
17+
</View>
18+
);
19+
}
20+
}
21+
22+
var styles = StyleSheet.create({
23+
container: {
24+
flex: 1,
25+
justifyContent: 'center',
26+
alignItems: 'center',
27+
backgroundColor: '#F5FCFF',
28+
},
29+
instructions: {
30+
textAlign: 'center',
31+
color: '#333333',
32+
marginBottom: 5,
33+
},
34+
});
35+
36+
module.exports = EchoView;

Example/components/TabView.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TabView extends React.Component {
2323
<Button onPress={Actions.tab3}>Switch to tab3</Button>
2424
<Button onPress={Actions.tab4}>Switch to tab4</Button>
2525
<Button onPress={Actions.tab5}>Switch to tab5</Button>
26+
<Button onPress={Actions.echo}>push new scene</Button>
2627
</View>
2728
);
2829
}
@@ -47,4 +48,4 @@ var styles = StyleSheet.create({
4748
},
4849
});
4950

50-
module.exports = TabView;
51+
module.exports = TabView;

Example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"react-native": "^0.22.0",
1010
"react-native-button": "^1.2.1",
1111
"react": "^0.14.7",
12-
"react-native-router-flux": "^3.2.0",
12+
"react-native-router-flux": "file:../",
1313
"react-native-modalbox": "^1.3.0"
1414
}
1515
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class App extends React.Component {
118118
| renderRightButton | Closure | | optional closure to render the right title / buttons element |
119119
| rightButtonStyle | View style | | optional style override for the container of right title / buttons |
120120
| rightButtonTextStyle | Text style | | optional style override for the right title element |
121+
| clone | bool | | Scenes marked with `clone` will be treated as templates and cloned into the current scene's parent when pushed. See example. |
121122
| other props | | | all properties that will be passed to your component instance |
122123
123124
## Example

src/Reducer.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ function update(state,action){
9999
return inject(state, action, props, state.scenes);
100100
}
101101

102+
var _uniqPush = 0;
103+
102104
function reducer({initialState, scenes}){
103105
assert(initialState, "initialState should not be null");
104106
assert(initialState.key, "initialState.key should not be null");
@@ -110,7 +112,17 @@ function reducer({initialState, scenes}){
110112
assert(state.scenes, "state.scenes is missed");
111113

112114
if (action.key){
113-
assert(state.scenes[action.key], "missed route data for key="+action.key);
115+
let scene = state.scenes[action.key];
116+
assert(scene, "missed route data for key="+action.key);
117+
118+
// clone scene
119+
if (action.type === PUSH_ACTION && scene.clone) {
120+
let uniqKey = `${_uniqPush++}$${scene.key}`;
121+
let clone = {...scene, key: uniqKey, sceneKey: uniqKey, parent: getCurrent(state).parent};
122+
state.scenes[uniqKey] = clone;
123+
action.key = uniqKey;
124+
}
125+
114126
} else {
115127
// set current route for pop action or refresh action
116128
if (action.type === POP_ACTION || action.type === POP_ACTION2 || action.type === REFRESH_ACTION){
@@ -127,7 +139,13 @@ function reducer({initialState, scenes}){
127139
assert(el, "Cannot find element for parent=" + el.parent + " within current state");
128140
}
129141
action.parent = el.sceneKey;
130-
}
142+
}
143+
144+
// remove if clone
145+
if (action.clone && action.sceneKey && (action.type === POP_ACTION || action.type === POP_ACTION2)) {
146+
delete state.scenes[action.sceneKey];
147+
}
148+
131149
}
132150
switch (action.type) {
133151
case POP_ACTION2:

0 commit comments

Comments
 (0)