Skip to content

Commit

Permalink
Merge pull request #49828 from cretadn22/reset-error-on-going-back
Browse files Browse the repository at this point in the history
reset error on going back
  • Loading branch information
MarioExpensify authored Oct 1, 2024
2 parents 8b13d8d + c900bef commit d94d7b8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,16 @@ function FormProvider(
},
[inputValues],
);

const resetErrors = useCallback(() => {
FormActions.clearErrors(formID);
FormActions.clearErrorFields(formID);
setErrors({});
}, [formID]);

useImperativeHandle(forwardedRef, () => ({
resetForm,
resetErrors,
}));

const registerInput = useCallback<RegisterInput>(
Expand Down
1 change: 1 addition & 0 deletions src/components/Form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type FormProps<TFormID extends OnyxFormKey = OnyxFormKey> = {

type FormRef<TFormID extends OnyxFormKey = OnyxFormKey> = {
resetForm: (optionalValue: FormOnyxValues<TFormID>) => void;
resetErrors: () => void;
};

type InputRefs = Record<string, MutableRefObject<InputComponentBaseProps>>;
Expand Down
5 changes: 4 additions & 1 deletion src/pages/MissingPersonalDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {ForwardedRef} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import type {FormInputErrors, FormOnyxValues, FormRef} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import InteractiveStepSubHeader from '@components/InteractiveStepSubHeader';
import type {InteractiveStepSubHeaderHandle} from '@components/InteractiveStepSubHeader';
Expand Down Expand Up @@ -34,6 +34,7 @@ function MissingPersonalDetails() {
const styles = useThemeStyles();
const {translate} = useLocalize();
const ref: ForwardedRef<InteractiveStepSubHeaderHandle> = useRef(null);
const formRef = useRef<FormRef | null>(null);
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);

Expand Down Expand Up @@ -65,6 +66,7 @@ function MissingPersonalDetails() {
return;
}
ref.current?.movePrevious();
formRef.current?.resetErrors();
prevScreen();
};

Expand Down Expand Up @@ -187,6 +189,7 @@ function MissingPersonalDetails() {
/>
</View>
<FormProvider
ref={formRef}
formID={ONYXKEYS.FORMS.PERSONAL_DETAILS_FORM}
submitButtonText={screenIndex === formSteps.length - 1 ? translate('common.confirm') : translate('common.next')}
onSubmit={screenIndex === formSteps.length - 1 ? updatePersonalDetails : handleNextScreen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {ForwardedRef} from 'react';
import {InteractionManager, View} from 'react-native';
import ConnectionLayout from '@components/ConnectionLayout';
import FormProvider from '@components/Form/FormProvider';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import type {FormInputErrors, FormOnyxValues, FormRef} from '@components/Form/types';
import InteractiveStepSubHeader from '@components/InteractiveStepSubHeader';
import type {InteractiveStepSubHeaderHandle} from '@components/InteractiveStepSubHeader';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -31,6 +31,7 @@ function NetSuiteImportAddCustomListPage({policy}: WithPolicyConnectionsProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const ref: ForwardedRef<InteractiveStepSubHeaderHandle> = useRef(null);
const formRef = useRef<FormRef | null>(null);

const config = policy?.connections?.netsuite?.options?.config;
const customLists = useMemo(() => config?.syncOptions?.customLists ?? [], [config?.syncOptions]);
Expand Down Expand Up @@ -67,6 +68,7 @@ function NetSuiteImportAddCustomListPage({policy}: WithPolicyConnectionsProps) {
return;
}
ref.current?.movePrevious();
formRef.current?.resetErrors();
prevScreen();
};

Expand Down Expand Up @@ -156,6 +158,7 @@ function NetSuiteImportAddCustomListPage({policy}: WithPolicyConnectionsProps) {
</View>
<View style={[styles.flexGrow1, styles.mt3]}>
<FormProvider
ref={formRef}
formID={ONYXKEYS.FORMS.NETSUITE_CUSTOM_LIST_ADD_FORM}
submitButtonText={screenIndex === formSteps.length - 1 ? translate('common.confirm') : translate('common.next')}
onSubmit={screenIndex === formSteps.length - 1 ? updateNetSuiteCustomLists : handleNextScreen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {InteractionManager, View} from 'react-native';
import type {ValueOf} from 'type-fest';
import ConnectionLayout from '@components/ConnectionLayout';
import FormProvider from '@components/Form/FormProvider';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import type {FormInputErrors, FormOnyxValues, FormRef} from '@components/Form/types';
import InteractiveStepSubHeader from '@components/InteractiveStepSubHeader';
import type {InteractiveStepSubHeaderHandle} from '@components/InteractiveStepSubHeader';
import useLocalize from '@hooks/useLocalize';
Expand Down Expand Up @@ -35,6 +35,7 @@ function NetSuiteImportAddCustomSegmentPage({policy}: WithPolicyConnectionsProps
const styles = useThemeStyles();
const {translate} = useLocalize();
const ref: ForwardedRef<InteractiveStepSubHeaderHandle> = useRef(null);
const formRef = useRef<FormRef | null>(null);

const config = policy?.connections?.netsuite?.options?.config;
const customSegments = useMemo(() => config?.syncOptions?.customSegments ?? [], [config?.syncOptions]);
Expand Down Expand Up @@ -67,6 +68,7 @@ function NetSuiteImportAddCustomSegmentPage({policy}: WithPolicyConnectionsProps
return;
}
ref.current?.movePrevious();
formRef.current?.resetErrors();
prevScreen();
};

Expand Down Expand Up @@ -206,6 +208,7 @@ function NetSuiteImportAddCustomSegmentPage({policy}: WithPolicyConnectionsProps
renderSubStepContent
) : (
<FormProvider
ref={formRef}
formID={ONYXKEYS.FORMS.NETSUITE_CUSTOM_SEGMENT_ADD_FORM}
submitButtonText={screenIndex === formSteps.length - 1 ? translate('common.confirm') : translate('common.next')}
onSubmit={screenIndex === formSteps.length - 1 ? updateNetSuiteCustomSegments : handleNextScreen}
Expand Down

0 comments on commit d94d7b8

Please sign in to comment.