Skip to content

Commit f8623f9

Browse files
committed
Replace getFieldNames and getUsedFormData with schemaUtils.omitExtraData, remove moved tests, fix relevant tests
1 parent 0fdaef6 commit f8623f9

File tree

2 files changed

+8
-408
lines changed

2 files changed

+8
-408
lines changed

packages/core/src/components/Form.tsx

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ import {
66
ErrorSchema,
77
ErrorTransformer,
88
FormContextType,
9-
GenericObjectType,
109
getTemplate,
1110
getUiOptions,
1211
IdSchema,
1312
isObject,
1413
mergeObjects,
15-
NAME_KEY,
16-
PathSchema,
1714
StrictRJSFSchema,
1815
Registry,
1916
RegistryFieldsType,
2017
RegistryWidgetsType,
2118
RJSFSchema,
2219
RJSFValidationError,
23-
RJSF_ADDITONAL_PROPERTIES_FLAG,
2420
SchemaUtilsType,
2521
shouldRender,
2622
SUBMIT_BTN_OPTIONS_KEY,
@@ -34,9 +30,6 @@ import {
3430
ValidatorType,
3531
Experimental_DefaultFormStateBehavior,
3632
} from '@rjsf/utils';
37-
import _get from 'lodash/get';
38-
import _isEmpty from 'lodash/isEmpty';
39-
import _pick from 'lodash/pick';
4033
import _toPath from 'lodash/toPath';
4134

4235
import getDefaultRegistry from '../getDefaultRegistry';
@@ -506,63 +499,6 @@ export default class Form<
506499
return null;
507500
}
508501

509-
/** Returns the `formData` with only the elements specified in the `fields` list
510-
*
511-
* @param formData - The data for the `Form`
512-
* @param fields - The fields to keep while filtering
513-
*/
514-
getUsedFormData = (formData: T | undefined, fields: string[][]): T | undefined => {
515-
// For the case of a single input form
516-
if (fields.length === 0 && typeof formData !== 'object') {
517-
return formData;
518-
}
519-
520-
// _pick has incorrect type definition, it works with string[][], because lodash/hasIn supports it
521-
const data: GenericObjectType = _pick(formData, fields as unknown as string[]);
522-
if (Array.isArray(formData)) {
523-
return Object.keys(data).map((key: string) => data[key]) as unknown as T;
524-
}
525-
526-
return data as T;
527-
};
528-
529-
/** Returns the list of field names from inspecting the `pathSchema` as well as using the `formData`
530-
*
531-
* @param pathSchema - The `PathSchema` object for the form
532-
* @param [formData] - The form data to use while checking for empty objects/arrays
533-
*/
534-
getFieldNames = (pathSchema: PathSchema<T>, formData?: T): string[][] => {
535-
const getAllPaths = (_obj: GenericObjectType, acc: string[][] = [], paths: string[][] = [[]]) => {
536-
Object.keys(_obj).forEach((key: string) => {
537-
if (typeof _obj[key] === 'object') {
538-
const newPaths = paths.map((path) => [...path, key]);
539-
// If an object is marked with additionalProperties, all its keys are valid
540-
if (_obj[key][RJSF_ADDITONAL_PROPERTIES_FLAG] && _obj[key][NAME_KEY] !== '') {
541-
acc.push(_obj[key][NAME_KEY]);
542-
} else {
543-
getAllPaths(_obj[key], acc, newPaths);
544-
}
545-
} else if (key === NAME_KEY && _obj[key] !== '') {
546-
paths.forEach((path) => {
547-
const formValue = _get(formData, path);
548-
// adds path to fieldNames if it points to a value
549-
// or an empty object/array
550-
if (
551-
typeof formValue !== 'object' ||
552-
_isEmpty(formValue) ||
553-
(Array.isArray(formValue) && formValue.every((val) => typeof val !== 'object'))
554-
) {
555-
acc.push(path);
556-
}
557-
});
558-
}
559-
});
560-
return acc;
561-
};
562-
563-
return getAllPaths(pathSchema);
564-
};
565-
566502
/** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
567503
* `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
568504
* then, if `omitExtraData` and `liveOmit` are turned on, the `formData` will be filterer to remove any extra data not
@@ -590,11 +526,8 @@ export default class Form<
590526
let _retrievedSchema: S | undefined;
591527
if (omitExtraData === true && liveOmit === true) {
592528
_retrievedSchema = schemaUtils.retrieveSchema(schema, formData);
593-
const pathSchema = schemaUtils.toPathSchema(_retrievedSchema, '', formData);
594-
595-
const fieldNames = this.getFieldNames(pathSchema, formData);
529+
newFormData = schemaUtils.omitExtraData(_retrievedSchema, formData);
596530

597-
newFormData = this.getUsedFormData(formData, fieldNames);
598531
state = {
599532
formData: newFormData,
600533
};
@@ -702,11 +635,7 @@ export default class Form<
702635

703636
if (omitExtraData === true) {
704637
const retrievedSchema = schemaUtils.retrieveSchema(schema, newFormData);
705-
const pathSchema = schemaUtils.toPathSchema(retrievedSchema, '', newFormData);
706-
707-
const fieldNames = this.getFieldNames(pathSchema, newFormData);
708-
709-
newFormData = this.getUsedFormData(newFormData, fieldNames);
638+
newFormData = schemaUtils.omitExtraData(retrievedSchema, newFormData);
710639
}
711640

712641
if (noValidate || this.validateForm()) {

0 commit comments

Comments
 (0)