-
Notifications
You must be signed in to change notification settings - Fork 18
/
real-example.tsx
116 lines (109 loc) · 3.76 KB
/
real-example.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
import React from 'react';
import { useController, useForm } from 'react-hook-form';
import type { TextStyle, ViewStyle } from 'react-native';
import { Alert, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import type { OptionType } from '@mobile-reality/react-native-select-pro';
import { Select } from '@mobile-reality/react-native-select-pro';
import { SafeAreaViewWrapper } from '../components/safe-area-view-wrapper';
import { PROGRAMMING_LANGUAGES } from '../constants/data';
import { SELECT_STYLES } from '../constants/styles';
export const RealExample = () => {
const { control, handleSubmit, formState, watch, reset } = useForm<{ languages: OptionType[] }>(
{
defaultValues: {
languages: [],
},
},
);
const { field } = useController({ name: 'languages', control });
const { languages } = watch();
const onSubmit = () => {
Alert.alert('Your choices has been saved!');
};
return (
<SafeAreaViewWrapper style={styles.container}>
{formState.isSubmitted ? (
<View style={styles.fullWidth}>
<Text style={styles.label}>Your programming languages are:</Text>
{languages.length > 0 &&
languages.map((language, index) => (
<Text key={index} style={styles.languageLabel}>
{language.label}
</Text>
))}
</View>
) : (
<View style={styles.fullWidth}>
<Text style={styles.label}>Select your programming languages</Text>
<Select
styles={SELECT_STYLES}
options={PROGRAMMING_LANGUAGES}
placeholderTextColor="#f34c54"
multiple
onSelect={(option) => {
field.onChange([...field.value, option]);
}}
onRemove={(option) => {
field.onChange(
field.value.filter(
(item) => item.value !== (option as OptionType).value,
),
);
}}
/>
</View>
)}
<TouchableOpacity
style={[
styles.button,
{
backgroundColor: languages.length === 0 ? '#19222f' : '#f34c54',
},
]}
disabled={languages.length === 0}
onPress={() => (formState.isSubmitted ? reset() : handleSubmit(onSubmit)())}
>
<Text style={styles.buttonText}>{formState.isSubmitted ? 'EDIT' : 'SAVE'}</Text>
</TouchableOpacity>
</SafeAreaViewWrapper>
);
};
type Styles = {
container: ViewStyle;
fullWidth: ViewStyle;
label: TextStyle;
languageLabel: TextStyle;
button: ViewStyle;
buttonText: TextStyle;
};
const styles = StyleSheet.create<Styles>({
container: {
backgroundColor: '#000a19',
paddingTop: 200,
justifyContent: 'space-between',
},
fullWidth: {
width: '100%',
},
languageLabel: {
fontSize: 16,
color: '#04e590',
marginBottom: 10,
},
label: {
fontSize: 16,
color: '#f34c54',
marginBottom: 10,
},
button: {
width: '100%',
height: 50,
borderRadius: 25,
justifyContent: 'center',
alignItems: 'center',
},
buttonText: {
fontSize: 16,
fontWeight: 'bold',
},
});