Skip to content

Commit

Permalink
Merge pull request #49358 from HezekielT/migrate-FormWrapper-from-wit…
Browse files Browse the repository at this point in the history
…hOnyx-to-useOnyx

Migrate form wrapper from with onyx to use onyx
  • Loading branch information
roryabraham authored Oct 3, 2024
2 parents b027baa + 5fa13d5 commit e8c4faf
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/components/Form/FormWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type {RefObject} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {ScrollView as RNScrollView, StyleProp, View, ViewStyle} from 'react-native';
import {Keyboard} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import FormElement from '@components/FormElement';
import SafeAreaConsumer from '@components/SafeAreaConsumer';
Expand All @@ -13,18 +12,13 @@ import ScrollView from '@components/ScrollView';
import ScrollViewWithContext from '@components/ScrollViewWithContext';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import type {OnyxFormKey} from '@src/ONYXKEYS';
import type {Form} from '@src/types/form';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {FormInputErrors, FormProps, InputRefs} from './types';

type FormWrapperOnyxProps = {
/** Contains the form state that must be accessed outside the component */
formState: OnyxEntry<Form>;
};

type FormWrapperProps = ChildrenProps &
FormWrapperOnyxProps &
FormProps & {
/** Submit button styles */
submitButtonStyles?: StyleProp<ViewStyle>;
Expand All @@ -48,7 +42,6 @@ type FormWrapperProps = ChildrenProps &
function FormWrapper({
onSubmit,
children,
formState,
errors,
inputRefs,
submitButtonText,
Expand All @@ -69,6 +62,9 @@ function FormWrapper({
const styles = useThemeStyles();
const formRef = useRef<RNScrollView>(null);
const formContentRef = useRef<View>(null);

const [formState] = useOnyx<OnyxFormKey, Form>(`${formID}`);

const errorMessage = useMemo(() => (formState ? ErrorUtils.getLatestErrorMessage(formState) : undefined), [formState]);

const onFixTheErrorsLinkPressed = useCallback(() => {
Expand Down Expand Up @@ -189,10 +185,4 @@ function FormWrapper({

FormWrapper.displayName = 'FormWrapper';

export default withOnyx<FormWrapperProps, FormWrapperOnyxProps>({
formState: {
// withOnyx typings are not able to handle such generic cases like this one, since it's a generic component we need to cast the keys to any
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
key: (props) => props.formID as any,
},
})(FormWrapper);
export default FormWrapper;

0 comments on commit e8c4faf

Please sign in to comment.