@@ -6,7 +6,7 @@ export interface AutoInputComponent<P> extends React.FC<P> {
6
6
__autoInput : true ;
7
7
}
8
8
9
- export function autoInput < P extends { field : string } > ( Component : React . FC < P > ) : AutoInputComponent < P & { selectPaths ?: string [ ] } > {
9
+ export function autoInput < P extends { field : string } > ( Component : React . FC < P > ) : AutoInputComponent < P > {
10
10
const WrappedComponent : React . FC < P > = ( props ) => {
11
11
const { hasCustomFormChildren, registerFields, fieldSet } = useFieldsFromChildComponents ( ) ;
12
12
const relationshipContext = useRelationshipContext ( ) ;
@@ -22,20 +22,43 @@ export function autoInput<P extends { field: string }>(Component: React.FC<P>):
22
22
return props . field ;
23
23
} , [ relationshipContext , props . field ] ) ;
24
24
25
+ useEffect ( ( ) => {
26
+ registerFields ( [ fieldSetPath ] ) ;
27
+ } , [ registerFields , fieldSetPath ] ) ;
28
+
29
+ if ( hasCustomFormChildren && ! fieldSet . has ( fieldSetPath ) ) {
30
+ // Do not render before registration
31
+ return null ;
32
+ }
33
+
34
+ return < Component { ...props } /> ;
35
+ } ;
36
+
37
+ ( WrappedComponent as AutoInputComponent < P > ) . __autoInput = true ;
38
+
39
+ return WrappedComponent as AutoInputComponent < P > ;
40
+ }
41
+
42
+ export function autoRelationshipForm < P extends { field : string } > (
43
+ Component : React . FC < P >
44
+ ) : AutoInputComponent < P & { selectPaths ?: string [ ] } > {
45
+ const WrappedComponent : React . FC < P > = ( props ) => {
46
+ const { hasCustomFormChildren, registerFields, fieldSet } = useFieldsFromChildComponents ( ) ;
47
+
25
48
const hasSelectPaths = "selectPaths" in props && props . selectPaths ;
26
- const selectPaths = useMemo ( ( ) => ( hasSelectPaths && Array . isArray ( props . selectPaths ) ? props . selectPaths : [ ] ) , [ hasSelectPaths ] ) ;
49
+ const selectPathsToRegister = useMemo (
50
+ ( ) =>
51
+ hasSelectPaths && Array . isArray ( props . selectPaths ) ? props . selectPaths . map ( ( selectPath ) => `${ props . field } .${ selectPath } ` ) : [ ] ,
52
+ [ hasSelectPaths , props . field ]
53
+ ) ;
27
54
28
55
useEffect ( ( ) => {
29
- const fieldsToRegister = [ fieldSetPath ] ;
30
-
31
56
if ( hasSelectPaths ) {
32
- fieldsToRegister . push ( ... selectPaths . map ( ( selectPath ) => ` ${ props . field } . ${ selectPath } ` ) ) ;
57
+ registerFields ( selectPathsToRegister ) ;
33
58
}
59
+ } , [ hasSelectPaths , registerFields , selectPathsToRegister ] ) ;
34
60
35
- registerFields ( fieldsToRegister ) ;
36
- } , [ registerFields , fieldSetPath , selectPaths ] ) ;
37
-
38
- if ( hasCustomFormChildren && ! fieldSet . has ( fieldSetPath ) ) {
61
+ if ( hasCustomFormChildren && ! selectPathsToRegister . every ( ( field ) => fieldSet . has ( field ) ) ) {
39
62
// Do not render before registration
40
63
return null ;
41
64
}
@@ -45,7 +68,7 @@ export function autoInput<P extends { field: string }>(Component: React.FC<P>):
45
68
46
69
( WrappedComponent as AutoInputComponent < P > ) . __autoInput = true ;
47
70
48
- return WrappedComponent as AutoInputComponent < P > ;
71
+ return autoInput ( WrappedComponent as AutoInputComponent < P > ) ;
49
72
}
50
73
51
74
export function isAutoInput ( component : React . ReactElement ) : component is React . ReactElement < any , AutoInputComponent < any > > {
0 commit comments