-
-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathTest432.tsx
More file actions
161 lines (146 loc) · 4.1 KB
/
Test432.tsx
File metadata and controls
161 lines (146 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { View, Button, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import {
NativeStackScreenProps,
createNativeStackNavigator,
} from '@react-navigation/native-stack';
import React, { useEffect, useLayoutEffect, useState } from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Square } from '../shared';
type StackParamList = {
Home: undefined;
Details: undefined;
Settings: undefined;
Info: undefined;
};
type StackScreenProps<T extends keyof StackParamList> = NativeStackScreenProps<
StackParamList,
T
>;
const HomeScreen = ({ navigation }: StackScreenProps<'Home'>) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
title={'Go to details'}
onPress={() => navigation.navigate('Details')}
testID="home-button-go-to-details"
/>
<Button
title={'Go to info'}
onPress={() => navigation.navigate('Info')}
testID="home-button-go-to-info"
/>
<Button
title={'Show settings'}
onPress={() => navigation.navigate('Settings')}
testID="home-button-show-settings"
/>
</View>
);
};
const DetailsScreen = ({ navigation }: StackScreenProps<'Details'>) => {
const [x, setX] = useState(false);
useEffect(() => {
navigation.setOptions({
headerBackVisible: !x,
headerRight: () =>
x ? (
<Square size={20} color="green" testID="details-headerRight-green" />
) : (
<Square size={10} testID="details-headerRight-red" />
),
});
}, [navigation, x]);
return (
<Button
title="Toggle subviews"
onPress={() => setX(prev => !prev)}
testID="details-button-toggle-subviews"
/>
);
};
const SettingsScreen = () => {
return <Text testID="settings-text">Settings</Text>;
};
const InfoScreen = ({ navigation }: StackScreenProps<'Info'>) => {
const [hasLeftItem, setHasLeftItem] = useState(false);
const square1 = (props: { tintColor?: string }) => (
<View style={{ gap: 8, flexDirection: 'row' }}>
{hasLeftItem && (
<Square
{...props}
color="green"
size={20}
testID="info-headerRight-green-2"
/>
)}
<Square
{...props}
color="green"
size={20}
testID="info-headerRight-green-1"
/>
</View>
);
const square2 = (props: { tintColor?: string }) => (
<Square {...props} color="red" size={20} testID="info-headerLeft-red" />
);
useLayoutEffect(() => {
navigation.setOptions({
headerRight: square1,
headerTitle: undefined,
headerLeft: hasLeftItem ? square2 : undefined,
});
}, [navigation, hasLeftItem]);
return (
<Button
title="Toggle subviews"
onPress={() => setHasLeftItem(prev => !prev)}
testID="info-button-toggle-subviews"
/>
);
};
const Stack = createNativeStackNavigator<StackParamList>();
const StackNavigator = () => {
return (
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
headerRight: () => (
<Square size={20} color="black" testID="home-headerRight" />
),
}}
/>
<Stack.Screen name="Details" component={DetailsScreen} />
<Stack.Screen
name="Info"
component={InfoScreen}
options={{
headerTintColor: 'hotpink',
}}
/>
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{
presentation: 'modal',
animation: 'slide_from_bottom',
headerRight: () => <Square size={30} testID="settings-headerRight" />,
}}
/>
</Stack.Navigator>
);
};
const Tabs = createBottomTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tabs.Navigator screenOptions={{ headerShown: false }}>
<Tabs.Screen name="First" component={StackNavigator} />
<Tabs.Screen name="Second" component={StackNavigator} />
</Tabs.Navigator>
</NavigationContainer>
);
}