refactor: drop getStateForChildUpdate in favor of getStateForRouteFocus (#15)

This commit is contained in:
Satyajit Sahoo
2019-07-20 14:05:30 +02:00
committed by Michał Osadnik
parent 3d8ba13135
commit 44b2ace9ee
12 changed files with 269 additions and 306 deletions

View File

@@ -96,6 +96,20 @@ const StackRouter: Router<CommonAction | Action> = {
};
},
getStateForRouteFocus(state, key) {
const index = state.routes.findIndex(r => r.key === key);
if (index === -1 || index === state.index) {
return state;
}
return {
...state,
index,
routes: state.routes.slice(0, index + 1),
};
},
getStateForAction(state, action) {
switch (action.type) {
case 'PUSH':
@@ -219,27 +233,6 @@ const StackRouter: Router<CommonAction | Action> = {
}
},
getStateForChildUpdate(state, { update, focus, key }) {
const index = state.routes.findIndex(r => r.key === key);
if (index === -1) {
return state;
}
return {
...state,
index: focus ? index : state.index,
routes: focus
? [
...state.routes.slice(0, index),
{ ...state.routes[index], state: update },
]
: state.routes.map((route, i) =>
i === index ? { ...route, state: update } : route
),
};
},
shouldActionPropagateToChildren(action) {
return action.type === 'NAVIGATE';
},