Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset error on going back #49828

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,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 @@ -153,6 +155,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
Loading