-
Notifications
You must be signed in to change notification settings - Fork 3k
/
AddressForm.tsx
248 lines (219 loc) · 9.62 KB
/
AddressForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React, {useCallback} from 'react';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import type {Country} from '@src/CONST';
import CONST from '@src/CONST';
import type ONYXKEYS from '@src/ONYXKEYS';
import INPUT_IDS from '@src/types/form/HomeAddressForm';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import AddressSearch from './AddressSearch';
import CountrySelector from './CountrySelector';
import FormProvider from './Form/FormProvider';
import InputWrapper from './Form/InputWrapper';
import type {FormOnyxValues} from './Form/types';
import type {State} from './StateSelector';
import StateSelector from './StateSelector';
import TextInput from './TextInput';
type CountryZipRegex = {
regex?: RegExp;
samples?: string;
};
type AddressFormProps = {
/** Address city field */
city?: string;
/** Address country field */
country?: Country | '';
/** Address state field */
state?: string;
/** Address street line 1 field */
street1?: string;
/** Address street line 2 field */
street2?: string;
/** Address zip code field */
zip?: string;
/** Callback which is executed when the user changes address, city or state */
onAddressChanged?: (value: unknown, key: unknown) => void;
/** Callback which is executed when the user submits his address changes */
onSubmit: (values: FormOnyxValues<typeof ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM | typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>) => void;
/** Whether or not should the form data should be saved as draft */
shouldSaveDraft?: boolean;
/** Text displayed on the bottom submit button */
submitButtonText?: string;
/** A unique Onyx key identifying the form */
formID: typeof ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM | typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM;
};
function AddressForm({
city = '',
country = '',
formID,
onAddressChanged = () => {},
onSubmit,
shouldSaveDraft = false,
state = '',
street1 = '',
street2 = '',
submitButtonText = '',
zip = '',
}: AddressFormProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const zipSampleFormat = (country && (CONST.COUNTRY_ZIP_REGEX_DATA[country] as CountryZipRegex)?.samples) ?? '';
const zipFormat = translate('common.zipCodeExampleFormat', {zipSampleFormat});
const isUSAForm = country === CONST.COUNTRY.US;
/**
* @param translate - translate function
* @param isUSAForm - selected country ISO code is US
* @param values - form input values
* @returns - An object containing the errors for each inputID
*/
const validator = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM | typeof ONYXKEYS.FORMS.HOME_ADDRESS_FORM>): Errors => {
const errors: Errors & {
zipPostCode?: string | string[];
} = {};
const requiredFields = ['addressLine1', 'city', 'country', 'state'] as const;
// Check "State" dropdown is a valid state if selected Country is USA
if (values.country === CONST.COUNTRY.US && !values.state) {
errors.state = translate('common.error.fieldRequired');
}
// Add "Field required" errors if any required field is empty
requiredFields.forEach((fieldKey) => {
const fieldValue = values[fieldKey] ?? '';
if (ValidationUtils.isRequiredFulfilled(fieldValue)) {
return;
}
errors[fieldKey] = translate('common.error.fieldRequired');
});
// If no country is selected, default value is an empty string and there's no related regex data so we default to an empty object
const countryRegexDetails = (values.country ? CONST.COUNTRY_ZIP_REGEX_DATA?.[values.country] : {}) as CountryZipRegex;
// The postal code system might not exist for a country, so no regex either for them.
const countrySpecificZipRegex = countryRegexDetails?.regex;
const countryZipFormat = countryRegexDetails?.samples ?? '';
ErrorUtils.addErrorMessage(errors, 'firstName', translate('bankAccount.error.firstName'));
if (countrySpecificZipRegex) {
if (!countrySpecificZipRegex.test(values.zipPostCode?.trim().toUpperCase())) {
if (ValidationUtils.isRequiredFulfilled(values.zipPostCode?.trim())) {
errors.zipPostCode = translate('privatePersonalDetails.error.incorrectZipFormat', {zipFormat: countryZipFormat});
} else {
errors.zipPostCode = translate('common.error.fieldRequired');
}
}
} else if (!CONST.GENERIC_ZIP_CODE_REGEX.test(values?.zipPostCode?.trim()?.toUpperCase() ?? '')) {
errors.zipPostCode = translate('privatePersonalDetails.error.incorrectZipFormat');
}
return errors;
},
[translate],
);
return (
<FormProvider
style={[styles.flexGrow1, styles.mh5]}
formID={formID}
validate={validator}
onSubmit={onSubmit}
submitButtonText={submitButtonText}
enabledWhenOffline
>
<View>
<InputWrapper
InputComponent={AddressSearch}
inputID={INPUT_IDS.ADDRESS_LINE_1}
label={translate('common.addressLine', {lineNumber: 1})}
onValueChange={(data: unknown, key: unknown) => {
onAddressChanged(data, key);
}}
defaultValue={street1}
renamedInputKeys={{
street: INPUT_IDS.ADDRESS_LINE_1,
street2: INPUT_IDS.ADDRESS_LINE_2,
city: INPUT_IDS.CITY,
state: INPUT_IDS.STATE,
zipCode: INPUT_IDS.ZIP_POST_CODE,
country: INPUT_IDS.COUNTRY as Country,
}}
maxInputLength={CONST.FORM_CHARACTER_LIMIT}
shouldSaveDraft={shouldSaveDraft}
/>
</View>
<View style={styles.formSpaceVertical} />
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.ADDRESS_LINE_2}
label={translate('common.addressLine', {lineNumber: 2})}
aria-label={translate('common.addressLine', {lineNumber: 2})}
role={CONST.ROLE.PRESENTATION}
defaultValue={street2}
maxLength={CONST.FORM_CHARACTER_LIMIT}
spellCheck={false}
shouldSaveDraft={shouldSaveDraft}
/>
<View style={styles.formSpaceVertical} />
<View style={styles.mhn5}>
<InputWrapper
InputComponent={CountrySelector}
inputID={INPUT_IDS.COUNTRY}
value={country}
onValueChange={onAddressChanged}
shouldSaveDraft={shouldSaveDraft}
/>
</View>
<View style={styles.formSpaceVertical} />
{isUSAForm ? (
<View style={styles.mhn5}>
<InputWrapper
InputComponent={StateSelector}
inputID={INPUT_IDS.STATE}
value={state as State}
onValueChange={onAddressChanged}
shouldSaveDraft={shouldSaveDraft}
/>
</View>
) : (
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.STATE}
label={translate('common.stateOrProvince')}
aria-label={translate('common.stateOrProvince')}
role={CONST.ROLE.PRESENTATION}
value={state}
maxLength={CONST.STATE_CHARACTER_LIMIT}
spellCheck={false}
onValueChange={onAddressChanged}
shouldSaveDraft={shouldSaveDraft}
/>
)}
<View style={styles.formSpaceVertical} />
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.CITY}
label={translate('common.city')}
aria-label={translate('common.city')}
role={CONST.ROLE.PRESENTATION}
defaultValue={city}
maxLength={CONST.FORM_CHARACTER_LIMIT}
spellCheck={false}
onValueChange={onAddressChanged}
shouldSaveDraft={shouldSaveDraft}
/>
<View style={styles.formSpaceVertical} />
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.ZIP_POST_CODE}
label={translate('common.zipPostCode')}
aria-label={translate('common.zipPostCode')}
role={CONST.ROLE.PRESENTATION}
autoCapitalize="characters"
defaultValue={zip}
maxLength={CONST.BANK_ACCOUNT.MAX_LENGTH.ZIP_CODE}
hint={zipFormat}
onValueChange={onAddressChanged}
shouldSaveDraft={shouldSaveDraft}
/>
</FormProvider>
);
}
AddressForm.displayName = 'AddressForm';
export default AddressForm;