-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Form.js
542 lines (462 loc) · 21.8 KB
/
Form.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import React, {useState, useEffect, useCallback, useMemo, useRef} from 'react';
import lodashGet from 'lodash/get';
import {Keyboard, ScrollView, StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import compose from '../libs/compose';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import * as FormActions from '../libs/actions/FormActions';
import * as ErrorUtils from '../libs/ErrorUtils';
import styles from '../styles/styles';
import CONST from '../CONST';
import FormAlertWithSubmitButton from './FormAlertWithSubmitButton';
import FormSubmit from './FormSubmit';
import SafeAreaConsumer from './SafeAreaConsumer';
import ScrollViewWithContext from './ScrollViewWithContext';
import stylePropTypes from '../styles/stylePropTypes';
import {withNetwork} from './OnyxProvider';
import networkPropTypes from './networkPropTypes';
import Visibility from '../libs/Visibility';
const propTypes = {
/** A unique Onyx key identifying the form */
formID: PropTypes.string.isRequired,
/** Text to be displayed in the submit button */
submitButtonText: PropTypes.string.isRequired,
/** Controls the submit button's visibility */
isSubmitButtonVisible: PropTypes.bool,
/** Callback to validate the form */
validate: PropTypes.func,
/** Callback to submit the form */
onSubmit: PropTypes.func.isRequired,
/** Children to render. */
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
/* Onyx Props */
/** Contains the form state that must be accessed outside of the component */
formState: PropTypes.shape({
/** Controls the loading state of the form */
isLoading: PropTypes.bool,
/** Server side errors keyed by microtime */
errors: PropTypes.objectOf(PropTypes.string),
/** Field-specific server side errors keyed by microtime */
errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
}),
/** Contains draft values for each input in the form */
// eslint-disable-next-line react/forbid-prop-types
draftValues: PropTypes.object,
/** Should the button be enabled when offline */
enabledWhenOffline: PropTypes.bool,
/** Whether the form submit action is dangerous */
isSubmitActionDangerous: PropTypes.bool,
/** Whether the validate() method should run on input changes */
shouldValidateOnChange: PropTypes.bool,
/** Whether the validate() method should run on blur */
shouldValidateOnBlur: PropTypes.bool,
/** Whether ScrollWithContext should be used instead of regular ScrollView.
* Set to true when there's a nested Picker component in Form.
*/
scrollContextEnabled: PropTypes.bool,
/** Container styles */
style: stylePropTypes,
/** Custom content to display in the footer after submit button */
footerContent: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
/** Information about the network */
network: networkPropTypes.isRequired,
...withLocalizePropTypes,
};
const defaultProps = {
isSubmitButtonVisible: true,
formState: {
isLoading: false,
},
draftValues: {},
enabledWhenOffline: false,
isSubmitActionDangerous: false,
scrollContextEnabled: false,
shouldValidateOnChange: true,
shouldValidateOnBlur: true,
footerContent: null,
style: [],
validate: () => ({}),
};
function Form(props) {
const [errors, setErrors] = useState({});
const [inputValues, setInputValues] = useState({...props.draftValues});
const formRef = useRef(null);
const formContentRef = useRef(null);
const inputRefs = useRef({});
const touchedInputs = useRef({});
const focusedInput = useRef(null);
const isFirstRender = useRef(true);
const {validate, onSubmit, children} = props;
const hasServerError = useMemo(() => Boolean(props.formState) && !_.isEmpty(props.formState.errors), [props.formState]);
/**
* @param {Object} values - An object containing the value of each inputID, e.g. {inputID1: value1, inputID2: value2}
* @returns {Object} - An object containing the errors for each inputID, e.g. {inputID1: error1, inputID2: error2}
*/
const onValidate = useCallback(
(values, shouldClearServerError = true) => {
const trimmedStringValues = {};
_.each(values, (inputValue, inputID) => {
if (_.isString(inputValue)) {
trimmedStringValues[inputID] = inputValue.trim();
} else {
trimmedStringValues[inputID] = inputValue;
}
});
if (shouldClearServerError) {
FormActions.setErrors(props.formID, null);
}
FormActions.setErrorFields(props.formID, null);
// Run any validations passed as a prop
const validationErrors = validate(trimmedStringValues);
// Validate the input for html tags. It should supercede any other error
_.each(trimmedStringValues, (inputValue, inputID) => {
// If the input value is empty OR is non-string, we don't need to validate it for HTML tags
if (!inputValue || !_.isString(inputValue)) {
return;
}
const foundHtmlTagIndex = inputValue.search(CONST.VALIDATE_FOR_HTML_TAG_REGEX);
const leadingSpaceIndex = inputValue.search(CONST.VALIDATE_FOR_LEADINGSPACES_HTML_TAG_REGEX);
// Return early if there are no HTML characters
if (leadingSpaceIndex === -1 && foundHtmlTagIndex === -1) {
return;
}
const matchedHtmlTags = inputValue.match(CONST.VALIDATE_FOR_HTML_TAG_REGEX);
let isMatch = _.some(CONST.WHITELISTED_TAGS, (r) => r.test(inputValue));
// Check for any matches that the original regex (foundHtmlTagIndex) matched
if (matchedHtmlTags) {
// Check if any matched inputs does not match in WHITELISTED_TAGS list and return early if needed.
for (let i = 0; i < matchedHtmlTags.length; i++) {
const htmlTag = matchedHtmlTags[i];
isMatch = _.some(CONST.WHITELISTED_TAGS, (r) => r.test(htmlTag));
if (!isMatch) {
break;
}
}
}
if (isMatch && leadingSpaceIndex === -1) {
return;
}
// Add a validation error here because it is a string value that contains HTML characters
validationErrors[inputID] = 'common.error.invalidCharacter';
});
if (!_.isObject(validationErrors)) {
throw new Error('Validate callback must return an empty object or an object with shape {inputID: error}');
}
const touchedInputErrors = _.pick(validationErrors, (inputValue, inputID) => Boolean(touchedInputs.current[inputID]));
if (!_.isEqual(errors, touchedInputErrors)) {
setErrors(touchedInputErrors);
}
return touchedInputErrors;
},
[errors, touchedInputs, props.formID, validate],
);
useEffect(() => {
// We want to skip Form validation on initial render.
// This also avoids a bug where we immediately clear server errors when the loading indicator unmounts and Form remounts with server errors.
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
onValidate(inputValues);
// eslint-disable-next-line react-hooks/exhaustive-deps -- we just want to revalidate the form on update if the preferred locale changed on another device so that errors get translated
}, [props.preferredLocale]);
const errorMessage = useMemo(() => {
const latestErrorMessage = ErrorUtils.getLatestErrorMessage(props.formState);
return typeof latestErrorMessage === 'string' ? latestErrorMessage : '';
}, [props.formState]);
/**
* @param {String} inputID - The inputID of the input being touched
*/
const setTouchedInput = useCallback(
(inputID) => {
touchedInputs.current[inputID] = true;
},
[touchedInputs],
);
const submit = useCallback(() => {
// Return early if the form is already submitting to avoid duplicate submission
if (props.formState.isLoading) {
return;
}
// Touches all form inputs so we can validate the entire form
_.each(inputRefs.current, (inputRef, inputID) => (touchedInputs.current[inputID] = true));
// Validate form and return early if any errors are found
if (!_.isEmpty(onValidate(inputValues))) {
return;
}
// Do not submit form if network is offline and the form is not enabled when offline
if (props.network.isOffline && !props.enabledWhenOffline) {
return;
}
// Call submit handler
onSubmit(inputValues);
}, [props.formState, onSubmit, inputRefs, inputValues, onValidate, touchedInputs, props.network.isOffline, props.enabledWhenOffline]);
/**
* Loops over Form's children and automatically supplies Form props to them
*
* @param {Array | Function | Node} children - An array containing all Form children
* @returns {React.Component}
*/
const childrenWrapperWithProps = useCallback(
(childNodes) => {
const childrenElements = React.Children.map(childNodes, (child) => {
// Just render the child if it is not a valid React element, e.g. text within a <Text> component
if (!React.isValidElement(child)) {
return child;
}
// Depth first traversal of the render tree as the input element is likely to be the last node
if (child.props.children) {
return React.cloneElement(child, {
children: childrenWrapperWithProps(child.props.children),
});
}
// Look for any inputs nested in a custom component, e.g AddressForm or IdentityForm
if (_.isFunction(child.type)) {
const childNode = new child.type(child.props);
// If the custom component has a render method, use it to get the nested children
const nestedChildren = _.isFunction(childNode.render) ? childNode.render() : childNode;
// Render the custom component if it's a valid React element
// If the custom component has nested children, Loop over them and supply From props
if (React.isValidElement(nestedChildren) || lodashGet(nestedChildren, 'props.children')) {
return childrenWrapperWithProps(nestedChildren);
}
// Just render the child if it's custom component not a valid React element, or if it hasn't children
return child;
}
// We check if the child has the inputID prop.
// We don't want to pass form props to non form components, e.g. View, Text, etc
if (!child.props.inputID) {
return child;
}
// We clone the child passing down all form props
const inputID = child.props.inputID;
let defaultValue;
// We need to make sure that checkboxes have correct
// value assigned from the list of draft values
// https://github.com/Expensify/App/issues/16885#issuecomment-1520846065
if (_.isBoolean(props.draftValues[inputID])) {
defaultValue = props.draftValues[inputID];
} else {
defaultValue = props.draftValues[inputID] || child.props.defaultValue;
}
// We want to initialize the input value if it's undefined
if (_.isUndefined(inputValues[inputID])) {
inputValues[inputID] = _.isBoolean(defaultValue) ? defaultValue : defaultValue || '';
}
// We force the form to set the input value from the defaultValue props if there is a saved valid value
if (child.props.shouldUseDefaultValue) {
inputValues[inputID] = child.props.defaultValue;
}
if (!_.isUndefined(child.props.value)) {
inputValues[inputID] = child.props.value;
}
const errorFields = lodashGet(props.formState, 'errorFields', {});
const fieldErrorMessage =
_.chain(errorFields[inputID])
.keys()
.sortBy()
.reverse()
.map((key) => errorFields[inputID][key])
.first()
.value() || '';
return React.cloneElement(child, {
ref: (node) => {
inputRefs.current[inputID] = node;
const {ref} = child;
if (_.isFunction(ref)) {
ref(node);
}
},
value: inputValues[inputID],
// As the text input is controlled, we never set the defaultValue prop
// as this is already happening by the value prop.
defaultValue: undefined,
errorText: errors[inputID] || fieldErrorMessage,
onFocus: (event) => {
focusedInput.current = inputID;
if (_.isFunction(child.props.onFocus)) {
child.props.onFocus(event);
}
},
onBlur: (event) => {
// Only run validation when user proactively blurs the input.
if (Visibility.isVisible() && Visibility.hasFocus()) {
// We delay the validation in order to prevent Checkbox loss of focus when
// the user are focusing a TextInput and proceeds to toggle a CheckBox in
// web and mobile web platforms.
setTimeout(() => {
setTouchedInput(inputID);
if (props.shouldValidateOnBlur) {
onValidate(inputValues, !hasServerError);
}
}, 200);
}
if (_.isFunction(child.props.onBlur)) {
child.props.onBlur(event);
}
},
onTouched: () => {
setTouchedInput(inputID);
},
onInputChange: (value, key) => {
const inputKey = key || inputID;
if (focusedInput.current && focusedInput.current !== inputKey) {
setTouchedInput(focusedInput.current);
}
setInputValues((prevState) => {
const newState = {
...prevState,
[inputKey]: value,
};
if (props.shouldValidateOnChange) {
onValidate(newState);
}
return newState;
});
if (child.props.shouldSaveDraft) {
FormActions.setDraftValues(props.formID, {[inputKey]: value});
}
if (child.props.onValueChange) {
child.props.onValueChange(value, inputKey);
}
},
});
});
return childrenElements;
},
[
errors,
inputRefs,
inputValues,
onValidate,
props.draftValues,
props.formID,
props.formState,
setTouchedInput,
props.shouldValidateOnBlur,
props.shouldValidateOnChange,
hasServerError,
],
);
const scrollViewContent = useCallback(
(safeAreaPaddingBottomStyle) => (
<FormSubmit
ref={formContentRef}
style={StyleSheet.flatten([props.style, safeAreaPaddingBottomStyle])}
onSubmit={submit}
>
{childrenWrapperWithProps(_.isFunction(children) ? children({inputValues}) : children)}
{props.isSubmitButtonVisible && (
<FormAlertWithSubmitButton
buttonText={props.submitButtonText}
isAlertVisible={_.size(errors) > 0 || Boolean(errorMessage) || !_.isEmpty(props.formState.errorFields)}
isLoading={props.formState.isLoading}
message={_.isEmpty(props.formState.errorFields) ? errorMessage : null}
onSubmit={submit}
footerContent={props.footerContent}
onFixTheErrorsLinkPressed={() => {
const errorFields = !_.isEmpty(errors) ? errors : props.formState.errorFields;
const focusKey = _.find(_.keys(inputRefs.current), (key) => _.keys(errorFields).includes(key));
const focusInput = inputRefs.current[focusKey];
// Dismiss the keyboard for non-text fields by checking if the component has the isFocused method, as only TextInput has this method.
if (typeof focusInput.isFocused !== 'function') {
Keyboard.dismiss();
}
// We subtract 10 to scroll slightly above the input
if (focusInput.measureLayout && typeof focusInput.measureLayout === 'function') {
// We measure relative to the content root, not the scroll view, as that gives
// consistent results across mobile and web
focusInput.measureLayout(formContentRef.current, (x, y) => formRef.current.scrollTo({y: y - 10, animated: false}));
}
// Focus the input after scrolling, as on the Web it gives a slightly better visual result
if (focusInput.focus && typeof focusInput.focus === 'function') {
focusInput.focus();
}
}}
containerStyles={[styles.mh0, styles.mt5, styles.flex1]}
enabledWhenOffline={props.enabledWhenOffline}
isSubmitActionDangerous={props.isSubmitActionDangerous}
disablePressOnEnter
/>
)}
</FormSubmit>
),
[
childrenWrapperWithProps,
errors,
formContentRef,
formRef,
errorMessage,
inputRefs,
inputValues,
submit,
props.style,
children,
props.formState,
props.footerContent,
props.enabledWhenOffline,
props.isSubmitActionDangerous,
props.isSubmitButtonVisible,
props.submitButtonText,
],
);
useEffect(() => {
_.each(inputRefs.current, (inputRef, inputID) => {
if (inputRef) {
return;
}
delete inputRefs.current[inputID];
delete touchedInputs.current[inputID];
setInputValues((prevState) => {
const copyPrevState = _.clone(prevState);
delete copyPrevState[inputID];
return copyPrevState;
});
});
// We need to verify that all references and values are still actual.
// We should not store it when e.g. some input has been unmounted.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [children]);
return (
<SafeAreaConsumer>
{({safeAreaPaddingBottomStyle}) =>
props.scrollContextEnabled ? (
<ScrollViewWithContext
style={[styles.w100, styles.flex1]}
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="handled"
ref={formRef}
>
{scrollViewContent(safeAreaPaddingBottomStyle)}
</ScrollViewWithContext>
) : (
<ScrollView
style={[styles.w100, styles.flex1]}
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="handled"
ref={formRef}
>
{scrollViewContent(safeAreaPaddingBottomStyle)}
</ScrollView>
)
}
</SafeAreaConsumer>
);
}
Form.displayName = 'Form';
Form.propTypes = propTypes;
Form.defaultProps = defaultProps;
export default compose(
withLocalize,
withNetwork(),
withOnyx({
formState: {
key: (props) => props.formID,
},
draftValues: {
key: (props) => `${props.formID}Draft`,
},
}),
)(Form);