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

Commit

Permalink
refactor: only accept name in jumpTo
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 22, 2019
1 parent 5af0f2c commit 83264a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
31 changes: 8 additions & 23 deletions example/TabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
ParamListBase,
Router,
createNavigator,
TargetRoute,
BaseRouter,
} from '../src/index';

Expand All @@ -20,7 +19,7 @@ type Props = {

type Action = {
type: 'JUMP_TO';
payload: { name?: string; key?: string; params?: object };
payload: { name: string; params?: object };
};

export type TabNavigationOptions = {
Expand All @@ -42,8 +41,8 @@ export type TabNavigationProp<ParamList extends ParamListBase> = NavigationProp<
*/
jumpTo<RouteName extends Extract<keyof ParamList, string>>(
...args: ParamList[RouteName] extends void
? [TargetRoute<RouteName>]
: [TargetRoute<RouteName>, ParamList[RouteName]]
? [RouteName]
: [RouteName, ParamList[RouteName]]
): void;
};

Expand Down Expand Up @@ -115,15 +114,13 @@ const TabRouter: Router<Action | CommonAction> = {
case 'NAVIGATE': {
let index = -1;

if (action.payload.key) {
if (action.type === 'NAVIGATE' && action.payload.key) {
index = state.routes.findIndex(
route => route.key === action.payload.key
);
}

if (action.payload.name) {
} else {
index = state.routes.findIndex(
route => route.name === action.payload.name
route => route.key === action.payload.name
);
}

Expand Down Expand Up @@ -165,20 +162,8 @@ const TabRouter: Router<Action | CommonAction> = {
},

actionCreators: {
jumpTo(target: TargetRoute<string>, params?: object) {
if (typeof target === 'string') {
return { type: 'JUMP_TO', payload: { name: target, params } };
} else {
if (
(target.hasOwnProperty('key') && target.hasOwnProperty('name')) ||
(!target.hasOwnProperty('key') && !target.hasOwnProperty('name'))
) {
throw new Error(
'While calling jumpTo you need to specify either name or key'
);
}
return { type: 'JUMP_TO', payload: { ...target, params } };
}
jumpTo(name: string, params?: object) {
return { type: 'JUMP_TO', payload: { name, params } };
},
},
};
Expand Down
10 changes: 7 additions & 3 deletions src/BaseActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export type Action =
| { type: 'GO_BACK' }
| {
type: 'NAVIGATE';
payload: { name?: string; key?: string; params?: object };
payload:
| { name: string; key?: undefined; params?: object }
| { key: string; name?: undefined; params?: object };
}
| {
type: 'REPLACE';
Expand All @@ -16,7 +18,9 @@ export type Action =
}
| {
type: 'SET_PARAMS';
payload: { name?: string; key?: string; params?: object };
payload:
| { name: string; key?: undefined; params?: object }
| { key: string; name?: undefined; params?: object };
};

export function goBack(): Action {
Expand Down Expand Up @@ -49,7 +53,7 @@ export function reset(state: PartialState & { key?: string }): Action {

export function setParams(
params: object,
target: { name?: string; key?: string }
target: { name: string } | { key: string }
): Action {
if (
target &&
Expand Down

0 comments on commit 83264a3

Please sign in to comment.