Skip to content

Commit

Permalink
Merge pull request #30525 from software-mansion-labs/form-migration/B…
Browse files Browse the repository at this point in the history
…ankAccountManualStep

[Form Provider Refactor] BankAccountManualStep
  • Loading branch information
luacmartins authored Nov 22, 2023
2 parents 63e7509 + 86c4e97 commit 8417724
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/components/Form/FormProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const propTypes = {
errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
}),

/** Contains draft values for each input in the form */
draftValues: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.string, PropTypes.bool, PropTypes.number, PropTypes.objectOf(Date)])),

/** Should the button be enabled when offline */
enabledWhenOffline: PropTypes.bool,

Expand Down Expand Up @@ -77,6 +80,7 @@ const defaultProps = {
formState: {
isLoading: false,
},
draftValues: {},
enabledWhenOffline: false,
isSubmitActionDangerous: false,
scrollContextEnabled: false,
Expand All @@ -100,7 +104,7 @@ function getInitialValueByType(valueType) {
}
}

function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnChange, children, formState, network, enabledWhenOffline, onSubmit, ...rest}) {
function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnChange, children, formState, network, enabledWhenOffline, draftValues, onSubmit, ...rest}) {
const inputRefs = useRef({});
const touchedInputs = useRef({});
const [inputValues, setInputValues] = useState({});
Expand Down Expand Up @@ -208,7 +212,9 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC

if (!_.isUndefined(propsToParse.value)) {
inputValues[inputID] = propsToParse.value;
} else if (propsToParse.shouldUseDefaultValue) {
} else if (propsToParse.shouldSaveDraft && !_.isUndefined(draftValues[inputID]) && _.isUndefined(inputValues[inputID])) {
inputValues[inputID] = draftValues[inputID];
} else if (propsToParse.shouldUseDefaultValue && _.isUndefined(inputValues[inputID])) {
// We force the form to set the input value from the defaultValue props if there is a saved valid value
inputValues[inputID] = propsToParse.defaultValue;
} else if (_.isUndefined(inputValues[inputID])) {
Expand Down Expand Up @@ -298,7 +304,7 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
});

if (propsToParse.shouldSaveDraft) {
FormActions.setDraftValues(propsToParse.formID, {[inputKey]: value});
FormActions.setDraftValues(formID, {[inputKey]: value});
}

if (_.isFunction(propsToParse.onValueChange)) {
Expand All @@ -307,7 +313,7 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
},
};
},
[errors, formState, hasServerError, inputValues, onValidate, setTouchedInput, shouldValidateOnBlur, shouldValidateOnChange],
[draftValues, formID, errors, formState, hasServerError, inputValues, onValidate, setTouchedInput, shouldValidateOnBlur, shouldValidateOnChange],
);
const value = useMemo(() => ({registerInput}), [registerInput]);

Expand Down Expand Up @@ -338,5 +344,8 @@ export default compose(
formState: {
key: (props) => props.formID,
},
draftValues: {
key: (props) => `${props.formID}Draft`,
},
}),
)(FormProvider);
16 changes: 10 additions & 6 deletions src/pages/ReimbursementAccount/BankAccountManualStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import React, {useCallback} from 'react';
import {Image} from 'react-native';
import _ from 'underscore';
import CheckboxWithLabel from '@components/CheckboxWithLabel';
import Form from '@components/Form';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
Expand Down Expand Up @@ -86,7 +87,7 @@ function BankAccountManualStep(props) {
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_BANK_ACCOUNT}
onBackButtonPress={props.onBackButtonPress}
/>
<Form
<FormProvider
formID={ONYXKEYS.REIMBURSEMENT_ACCOUNT}
onSubmit={submit}
validate={validate}
Expand All @@ -99,7 +100,8 @@ function BankAccountManualStep(props) {
style={[styles.exampleCheckImage, styles.mb5]}
source={exampleCheckImage(preferredLocale)}
/>
<TextInput
<InputWrapper
InputComponent={TextInput}
autoFocus
shouldDelayFocus={shouldDelayFocus}
inputID="routingNumber"
Expand All @@ -112,7 +114,8 @@ function BankAccountManualStep(props) {
shouldSaveDraft
shouldUseDefaultValue={shouldDisableInputs}
/>
<TextInput
<InputWrapper
InputComponent={TextInput}
inputID="accountNumber"
containerStyles={[styles.mt4]}
label={translate('bankAccount.accountNumber')}
Expand All @@ -124,7 +127,8 @@ function BankAccountManualStep(props) {
shouldSaveDraft
shouldUseDefaultValue={shouldDisableInputs}
/>
<CheckboxWithLabel
<InputWrapper
InputComponent={CheckboxWithLabel}
aria-label={`${translate('common.iAcceptThe')} ${translate('common.expensifyTermsOfService')}`}
style={styles.mt4}
inputID="acceptTerms"
Expand All @@ -137,7 +141,7 @@ function BankAccountManualStep(props) {
defaultValue={props.getDefaultStateForField('acceptTerms', false)}
shouldSaveDraft
/>
</Form>
</FormProvider>
</ScreenWrapper>
);
}
Expand Down

0 comments on commit 8417724

Please sign in to comment.