@@ -6,21 +6,17 @@ import {
6
6
ErrorSchema ,
7
7
ErrorTransformer ,
8
8
FormContextType ,
9
- GenericObjectType ,
10
9
getTemplate ,
11
10
getUiOptions ,
12
11
IdSchema ,
13
12
isObject ,
14
13
mergeObjects ,
15
- NAME_KEY ,
16
- PathSchema ,
17
14
StrictRJSFSchema ,
18
15
Registry ,
19
16
RegistryFieldsType ,
20
17
RegistryWidgetsType ,
21
18
RJSFSchema ,
22
19
RJSFValidationError ,
23
- RJSF_ADDITONAL_PROPERTIES_FLAG ,
24
20
SchemaUtilsType ,
25
21
shouldRender ,
26
22
SUBMIT_BTN_OPTIONS_KEY ,
@@ -34,9 +30,6 @@ import {
34
30
ValidatorType ,
35
31
Experimental_DefaultFormStateBehavior ,
36
32
} from '@rjsf/utils' ;
37
- import _get from 'lodash/get' ;
38
- import _isEmpty from 'lodash/isEmpty' ;
39
- import _pick from 'lodash/pick' ;
40
33
import _toPath from 'lodash/toPath' ;
41
34
42
35
import getDefaultRegistry from '../getDefaultRegistry' ;
@@ -506,63 +499,6 @@ export default class Form<
506
499
return null ;
507
500
}
508
501
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
-
566
502
/** Function to handle changes made to a field in the `Form`. This handler receives an entirely new copy of the
567
503
* `formData` along with a new `ErrorSchema`. It will first update the `formData` with any missing default fields and
568
504
* 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<
590
526
let _retrievedSchema : S | undefined ;
591
527
if ( omitExtraData === true && liveOmit === true ) {
592
528
_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 ) ;
596
530
597
- newFormData = this . getUsedFormData ( formData , fieldNames ) ;
598
531
state = {
599
532
formData : newFormData ,
600
533
} ;
@@ -702,11 +635,7 @@ export default class Form<
702
635
703
636
if ( omitExtraData === true ) {
704
637
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 ) ;
710
639
}
711
640
712
641
if ( noValidate || this . validateForm ( ) ) {
0 commit comments