@@ -21,14 +21,30 @@ import {
21
21
import { useFieldsFromChildComponents } from "./AutoFormContext.js" ;
22
22
import { isAutoInput } from "./AutoInput.js" ;
23
23
24
+ /** When the AutoForm does not have children, these properties are available to control the rendering of the form */
25
+ type AutoFormPropsWithoutChildren = {
26
+ children ?: never ;
27
+
28
+ /** The title at the top of the form. False to omit */
29
+ title ?: string | false ;
30
+
31
+ /** The label to use for the submit button at the bottom of the form */
32
+ submitLabel ?: ReactNode ;
33
+ } ;
34
+
35
+ type AutoFormPropsWithChildren = {
36
+ /** Custom components to render within the form. Using this will override all default field rendering. */
37
+ children ?: ReactNode ;
38
+ } ;
39
+
24
40
/** The props that any <AutoForm/> component accepts */
25
41
export type AutoFormProps <
26
42
GivenOptions extends OptionsType ,
27
43
SchemaT ,
28
44
ActionFunc extends ActionFunction < GivenOptions , any , any , SchemaT , any > | GlobalActionFunction < any > ,
29
45
ExtraFormVariables extends FieldValues = Record < string , unknown > ,
30
46
DefaultValues = ActionFunc [ "variablesType" ] & ExtraFormVariables
31
- > = {
47
+ > = ( AutoFormPropsWithChildren | AutoFormPropsWithoutChildren ) & {
32
48
/** Which action this fom will run on submit */
33
49
action : ActionFunc ;
34
50
/** A record for this form to act on */
@@ -39,32 +55,26 @@ export type AutoFormProps<
39
55
exclude ?: string [ ] ;
40
56
/** A set of field values to pre-populate the form with on load. Only applies to create forms. */
41
57
defaultValues ?: DefaultValues ;
42
- /** The label to use for the submit button at the bottom of the form */
43
- submitLabel ?: ReactNode ;
44
58
/** What to show the user once the form has been submitted successfully */
45
59
successContent ?: ReactNode ;
46
- /** The title at the top of the form. False to omit */
47
- title ?: string | false ;
48
60
/** Selection object to pass to the form to retrieve existing values. This will override the default selection based on included fields */
49
61
select ?: GivenOptions [ "select" ] ;
50
62
/** Called when the form submission completes successfully on the backend */
51
63
onSuccess ?: ( record : UseActionFormHookStateData < ActionFunc > ) => void ;
52
64
/** Called when the form submission errors before sending, during the API call, or if the API call returns an error. */
53
65
onFailure ?: ( error : Error | FieldErrors < ActionFunc [ "variablesType" ] > ) => void ;
54
- /** Custom components to render within the form. Using this will override all default field rendering. */
55
- children ?: ReactNode ;
56
66
/** Enable debug logging for this form */
57
67
debug ?: boolean ;
58
68
} & ( ActionFunc extends AnyActionWithId < GivenOptions >
59
- ? {
60
- /**
61
- * The record identifier to run this action on, if it already exists.
62
- * Should be undefined for create actions, or a record ID (or finder) for update / etc actions
63
- **/
64
- findBy ?: RecordIdentifier ;
65
- }
66
- : // eslint-disable-next-line @typescript-eslint/ban-types
67
- { } ) ;
69
+ ? {
70
+ /**
71
+ * The record identifier to run this action on, if it already exists.
72
+ * Should be undefined for create actions, or a record ID (or finder) for update / etc actions
73
+ **/
74
+ findBy ?: RecordIdentifier ;
75
+ }
76
+ : // eslint-disable-next-line @typescript-eslint/ban-types
77
+ { } ) ;
68
78
69
79
/**
70
80
* React hook for getting the validation schema for a list of fields
@@ -218,7 +228,9 @@ export const useAutoForm = <
218
228
219
229
const { hasCustomFormChildren, fieldSet, registerFields } = useFieldsFromChildComponents ( ) ;
220
230
const hasRegisteredFieldsFromChildren = hasCustomFormChildren && fieldSet . size > 0 ;
221
- const registeredFieldsFromChildren = hasCustomFormChildren ? extractPathsFromChildren ( props . children ) : [ ] ;
231
+ const registeredFieldsFromChildren = hasCustomFormChildren
232
+ ? extractPathsFromChildren ( "children" in props ? props . children : undefined )
233
+ : [ ] ;
222
234
223
235
useEffect ( ( ) => {
224
236
registerFields ( registeredFieldsFromChildren ) ;
0 commit comments