|
| 1 | +import React, { Fragment } from 'react'; |
| 2 | +import { NavigationContainer, ParamListBase } from '@react-navigation/native'; |
| 3 | +import { |
| 4 | + NativeStackNavigationProp, |
| 5 | + createNativeStackNavigator, |
| 6 | +} from '@react-navigation/native-stack'; |
| 7 | +import { Button, StyleSheet, View } from 'react-native'; |
| 8 | +import { ThemedText, ThemedTextInput } from '../shared'; |
| 9 | + |
| 10 | +type StackRouteParamList = { |
| 11 | + Home: undefined; |
| 12 | + FormSheet: undefined; |
| 13 | +}; |
| 14 | + |
| 15 | +type NavigationProp<ParamList extends ParamListBase> = { |
| 16 | + navigation: NativeStackNavigationProp<ParamList>; |
| 17 | +}; |
| 18 | + |
| 19 | +type StackNavigationProp = NavigationProp<StackRouteParamList>; |
| 20 | + |
| 21 | +const Stack = createNativeStackNavigator<StackRouteParamList>(); |
| 22 | + |
| 23 | +function Home({ navigation }: StackNavigationProp) { |
| 24 | + return ( |
| 25 | + <View style={[{ backgroundColor: 'goldenrod', flex: 1 }]}> |
| 26 | + <Button |
| 27 | + title="Open FormSheet" |
| 28 | + onPress={() => navigation.navigate('FormSheet')} |
| 29 | + /> |
| 30 | + </View> |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +function FormSheet({}: StackNavigationProp) { |
| 35 | + const fields = [ |
| 36 | + { name: 'form-first-name', placeholder: 'First Name *', autoFocus: true }, |
| 37 | + { name: 'form-last-name', placeholder: 'Last Name *' }, |
| 38 | + { name: 'form-email', placeholder: 'Email *' }, |
| 39 | + ]; |
| 40 | + |
| 41 | + return ( |
| 42 | + <View testID="form" style={styles.wrapper}> |
| 43 | + <ThemedText testID="form-header" style={styles.heading}> |
| 44 | + Example form |
| 45 | + </ThemedText> |
| 46 | + {fields.map(({ name, placeholder, autoFocus }) => ( |
| 47 | + <Fragment key={name}> |
| 48 | + <ThemedText testID={`${name}-label`} style={styles.label}> |
| 49 | + {placeholder} |
| 50 | + </ThemedText> |
| 51 | + <ThemedTextInput |
| 52 | + autoFocus={autoFocus} |
| 53 | + testID={`${name}-input`} |
| 54 | + style={styles.input} |
| 55 | + /> |
| 56 | + </Fragment> |
| 57 | + ))} |
| 58 | + </View> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export default function App() { |
| 63 | + return ( |
| 64 | + <NavigationContainer> |
| 65 | + <Stack.Navigator> |
| 66 | + <Stack.Screen name="Home" component={Home} /> |
| 67 | + <Stack.Screen |
| 68 | + name="FormSheet" |
| 69 | + component={FormSheet} |
| 70 | + options={{ |
| 71 | + presentation: 'formSheet', |
| 72 | + sheetAllowedDetents: [0.5, 0.85], |
| 73 | + }} |
| 74 | + /> |
| 75 | + </Stack.Navigator> |
| 76 | + </NavigationContainer> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +const styles = StyleSheet.create({ |
| 81 | + wrapper: { |
| 82 | + margin: 15, |
| 83 | + }, |
| 84 | + heading: { |
| 85 | + fontSize: 16, |
| 86 | + fontWeight: 'bold', |
| 87 | + marginBottom: 16, |
| 88 | + }, |
| 89 | + label: { |
| 90 | + textTransform: 'capitalize', |
| 91 | + fontSize: 12, |
| 92 | + marginBottom: 8, |
| 93 | + }, |
| 94 | + input: { |
| 95 | + borderWidth: 1, |
| 96 | + borderRadius: 5, |
| 97 | + marginBottom: 12, |
| 98 | + height: 40, |
| 99 | + }, |
| 100 | +}); |
0 commit comments