From b76a832c43d15a70b33cb981354ed32993a31bdb Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Mon, 8 Jul 2024 21:20:19 +0200 Subject: [PATCH] Fix forms not registering input values Ended up ruining the clean looking code. There's probably a way to make it clean AND working --- src/components/FormInput.tsx | 10 ++++----- src/modules/login/Login.tsx | 6 +++++- src/modules/login/useLogin.tsx | 3 ++- src/modules/register/UserRegister.tsx | 26 +++++++++++++++++++++--- src/modules/register/useUserRegister.tsx | 3 ++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/components/FormInput.tsx b/src/components/FormInput.tsx index 91e6e81..3a00011 100644 --- a/src/components/FormInput.tsx +++ b/src/components/FormInput.tsx @@ -1,9 +1,11 @@ import { Input, Text, WarningOutlineIcon, FormControl, IInputProps } from 'native-base'; import React from 'react'; -import { Controller, UseControllerProps, useForm } from 'react-hook-form'; +import { Control, Controller, UseControllerProps } from 'react-hook-form'; import { ReturnKeyTypeOptions } from 'react-native'; type FormInputProps = { + errors: any; // TODO: Figure out how to handle typing this + control: Control; name: string; label: string; rules?: UseControllerProps['rules']; @@ -11,6 +13,8 @@ type FormInputProps = { } & IInputProps; const FormInput = ({ + errors, + control, name, label, rules = { @@ -19,10 +23,6 @@ const FormInput = ({ returnKeyType = 'next', ...rest }: FormInputProps) => { - const { - formState: { errors }, - control, - } = useForm(); const fieldError = errors[name]; return ( { - const { isLoggingIn, errors, setFocus, onSubmit } = useLogin(); + const { isLoggingIn, errors, control, setFocus, onSubmit } = useLogin(); return ( @@ -20,6 +20,8 @@ const Login = () => { name="email" label="E-post / Brukernavn" keyboardType="email-address" + control={control} + errors={errors} autoFocus autoCapitalize="none" autoCorrect={false} @@ -28,6 +30,8 @@ const Login = () => { { const loginError = useAppSelector((store) => store.loginError); const { + control, handleSubmit, formState: { errors }, setFocus, @@ -36,7 +37,7 @@ const useLogin = () => { } }, [isAuthenticated, navigation]); - return { onSubmit, isLoggingIn, setFocus, errors: { ...errors, ...loginError } }; + return { onSubmit, isLoggingIn, setFocus, control, errors: { ...errors, ...loginError } }; }; export default useLogin; diff --git a/src/modules/register/UserRegister.tsx b/src/modules/register/UserRegister.tsx index 2d35fc7..c7bbf5a 100644 --- a/src/modules/register/UserRegister.tsx +++ b/src/modules/register/UserRegister.tsx @@ -13,7 +13,8 @@ import useUserRegister from './useUserRegister'; const { Toast } = Incubator; const UserRegister = () => { - const { isRegisteringUser, onSubmit, isValid, errors, registerError, setFocus, toast, setToast } = useUserRegister(); + const { isRegisteringUser, onSubmit, isValid, control, errors, registerError, setFocus, toast, setToast } = + useUserRegister(); const isDisabled = isRegisteringUser || !isValid; // TODO: validate on type // TODO: highlight errorfields @@ -22,12 +23,27 @@ const UserRegister = () => { <> - setFocus('lastName')} /> - setFocus('email')} /> + setFocus('lastName')} + /> + setFocus('email')} + /> { setFocus('password')} /> { const isAuthenticatedPrev = usePrevious(isAuthenticated); const { + control, handleSubmit, formState: { errors, isValid }, setFocus, @@ -55,6 +56,6 @@ const useUserRegister = () => { } }, [isAuthenticatedPrev, isAuthenticated, navigation]); - return { isRegisteringUser, onSubmit, isValid, registerError, errors, setFocus, toast, setToast }; + return { isRegisteringUser, onSubmit, isValid, registerError, control, errors, setFocus, toast, setToast }; }; export default useUserRegister;