Skip to content

Commit

Permalink
fix: fix params from for the root screen when creating action
Browse files Browse the repository at this point in the history
closes #9006
  • Loading branch information
satya164 committed Oct 30, 2020
1 parent 5eee804 commit e8515f9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
50 changes: 50 additions & 0 deletions packages/core/src/__tests__/getActionFromState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ it('gets navigate action from state', () => {
});
});

it('gets navigate action from state for top-level screen', () => {
const state = {
routes: [
{
name: 'foo',
params: { answer: 42 },
},
],
};

expect(getActionFromState(state)).toEqual({
payload: {
name: 'foo',
params: { answer: 42 },
},
type: 'NAVIGATE',
});
});

it('gets navigate action from state with 2 screens', () => {
const state = {
routes: [
Expand Down Expand Up @@ -205,6 +224,37 @@ it('gets navigate action from state with config', () => {
});
});

it('gets navigate action from state for top-level screen with config', () => {
const state = {
routes: [
{
name: 'foo',
params: { answer: 42 },
},
],
};

const config = {
screens: {
initialRouteName: 'bar',
foo: {
path: 'some-path/:answer',
parse: {
answer: Number,
},
},
},
};

expect(getActionFromState(state, config)).toEqual({
payload: {
name: 'foo',
params: { answer: 42 },
},
type: 'NAVIGATE',
});
});

it('gets navigate action from state with 2 screens including initial route and with config', () => {
const state = {
routes: [
Expand Down
38 changes: 26 additions & 12 deletions packages/core/src/getActionFromState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
PartialRoute,
NavigationState,
PartialState,
CommonActions,
} from '@react-navigation/routers';
import type { PathConfig, PathConfigMap, NestedNavigateParams } from './types';

Expand All @@ -24,14 +25,34 @@ type NavigateAction<State extends NavigationState> = {
export default function getActionFromState(
state: PartialState<NavigationState>,
options?: Options
): NavigateAction<NavigationState> | undefined {
): NavigateAction<NavigationState> | CommonActions.Action | undefined {
// Create a normalized configs object which will be easier to use
const normalizedConfig = options ? createNormalizedConfigItem(options) : {};

let payload;
let current: PartialState<NavigationState> | undefined = state;
let config: ConfigItem | undefined = normalizedConfig;
let params: NestedNavigateParams<NavigationState> = {};
if (state.routes.length === 0) {
return undefined;
}

if (
!(
state.routes.length === 1 ||
(state.routes.length === 2 &&
state.routes[0].name === normalizedConfig?.initialRouteName)
)
) {
return {
type: 'RESET',
payload: state,
};
}

const route = state.routes[state.index ?? state.routes.length - 1];

let current: PartialState<NavigationState> | undefined = route?.state;
let config: ConfigItem | undefined = normalizedConfig?.screens?.[route?.name];
let params: NestedNavigateParams<NavigationState> = { ...route.params };

let payload = route ? { name: route.name, params } : undefined;

while (current) {
if (current.routes.length === 0) {
Expand Down Expand Up @@ -70,13 +91,6 @@ export default function getActionFromState(

current = route.state;
config = config?.screens?.[route.name];

if (!payload) {
payload = {
name: route.name,
params,
};
}
}

if (!payload) {
Expand Down

0 comments on commit e8515f9

Please sign in to comment.