|
| 1 | +import React from 'react'; |
| 2 | +import { View, StyleSheet, TextInput, Platform, Alert, ScrollView, Text } from 'react-native'; |
| 3 | +import { usePreventRemove } from '@react-navigation/native'; |
| 4 | +import { |
| 5 | + createNativeStackNavigator, |
| 6 | + NativeStackNavigationProp, |
| 7 | +} from '@react-navigation/native-stack'; |
| 8 | +import { Button } from '../shared'; |
| 9 | + |
| 10 | +type StackParamList = { |
| 11 | + Main: undefined; |
| 12 | + PreventRemoveScreen: undefined; |
| 13 | + PreventRemoveModal: undefined; |
| 14 | +}; |
| 15 | + |
| 16 | +interface MainScreenProps { |
| 17 | + navigation: NativeStackNavigationProp<StackParamList, 'Main'>; |
| 18 | +} |
| 19 | + |
| 20 | +const MainScreen = ({ navigation }: MainScreenProps): JSX.Element => ( |
| 21 | + <View style={{ ...styles.container, backgroundColor: 'bisque' }}> |
| 22 | + <Button |
| 23 | + title="Go to screen" |
| 24 | + onPress={() => navigation.navigate('PreventRemoveScreen')} |
| 25 | + /> |
| 26 | + <Button |
| 27 | + title="Open modal" |
| 28 | + onPress={() => navigation.navigate('PreventRemoveModal')} |
| 29 | + /> |
| 30 | + <Button onPress={() => navigation.pop()} title="🔙 Back to Examples" /> |
| 31 | + </View> |
| 32 | +); |
| 33 | + |
| 34 | +const PreventRemoveScreen = ({ |
| 35 | + navigation, |
| 36 | +}: { |
| 37 | + navigation: NativeStackNavigationProp<StackParamList>; |
| 38 | +}): JSX.Element => { |
| 39 | + const [text, setText] = React.useState(''); |
| 40 | + |
| 41 | + const hasUnsavedChanges = Boolean(text); |
| 42 | + |
| 43 | + usePreventRemove(hasUnsavedChanges, ({ data }) => { |
| 44 | + Alert.alert( |
| 45 | + 'Discard changes?', |
| 46 | + 'You have unsaved changes. Discard them and leave the screen?', |
| 47 | + [ |
| 48 | + { text: "Don't leave", style: 'cancel', onPress: () => { } }, |
| 49 | + { |
| 50 | + text: 'Discard', |
| 51 | + style: 'destructive', |
| 52 | + onPress: () => navigation.dispatch(data.action), |
| 53 | + }, |
| 54 | + ], |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + return ( |
| 59 | + <View style={{ ...styles.container, backgroundColor: 'thistle' }}> |
| 60 | + <ScrollView |
| 61 | + contentContainerStyle={styles.contentContainer} |
| 62 | + keyboardDismissMode="interactive" |
| 63 | + > |
| 64 | + <TextInput |
| 65 | + autoFocus |
| 66 | + value={text} |
| 67 | + placeholder="Type something to prevent remove…" |
| 68 | + placeholderTextColor="#999" |
| 69 | + onChangeText={setText} |
| 70 | + /> |
| 71 | + <Button |
| 72 | + title="Discard and go back" |
| 73 | + onPress={() => navigation.goBack()} |
| 74 | + /> |
| 75 | + {Array.from({ length: 10 }).map((_, i) => ( |
| 76 | + <Text key={`lorem-ipsum-${i}`} style={styles.loremIpsum}> |
| 77 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do |
| 78 | + eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim |
| 79 | + ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut |
| 80 | + aliquip ex ea commodo consequat. Duis aute irure dolor in |
| 81 | + reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla |
| 82 | + pariatur. Excepteur sint occaecat cupidatat non proident, sunt in |
| 83 | + culpa qui officia deserunt mollit anim id est laborum. |
| 84 | + </Text> |
| 85 | + ))} |
| 86 | + </ScrollView> |
| 87 | + </View> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +const Stack = createNativeStackNavigator<StackParamList>(); |
| 92 | + |
| 93 | +const App = (): JSX.Element => ( |
| 94 | + <Stack.Navigator> |
| 95 | + <Stack.Screen |
| 96 | + name="Main" |
| 97 | + component={MainScreen} |
| 98 | + options={{ title: 'Prevent Remove' }} |
| 99 | + /> |
| 100 | + <Stack.Screen name="PreventRemoveScreen" component={PreventRemoveScreen} /> |
| 101 | + <Stack.Screen |
| 102 | + name="PreventRemoveModal" |
| 103 | + component={PreventRemoveScreen} |
| 104 | + options={{ presentation: 'modal' }} |
| 105 | + /> |
| 106 | + </Stack.Navigator> |
| 107 | +); |
| 108 | + |
| 109 | +const styles = StyleSheet.create({ |
| 110 | + container: { |
| 111 | + flex: 1, |
| 112 | + }, |
| 113 | + contentContainer: { |
| 114 | + paddingTop: 10, |
| 115 | + paddingHorizontal: 10, |
| 116 | + }, |
| 117 | + loremIpsum: { |
| 118 | + marginVertical: 8, |
| 119 | + fontSize: 16, |
| 120 | + color: 'gray', |
| 121 | + }, |
| 122 | +}); |
| 123 | + |
| 124 | +export default App; |
0 commit comments