fix: only propagate navigate to children

This commit is contained in:
satyajit.happy
2019-06-13 10:28:04 +02:00
parent 336482e972
commit b58ea0ebeb
4 changed files with 26 additions and 9 deletions

View File

@@ -73,6 +73,12 @@ export type Router<Action extends NavigationAction = CommonAction> = {
}
): NavigationState;
/**
* Whether the action bubbles to other navigators
* When an action isn't handled by current navigator, it can be passed to nested navigators
*/
shouldActionPropagateToChildren(action: Action): boolean;
/**
* Whether the action should also change focus in parent navigator
*/

View File

@@ -19,7 +19,7 @@ type Options = {
children: React.ReactNode;
};
type HandleAction = (action: NavigationAction, fromKey: string) => boolean;
type HandleAction = (action: NavigationAction, fromKey?: string) => boolean;
const NavigationBuilderContext = React.createContext<{
helpers?: NavigationHelpers;
@@ -134,19 +134,22 @@ export default function useNavigationBuilder(
return true;
}
// If router returned `null`, it didn't handle it
// try to delegate the action to child navigators
for (let i = dispatchListeners.current.length - 1; i >= 0; i--) {
const listener = dispatchListeners.current[i];
if (
fromKey === undefined &&
router.shouldActionPropagateToChildren(action)
) {
// Try to delegate the action to child navigators
for (let i = dispatchListeners.current.length - 1; i >= 0; i--) {
const listener = dispatchListeners.current[i];
if (listener(action, state.key)) {
return true;
if (listener(action, state.key)) {
return true;
}
}
}
if (handleActionParent !== undefined) {
// If non of the child navigators could handle the action, delegate it to parent
// This will enable sibling navigators to handle the action
// Bubble action to the parent if the current navigator didn't handle it
if (handleActionParent(action, state.key)) {
return true;
}