Skip to content

Commit 9f74ba1

Browse files
committed
[test] Add test for pushing and popping the same screen
1 parent 40604ed commit 9f74ba1

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

example/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import NestedStack from "./tests/NestedStack";
3131
import NestedStackV4 from "./tests/NestedStack.v4";
3232
import NestedStack2 from "./tests/NestedStack2";
3333
import NestedStack2V4 from "./tests/NestedStack2.v4";
34+
import PushPopSameScreen from "./tests/PushPopSameScreen";
35+
import PushPopSameScreenV4 from "./tests/PushPopSameScreen.v4";
3436
import RevealFromBottomAndroid from "./tests/RevealFromBottomAndroid";
3537
import RevealFromBottomAndroidV4 from "./tests/RevealFromBottomAndroid.v4";
3638
import ScaleFromCenterAndroid from "./tests/ScaleFromCenterAndroid";
@@ -82,6 +84,11 @@ export default () => (
8284
Component={ForwardOnly}
8385
/>
8486
<Test title="BackOnly" ComponentV4={BackOnlyV4} Component={BackOnly} />
87+
<Test
88+
title="PushPopSameScreen"
89+
ComponentV4={PushPopSameScreen}
90+
Component={PushPopSameScreenV4}
91+
/>
8592
<Test
8693
title="NestedStack"
8794
ComponentV4={NestedStackV4}

example/src/screens/DetailScreen.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from "react";
2+
import { StyleSheet } from "react-native";
23
import { NavigationStackProp } from "react-navigation-stack";
34

5+
import { TouchableScale } from "../components";
46
import { Item, defaultItem } from "../data";
57
import { DetailComponent } from "./DetailComponent";
68
import { getDetailSharedElements } from "./getDetailSharedElements";
@@ -9,13 +11,32 @@ type Props = {
911
navigation: NavigationStackProp<any>;
1012
route: any; // v5
1113
modal: "none" | "full" | "sheet";
14+
onPress?: ({
15+
navigation,
16+
item,
17+
}: {
18+
navigation: NavigationStackProp<any>;
19+
item: Item;
20+
}) => void;
1221
};
1322

1423
export const DetailScreen = (props: Props) => {
15-
const { navigation, route, modal } = props;
24+
const { navigation, route, modal, onPress } = props;
1625
const params = route?.params || navigation?.state?.params;
1726
const item: Item = params?.item || defaultItem;
18-
return <DetailComponent item={item} navigation={navigation} modal={modal} />;
27+
const content = (
28+
<DetailComponent item={item} navigation={navigation} modal={modal} />
29+
);
30+
return onPress ? (
31+
<TouchableScale
32+
onPress={() => onPress({ navigation, item })}
33+
style={StyleSheet.absoluteFill}
34+
>
35+
{content}
36+
</TouchableScale>
37+
) : (
38+
content
39+
);
1940
};
2041

2142
DetailScreen.navigationOptions = {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { NavigationContainer } from "@react-navigation/native";
2+
import * as React from "react";
3+
import { createSharedElementStackNavigator } from "react-navigation-shared-element";
4+
5+
import { Item, items } from "../data";
6+
import { createScreen, DetailScreen } from "../screens";
7+
8+
const sameIdItems = items.map((item) => ({ ...item, id: "sameId" }));
9+
10+
const name = "PushPopSameScreen";
11+
12+
const Stack = createSharedElementStackNavigator({
13+
name,
14+
debug: true,
15+
});
16+
17+
const PressableDetailScreen = createScreen(DetailScreen, undefined, undefined, {
18+
onPress: (event: any) => {
19+
const item: Item = event.item;
20+
const itemIdx = sameIdItems.indexOf(item);
21+
const nextItem =
22+
sameIdItems[itemIdx < sameIdItems.length - 2 ? itemIdx + 1 : 0];
23+
event.navigation.push("Detail", {
24+
item: nextItem,
25+
});
26+
},
27+
});
28+
29+
export default () => (
30+
<NavigationContainer>
31+
<Stack.Navigator>
32+
<Stack.Screen
33+
name="Detail"
34+
component={PressableDetailScreen}
35+
initialParams={{ item: sameIdItems[0] }}
36+
sharedElements={(route, otherRoute, showing) => {
37+
const item = route.params.item;
38+
return [
39+
{ id: `${item.id}.image`, animation: "fade" },
40+
{ id: `${item.id}.logo` },
41+
{ id: `${item.id}.gradient`, animation: "fade" },
42+
{ id: `${item.id}.title`, animation: "fade" },
43+
{ id: `${item.id}.description`, animation: "fade" },
44+
{ id: "close" },
45+
];
46+
}}
47+
/>
48+
</Stack.Navigator>
49+
</NavigationContainer>
50+
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createAppContainer } from "react-navigation";
2+
import {
3+
createSharedElementStackNavigator,
4+
SharedElementsComponentConfig,
5+
} from "react-navigation-shared-element/build/v4";
6+
7+
import { Item, items } from "../data";
8+
import { createScreen, DetailScreen } from "../screens";
9+
10+
const sameIdItems = items.map((item) => ({ ...item, id: "sameId" }));
11+
12+
const name = "PushPopSameScreen";
13+
14+
const sharedElements: SharedElementsComponentConfig = (
15+
route,
16+
otherRoute,
17+
showing
18+
) => {
19+
const item = route.params.item;
20+
return [
21+
{ id: `${item.id}.image`, animation: "fade" },
22+
{ id: `${item.id}.logo` },
23+
{ id: `${item.id}.gradient`, animation: "fade" },
24+
{ id: `${item.id}.title`, animation: "fade" },
25+
{ id: `${item.id}.description`, animation: "fade" },
26+
{ id: "close" },
27+
];
28+
};
29+
30+
const StackNavigator = createSharedElementStackNavigator(
31+
{
32+
Detail: createScreen(DetailScreen, undefined, sharedElements, {
33+
onPress: (event: any) => {
34+
const item: Item = event.item;
35+
const itemIdx = sameIdItems.indexOf(item);
36+
const nextItem =
37+
sameIdItems[itemIdx < sameIdItems.length - 2 ? itemIdx + 1 : 0];
38+
event.navigation.push("Detail", {
39+
item: nextItem,
40+
});
41+
},
42+
}),
43+
},
44+
{
45+
initialRouteParams: {
46+
item: sameIdItems[0],
47+
},
48+
},
49+
{
50+
name,
51+
debug: true,
52+
}
53+
);
54+
55+
export default createAppContainer(StackNavigator);

0 commit comments

Comments
 (0)