Skip to content

Commit 10cf5e0

Browse files
Add getInitialState API to scene
1 parent 1ec99d5 commit 10cf5e0

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

src/State.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,34 @@
88
*/
99
import assert from "assert";
1010

11+
function getStateFromScenes(route, scenes, props) {
12+
const getters = [];
13+
let result = {};
14+
let scene = route;
15+
while (scene) {
16+
if (scene.getInitialState) {
17+
getters.push(scene.getInitialState);
18+
}
19+
scene = scenes[scene.parent];
20+
}
21+
22+
getters.reverse().forEach(fn => {
23+
result = {...result, ...fn(props)};
24+
});
25+
26+
return result;
27+
}
28+
1129
export function getInitialState(route:{string: any},scenes:{string:any}, position=0, props={}){
1230
const {key, style, type, ...parentProps} = props;
1331
if (!route.children){
14-
return { ...scenes.rootProps, ...route, key:position+"_"+route.sceneKey, ...parentProps,};
32+
return {
33+
...scenes.rootProps,
34+
...route,
35+
key:position+"_"+route.sceneKey,
36+
...parentProps,
37+
...getStateFromScenes(route, scenes, props),
38+
};
1539
}
1640
let {children, ...res} = {...route, ...parentProps};
1741
let index = 0;

test/Actions.test.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,32 @@ import React from 'react-native';
55
import Scene from '../src/Scene';
66
import createReducer from '../src/Reducer';
77

8-
const scenesData = <Scene component="Modal" key="modal">
9-
<Scene key="launch" component="Launch"/>
10-
<Scene key="sideMenu" component="Drawer" initial={true}>
11-
<Scene component="CubeBar" key="cubeBar" type="tabs">
12-
<Scene key="main" tabs={true}>
13-
<Scene key="home" component="Home"/>
14-
<Scene key="map" component="Map"/>
15-
<Scene key="myAccount" component="MyAccount"/>
16-
</Scene>
17-
<Scene key="messaging" initial={true}>
18-
<Scene key="conversations" component="Conversations"/>
19-
</Scene>
20-
</Scene>
21-
</Scene>
22-
<Scene key="privacyPolicy" component="PrivacyPolicy" type="modal"/>
23-
<Scene key="termsOfService" component="TermsOfService" type="modal"/>
24-
<Scene key="login">
25-
<Scene key="loginModal1" component="Login1"/>
26-
<Scene key="loginModal2" component="Login2"/>
27-
</Scene>
28-
</Scene>;
29-
308
describe('Actions', () => {
319
it('should create needed actions', () => {
10+
let id = 0;
11+
const guid = () => id++;
12+
13+
const scenesData = <Scene component="Modal" key="modal" getInitialState={() => ({ foo: guid() })}>
14+
<Scene key="launch" component="Launch"/>
15+
<Scene key="sideMenu" component="Drawer" initial={true}>
16+
<Scene component="CubeBar" key="cubeBar" type="tabs">
17+
<Scene key="main" tabs={true}>
18+
<Scene key="home" component="Home"/>
19+
<Scene key="map" component="Map"/>
20+
<Scene key="myAccount" component="MyAccount"/>
21+
</Scene>
22+
<Scene key="messaging" initial={true}>
23+
<Scene key="conversations" component="Conversations" getInitialState={() => ({ foo: 'what', bar: guid() })}/>
24+
</Scene>
25+
</Scene>
26+
</Scene>
27+
<Scene key="privacyPolicy" component="PrivacyPolicy" type="modal"/>
28+
<Scene key="termsOfService" component="TermsOfService" type="modal"/>
29+
<Scene key="login">
30+
<Scene key="loginModal1" component="Login1"/>
31+
<Scene key="loginModal2" component="Login2"/>
32+
</Scene>
33+
</Scene>;
3234
const scenes = Actions.create(scenesData);
3335
expect(scenes.conversations.component).to.equal("Conversations");
3436

@@ -57,6 +59,8 @@ describe('Actions', () => {
5759
expect(state).equal(undefined);
5860
Actions.init();
5961
expect(state.key).equal("0_modal");
62+
expect(state.children[0].children[0].children[0].children[0].bar).equal(1);
63+
expect(state.children[0].children[0].children[0].children[0].foo).equal('what');
6064

6165
Actions.messaging();
6266
expect(currentScene.key).equal("messaging");
@@ -69,6 +73,9 @@ describe('Actions', () => {
6973
Actions.pop();
7074
expect(state.from.key).equal("1_login");
7175

76+
Actions.launch();
77+
expect(state.children[1].foo).equal(3);
78+
expect(state.children[1].bar).equal(undefined);
7279
});
7380

7481
});

0 commit comments

Comments
 (0)