Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: merge initial params on push
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Nov 17, 2019
1 parent c17ad18 commit 11efb06
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 107 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/NavigationContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ it('handle dispatching with ref', () => {
return true;
},

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'REVERSE') {
return {
...state,
routes: state.routes.slice().reverse(),
};
}
return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/__tests__/useDescriptors.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,12 @@ it(`returns false for canGoBack when current router doesn't handle GO_BACK`, ()
const ChildRouter: Router<NavigationState, MockActions> = {
...CurrentMockRouter,

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'GO_BACK') {
return null;
}

return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down Expand Up @@ -470,12 +470,12 @@ it('returns true for canGoBack when current router handles GO_BACK', () => {
const ChildRouter: Router<NavigationState, MockActions> = {
...CurrentMockRouter,

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'GO_BACK') {
return state;
}

return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down Expand Up @@ -537,12 +537,12 @@ it('returns true for canGoBack when parent router handles GO_BACK', () => {
const ChildRouter: Router<NavigationState, MockActions> = {
...CurrentMockRouter,

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'GO_BACK') {
return state;
}

return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/useEventEmitter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ it('fires blur event when a route is removed with a delay', async () => {
};
},

getStateForAction(state, action) {
getStateForAction(state, action, options) {
switch (action.type) {
case 'PUSH':
return {
Expand All @@ -248,7 +248,7 @@ it('fires blur event when a route is removed with a delay', async () => {
};
}
default:
return router.getStateForAction(state, action);
return router.getStateForAction(state, action, options);
}
},

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/__tests__/useOnAction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ it("lets parent handle the action if child didn't", () => {
> = {
...CurrentMockRouter,

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'REVERSE') {
return {
...state,
routes: state.routes.slice().reverse(),
};
}

return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ParentRouter;
Expand Down Expand Up @@ -103,14 +103,14 @@ it("lets children handle the action if parent didn't", () => {
return true;
},

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'REVERSE') {
return {
...state,
routes: state.routes.slice().reverse(),
};
}
return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down Expand Up @@ -229,15 +229,15 @@ it("action doesn't bubble if target is specified", () => {
return true;
},

getStateForAction(state, action) {
getStateForAction(state, action, options) {
if (action.type === 'REVERSE') {
return {
...state,
routes: state.routes.slice().reverse(),
};
}

return CurrentMockRouter.getStateForAction(state, action);
return CurrentMockRouter.getStateForAction(state, action, options);
},
};
return ChildRouter;
Expand Down
25 changes: 12 additions & 13 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export type RouterFactory<
RouterOptions extends DefaultRouterOptions
> = (options: RouterOptions) => Router<State, Action>;

export type RouterConfigOptions = {
routeNames: string[];
routeParamList: ParamListBase;
};

export type Router<
State extends NavigationState,
Action extends NavigationAction
Expand All @@ -134,10 +139,7 @@ export type Router<
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getInitialState(options: {
routeNames: string[];
routeParamList: ParamListBase;
}): State;
getInitialState(options: RouterConfigOptions): State;

/**
* Rehydrate the full navigation state from a given partial state.
Expand All @@ -148,10 +150,7 @@ export type Router<
*/
getRehydratedState(
partialState: PartialState<State> | State,
options: {
routeNames: string[];
routeParamList: ParamListBase;
}
options: RouterConfigOptions
): State;

/**
Expand All @@ -163,10 +162,7 @@ export type Router<
*/
getStateForRouteNamesChange(
state: State,
options: {
routeNames: string[];
routeParamList: ParamListBase;
}
options: RouterConfigOptions
): State;

/**
Expand All @@ -183,10 +179,13 @@ export type Router<
*
* @param state State object to apply the action on.
* @param action Action object to apply.
* @param options.routeNames List of valid route names as defined in the screen components.
* @param options.routeParamsList Object containing params for each route.
*/
getStateForAction(
state: State,
action: Action
action: Action,
options: RouterConfigOptions
): State | PartialState<State> | null;

/**
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/useNavigationBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ export default function useNavigationBuilder<
// The update should be limited to current navigator only, so we call the router manually
const updatedState = router.getStateForAction(
state,
navigate(route.params.screen, route.params.params)
navigate(route.params.screen, route.params.params),
{
routeNames,
routeParamList,
}
);

nextState =
Expand Down Expand Up @@ -309,6 +313,10 @@ export default function useNavigationBuilder<
setState,
key,
listeners: actionListeners,
routerConfigOptions: {
routeNames,
routeParamList,
},
});

const onRouteFocus = useOnRouteFocus({
Expand Down
19 changes: 12 additions & 7 deletions packages/core/src/useNavigationHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ export default function useNavigationHelpers<
isFocused: parentNavigationHelpers
? parentNavigationHelpers.isFocused
: () => true,
canGoBack: () =>
router.getStateForAction(
getState(),
CommonActions.goBack() as Action
) !== null ||
(parentNavigationHelpers && parentNavigationHelpers.canGoBack()) ||
false,
canGoBack: () => {
const state = getState();

return (
router.getStateForAction(state, CommonActions.goBack() as Action, {
routeNames: state.routeNames,
routeParamList: {},
}) !== null ||
(parentNavigationHelpers && parentNavigationHelpers.canGoBack()) ||
false
);
},
} as NavigationHelpers<ParamListBase, EventMap> &
(NavigationProp<ParamListBase, string, any, any, any> | undefined);
}, [
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/useOnAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NavigationState,
PartialState,
Router,
RouterConfigOptions,
} from './types';

type Options = {
Expand All @@ -15,6 +16,7 @@ type Options = {
getState: () => NavigationState;
setState: (state: NavigationState | PartialState<NavigationState>) => void;
listeners: ChildActionListener[];
routerConfigOptions: RouterConfigOptions;
};

/**
Expand All @@ -32,6 +34,7 @@ export default function useOnAction({
setState,
key,
listeners,
routerConfigOptions,
}: Options) {
const {
onAction: onActionParent,
Expand All @@ -40,6 +43,14 @@ export default function useOnAction({
trackAction,
} = React.useContext(NavigationBuilderContext);

const routerConfigOptionsRef = React.useRef<RouterConfigOptions>(
routerConfigOptions
);

React.useEffect(() => {
routerConfigOptionsRef.current = routerConfigOptions;
});

const onAction = React.useCallback(
(
action: NavigationAction,
Expand All @@ -59,7 +70,11 @@ export default function useOnAction({
return false;
}

let result = router.getStateForAction(state, action);
let result = router.getStateForAction(
state,
action,
routerConfigOptionsRef.current
);

// If a target is specified and set to current navigator, the action shouldn't bubble
// So instead of `null`, we use the state object for such cases to signal that action was handled
Expand Down
Loading

0 comments on commit 11efb06

Please sign in to comment.