Skip to content

Commit

Permalink
20 - useImperativeHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasGarcez committed Aug 18, 2024
1 parent 328d3c5 commit 1619c98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/screens/app/EditProfileScreen/EditProfileScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React, {useState} from 'react';
import React, {useRef, useState} from 'react';

import {useUserGetById} from '@domain';

import {Button, Screen} from '@components';
import {AppScreenProps} from '@routes';

import {EditProfileForm} from './components/EditProfileForm';
import {
EditProfileForm,
EditProfileFormRef,
} from './components/EditProfileForm';
import {EditProfileHeader} from './components/EditProfileHeader';

export function EditProfileScreen({
Expand All @@ -14,13 +17,21 @@ export function EditProfileScreen({
const {user} = useUserGetById(route.params.userId);
const [formIsValid, setFormIsValid] = useState(false);

const formRef = useRef<EditProfileFormRef>(null);

function submitForm() {
//TODO:
formRef.current?.onSubmit();
}
return (
<Screen canGoBack scrollable title="Editar Perfil">
<EditProfileHeader user={user} />
{user && <EditProfileForm user={user} onChangeIsValid={setFormIsValid} />}
{user && (
<EditProfileForm
ref={formRef}
user={user}
onChangeIsValid={setFormIsValid}
/>
)}

<Button
mt="s40"
Expand Down
26 changes: 20 additions & 6 deletions src/screens/app/EditProfileScreen/components/EditProfileForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect} from 'react';
import React, {useEffect, useImperativeHandle} from 'react';
import {ActivityIndicator, View} from 'react-native';

import {User, authService} from '@domain';
Expand All @@ -15,18 +15,24 @@ type Props = {
onChangeIsValid: (isValid: boolean) => void;
};

export function EditProfileForm({user, onChangeIsValid}: Props) {
const {control, watch, getFieldState, formState} = useForm<EditProfileSchema>(
{
export type EditProfileFormRef = {
onSubmit: () => void;
};

export function EditProfileFormComponent(
{user, onChangeIsValid}: Props,
ref: React.Ref<EditProfileFormRef>,
) {
const {control, watch, getFieldState, formState, handleSubmit} =
useForm<EditProfileSchema>({
resolver: zodResolver(editProfileSchema),
defaultValues: {
username: user.username,
firstName: user.firstName,
lastName: user.lastName,
},
mode: 'onChange',
},
);
});

const usernameValidation = useAsyncValidation({
watch,
Expand All @@ -40,6 +46,12 @@ export function EditProfileForm({user, onChangeIsValid}: Props) {
onChangeIsValid(formState.isValid && !usernameValidation.notReady);
}, [formState.isValid, onChangeIsValid, usernameValidation.notReady]);

useImperativeHandle(ref, () => ({
onSubmit: () => {
handleSubmit(formValues => console.log(formValues))();
},
}));

return (
<View>
<FormTextInput
Expand Down Expand Up @@ -75,3 +87,5 @@ export function EditProfileForm({user, onChangeIsValid}: Props) {
</View>
);
}

export const EditProfileForm = React.forwardRef(EditProfileFormComponent);

0 comments on commit 1619c98

Please sign in to comment.