forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsign-up-form.tsx
103 lines (94 loc) · 3.01 KB
/
sign-up-form.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
import { zodResolver } from '@hookform/resolvers/zod';
import { type SubmitHandler, useForm } from 'react-hook-form';
import { KeyboardAvoidingView } from 'react-native';
import z from 'zod';
import { translate } from '@/core';
import { Button, ControlledInput, Text, View } from '@/ui';
const MIN_PASSWORD_LENGTH = 6;
const passwordSchema = z
.string({ required_error: translate('auth.signUp.error.passwordRequired') })
.min(MIN_PASSWORD_LENGTH, translate('auth.signUp.error.shortPassword'));
const schema = z
.object({
email: z
.string({ required_error: translate('auth.signUp.error.emailRequired') })
.email(translate('auth.signUp.error.emailInvalid')),
name: z.string({
required_error: translate('auth.signUp.error.nameRequired'),
}),
password: passwordSchema,
passwordConfirmation: z.string({
required_error: translate(
'auth.signUp.error.passwordConfirmationRequired',
),
}),
})
.refine((data) => data.password === data.passwordConfirmation, {
message: translate('auth.signUp.error.passwordsDoNotMatch'),
path: ['passwordConfirmation'],
});
export type FormType = z.infer<typeof schema>;
export type SignUpFormProps = {
onSubmit?: SubmitHandler<FormType>;
isPending?: boolean;
};
export const SignUpForm = ({
onSubmit = () => {},
isPending = false,
}: SignUpFormProps) => {
const { handleSubmit, control } = useForm<FormType>({
resolver: zodResolver(schema),
});
return (
<KeyboardAvoidingView
className="flex-1"
behavior="padding"
keyboardVerticalOffset={10}
>
<View className="flex-1 justify-center gap-4 p-4">
<Text testID="form-title" className="text-center text-2xl">
{translate('auth.signUp.title')}
</Text>
<View>
<ControlledInput
testID="email-input"
autoCapitalize="none"
autoComplete="email"
control={control}
name="email"
label={translate('auth.signUp.fields.email')}
/>
<ControlledInput
testID="name-input"
control={control}
name="name"
label={translate('auth.signUp.fields.name')}
/>
<ControlledInput
testID="password-input"
control={control}
name="password"
label={translate('auth.signUp.fields.password')}
placeholder="***"
secureTextEntry={true}
/>
<ControlledInput
testID="password-confirmation-input"
control={control}
name="passwordConfirmation"
label={translate('auth.signUp.fields.password')}
placeholder="***"
secureTextEntry={true}
/>
<Button
testID="sign-up-button"
label={translate('auth.signUp.signUpButton')}
onPress={handleSubmit(onSubmit)}
loading={isPending}
disabled={isPending}
/>
</View>
</View>
</KeyboardAvoidingView>
);
};