Skip to content

Commit

Permalink
Merge pull request #47908 from dominictb/fix/47335-formprovider
Browse files Browse the repository at this point in the history
fix: only validateOnBlur if screen is focused
  • Loading branch information
puneetlath authored Aug 29, 2024
2 parents 9275b86 + d9618ca commit fd2a9de
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useFocusEffect} from '@react-navigation/native';
import lodashIsEqual from 'lodash/isEqual';
import type {ForwardedRef, MutableRefObject, ReactNode, RefAttributes} from 'react';
import React, {createRef, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
Expand Down Expand Up @@ -215,6 +216,19 @@ function FormProvider(
onSubmit(trimmedStringValues);
}, [enabledWhenOffline, formState?.isLoading, inputValues, network?.isOffline, onSubmit, onValidate, shouldTrimValues]);

// Keep track of the focus state of the current screen.
// This is used to prevent validating the form on blur before it has been interacted with.
const isFocusedRef = useRef(true);

useFocusEffect(
useCallback(() => {
isFocusedRef.current = true;
return () => {
isFocusedRef.current = false;
};
}, []),
);

const resetForm = useCallback(
(optionalValue: FormOnyxValues) => {
Object.keys(inputValues).forEach((inputID) => {
Expand Down Expand Up @@ -332,7 +346,8 @@ function FormProvider(
return;
}
setTouchedInput(inputID);
if (shouldValidateOnBlur) {
// We don't validate the form on blur in case the current screen is not focused
if (shouldValidateOnBlur && isFocusedRef.current) {
onValidate(inputValues, !hasServerError);
}
}, VALIDATE_DELAY);
Expand Down

0 comments on commit fd2a9de

Please sign in to comment.