|
| 1 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 2 | +import { type SubmitHandler, useForm } from 'react-hook-form'; |
| 3 | +import { KeyboardAvoidingView } from 'react-native'; |
| 4 | +import z from 'zod'; |
| 5 | + |
| 6 | +import { Button, ControlledInput,Text, View } from '@/ui'; |
| 7 | + |
| 8 | +const MIN_PASSWORD_LENGTH = 6; |
| 9 | + |
| 10 | +const passwordSchema = z |
| 11 | + .string({ required_error: 'Password is required' }) |
| 12 | + .min(MIN_PASSWORD_LENGTH, 'Password must be at least 6 characters'); |
| 13 | + |
| 14 | +const schema = z |
| 15 | + .object({ |
| 16 | + email: z |
| 17 | + .string({ required_error: 'Email is required' }) |
| 18 | + .email('Invalid email format'), |
| 19 | + name: z.string({ required_error: 'Name is required' }), |
| 20 | + password: passwordSchema, |
| 21 | + passwordConfirmation: z.string({ |
| 22 | + required_error: 'Password confirmation is required', |
| 23 | + }), |
| 24 | + }) |
| 25 | + .refine((data) => data.password === data.passwordConfirmation, { |
| 26 | + message: 'Passwords do not match', |
| 27 | + path: ['passwordConfirmation'], |
| 28 | + }); |
| 29 | + |
| 30 | +export type FormType = z.infer<typeof schema>; |
| 31 | + |
| 32 | +export type SignUpFormProps = { |
| 33 | + onSubmit?: SubmitHandler<FormType>; |
| 34 | + isPending?: boolean; |
| 35 | +}; |
| 36 | + |
| 37 | +export const SignUpForm = ({ |
| 38 | + onSubmit = () => {}, |
| 39 | + isPending = false, |
| 40 | +}: SignUpFormProps) => { |
| 41 | + const { handleSubmit, control } = useForm<FormType>({ |
| 42 | + resolver: zodResolver(schema), |
| 43 | + }); |
| 44 | + |
| 45 | + return ( |
| 46 | + <KeyboardAvoidingView |
| 47 | + className="flex-1" |
| 48 | + behavior="padding" |
| 49 | + keyboardVerticalOffset={10} |
| 50 | + > |
| 51 | + <View className="flex-1 justify-center p-4"> |
| 52 | + <Text testID="form-title" className="pb-6 text-center text-2xl"> |
| 53 | + Sign Up |
| 54 | + </Text> |
| 55 | + |
| 56 | + <ControlledInput |
| 57 | + testID="email-input" |
| 58 | + autoCapitalize="none" |
| 59 | + autoComplete="email" |
| 60 | + control={control} |
| 61 | + name="email" |
| 62 | + label="Email" |
| 63 | + /> |
| 64 | + <ControlledInput |
| 65 | + testID="name-input" |
| 66 | + control={control} |
| 67 | + name="name" |
| 68 | + label="Name" |
| 69 | + /> |
| 70 | + <ControlledInput |
| 71 | + testID="password-input" |
| 72 | + control={control} |
| 73 | + name="password" |
| 74 | + label="Password" |
| 75 | + placeholder="***" |
| 76 | + secureTextEntry={true} |
| 77 | + /> |
| 78 | + <ControlledInput |
| 79 | + testID="password-confirmation-input" |
| 80 | + control={control} |
| 81 | + name="passwordConfirmation" |
| 82 | + label="Password Confirmation" |
| 83 | + placeholder="***" |
| 84 | + secureTextEntry={true} |
| 85 | + /> |
| 86 | + |
| 87 | + <Button |
| 88 | + testID="sign-up-button" |
| 89 | + label="Sign Up" |
| 90 | + onPress={handleSubmit(onSubmit)} |
| 91 | + loading={isPending} |
| 92 | + disabled={isPending} |
| 93 | + /> |
| 94 | + </View> |
| 95 | + </KeyboardAvoidingView> |
| 96 | + ); |
| 97 | +}; |
0 commit comments