mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-29 21:05:29 +08:00
refactor: move replace to stack router
This commit is contained in:
@@ -683,6 +683,145 @@ it('handles pop to top action', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('replaces focused screen with replace', () => {
|
||||
const router = StackRouter({});
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routeParamList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'bar', name: 'bar', params: { fruit: 'orange' } },
|
||||
{ key: 'baz', name: 'baz' },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
},
|
||||
StackActions.replace('qux', { answer: 42 }),
|
||||
options
|
||||
)
|
||||
).toEqual({
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'qux-test', name: 'qux', params: { answer: 42 } },
|
||||
{ key: 'baz', name: 'baz' },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
});
|
||||
});
|
||||
|
||||
it('replaces source screen with replace', () => {
|
||||
const router = StackRouter({});
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routeParamList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'bar', name: 'bar', params: { fruit: 'orange' } },
|
||||
{ key: 'baz', name: 'baz' },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
},
|
||||
{
|
||||
...StackActions.replace('qux', { answer: 42 }),
|
||||
source: 'baz',
|
||||
},
|
||||
options
|
||||
)
|
||||
).toEqual({
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'bar', name: 'bar', params: { fruit: 'orange' } },
|
||||
{ key: 'qux-test', name: 'qux', params: { answer: 42 } },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
});
|
||||
});
|
||||
|
||||
it("doesn't handle replace if source key isn't present", () => {
|
||||
const router = StackRouter({});
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routeParamList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'bar', name: 'bar', params: { fruit: 'orange' } },
|
||||
{ key: 'baz', name: 'baz' },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
},
|
||||
{
|
||||
...StackActions.replace('qux', { answer: 42 }),
|
||||
source: 'magic',
|
||||
},
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("doesn't handle replace if screen to replace with isn't present", () => {
|
||||
const router = StackRouter({});
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routeParamList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routes: [
|
||||
{ key: 'foo', name: 'foo' },
|
||||
{ key: 'bar', name: 'bar', params: { fruit: 'orange' } },
|
||||
{ key: 'baz', name: 'baz' },
|
||||
],
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
},
|
||||
{
|
||||
...StackActions.replace('nonexistent', { answer: 42 }),
|
||||
source: 'magic',
|
||||
},
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('handles push action', () => {
|
||||
const router = StackRouter({});
|
||||
const options = {
|
||||
|
||||
@@ -9,6 +9,12 @@ import {
|
||||
} from '@react-navigation/core';
|
||||
|
||||
export type StackActionType =
|
||||
| {
|
||||
type: 'REPLACE';
|
||||
payload: { name: string; key?: string | undefined; params?: object };
|
||||
source?: string;
|
||||
target?: string;
|
||||
}
|
||||
| {
|
||||
type: 'PUSH';
|
||||
payload: { name: string; key?: string | undefined; params?: object };
|
||||
@@ -37,6 +43,9 @@ export type StackNavigationState = NavigationState & {
|
||||
};
|
||||
|
||||
export const StackActions = {
|
||||
replace(name: string, params?: object): StackActionType {
|
||||
return { type: 'REPLACE', payload: { name, params } };
|
||||
},
|
||||
push(name: string, params?: object): StackActionType {
|
||||
return { type: 'PUSH', payload: { name, params } };
|
||||
},
|
||||
@@ -169,6 +178,35 @@ export default function StackRouter(options: StackRouterOptions) {
|
||||
const { routeParamList } = options;
|
||||
|
||||
switch (action.type) {
|
||||
case 'REPLACE': {
|
||||
const index = action.source
|
||||
? state.routes.findIndex(r => r.key === action.source)
|
||||
: state.index;
|
||||
|
||||
if (index === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { name, key, params } = action.payload;
|
||||
|
||||
if (!state.routeNames.includes(name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
routes: state.routes.map((route, i) =>
|
||||
i === index
|
||||
? {
|
||||
key: key !== undefined ? key : `${name}-${shortid()}`,
|
||||
name,
|
||||
params,
|
||||
}
|
||||
: route
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
case 'PUSH':
|
||||
if (state.routeNames.includes(action.payload.name)) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user