-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
PreventRemove.tsx
180 lines (164 loc) · 4.05 KB
/
PreventRemove.tsx
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import * as React from 'react';
import {
Alert,
View,
TextInput,
ScrollView,
StyleSheet,
Platform,
} from 'react-native';
import { Button } from 'react-native-paper';
import {
useTheme,
CommonActions,
ParamListBase,
NavigationAction,
} from '@react-navigation/native';
import {
createStackNavigator,
StackScreenProps,
} from '@react-navigation/stack';
import Article from '../Shared/Article';
type PreventRemoveParams = {
Article: { author: string };
Input: undefined;
};
const scrollEnabled = Platform.select({ web: true, default: false });
const ArticleScreen = ({
navigation,
route,
}: StackScreenProps<PreventRemoveParams, 'Article'>) => {
return (
<ScrollView>
<View style={styles.buttons}>
<Button
mode="contained"
onPress={() => navigation.push('Input')}
style={styles.button}
>
Push Input
</Button>
<Button
mode="outlined"
onPress={() => navigation.popToTop()}
style={styles.button}
>
Pop to top
</Button>
</View>
<Article
author={{ name: route.params?.author ?? 'Unknown' }}
scrollEnabled={scrollEnabled}
/>
</ScrollView>
);
};
const InputScreen = ({
navigation,
}: StackScreenProps<PreventRemoveParams, 'Input'>) => {
const [text, setText] = React.useState('');
const { colors } = useTheme();
const hasUnsavedChanges = Boolean(text);
React.useEffect(
() =>
navigation.addListener('beforeRemove', (e) => {
const action: NavigationAction & { payload?: { confirmed?: boolean } } =
e.data.action;
if (!hasUnsavedChanges || action.payload?.confirmed) {
return;
}
e.preventDefault();
if (Platform.OS === 'web') {
const discard = confirm(
'You have unsaved changes. Discard them and leave the screen?'
);
if (discard) {
navigation.dispatch(action);
}
} else {
Alert.alert(
'Discard changes?',
'You have unsaved changes. Discard them and leave the screen?',
[
{ text: "Don't leave", style: 'cancel', onPress: () => {} },
{
text: 'Discard',
style: 'destructive',
onPress: () => navigation.dispatch(action),
},
]
);
}
}),
[hasUnsavedChanges, navigation]
);
return (
<View style={styles.content}>
<TextInput
autoFocus
style={[
styles.input,
{ backgroundColor: colors.card, color: colors.text },
]}
value={text}
placeholder="Type something…"
onChangeText={setText}
/>
<Button
mode="outlined"
color="tomato"
onPress={() =>
navigation.dispatch({
...CommonActions.goBack(),
payload: { confirmed: true },
})
}
style={styles.button}
>
Discard and go back
</Button>
<Button
mode="outlined"
onPress={() => navigation.push('Article', { author: text })}
style={styles.button}
>
Push Article
</Button>
</View>
);
};
const SimpleStack = createStackNavigator<PreventRemoveParams>();
type Props = StackScreenProps<ParamListBase>;
export default function SimpleStackScreen({ navigation }: Props) {
React.useLayoutEffect(() => {
navigation.setOptions({
headerShown: false,
});
}, [navigation]);
return (
<SimpleStack.Navigator>
<SimpleStack.Screen name="Input" component={InputScreen} />
<SimpleStack.Screen name="Article" component={ArticleScreen} />
</SimpleStack.Navigator>
);
}
const styles = StyleSheet.create({
content: {
flex: 1,
padding: 16,
},
input: {
margin: 8,
padding: 10,
borderRadius: 3,
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'rgba(0, 0, 0, 0.08)',
},
buttons: {
flexDirection: 'row',
padding: 8,
},
button: {
margin: 8,
},
});