@@ -6,14 +6,17 @@ import {
66 FormDataKeys ,
77 FormDataType ,
88 FormDataValues ,
9- isUrlMethodPair ,
109 Method ,
1110 Progress ,
1211 RequestPayload ,
1312 router ,
1413 UrlMethodPair ,
1514 UseFormArguments ,
15+ UseFormSubmitArguments ,
16+ UseFormSubmitOptions ,
17+ UseFormTransformCallback ,
1618 UseFormUtils ,
19+ UseFormWithPrecognitionArguments ,
1720 VisitOptions ,
1821} from '@inertiajs/core'
1922import {
@@ -28,12 +31,6 @@ import { cloneDeep, get, has, isEqual, set } from 'lodash-es'
2831import { reactive , watch } from 'vue'
2932import { config } from '.'
3033
31- type FormOptions = Omit < VisitOptions , 'data' >
32- type SubmitArgs = [ Method , string , FormOptions ?] | [ UrlMethodPair , FormOptions ?] | [ FormOptions ?]
33- type TransformCallback < TForm > = ( data : TForm ) => object
34-
35- type WithPrecognitionArgs = [ Method | ( ( ) => Method ) , string | ( ( ) => string ) ] | [ UrlMethodPair | ( ( ) => UrlMethodPair ) ]
36-
3734export interface InertiaFormProps < TForm extends object > {
3835 isDirty : boolean
3936 errors : FormDataErrors < TForm >
@@ -43,7 +40,7 @@ export interface InertiaFormProps<TForm extends object> {
4340 wasSuccessful : boolean
4441 recentlySuccessful : boolean
4542 data ( ) : TForm
46- transform ( callback : TransformCallback < TForm > ) : this
43+ transform ( callback : UseFormTransformCallback < TForm > ) : this
4744 defaults ( ) : this
4845 defaults < T extends FormDataKeys < TForm > > ( field : T , value : FormDataValues < TForm , T > ) : this
4946 defaults ( fields : Partial < TForm > ) : this
@@ -52,14 +49,14 @@ export interface InertiaFormProps<TForm extends object> {
5249 resetAndClearErrors < K extends FormDataKeys < TForm > > ( ...fields : K [ ] ) : this
5350 setError < K extends FormDataKeys < TForm > > ( field : K , value : ErrorValue ) : this
5451 setError ( errors : FormDataErrors < TForm > ) : this
55- submit : ( ...args : SubmitArgs ) => void
56- get ( url : string , options ?: FormOptions ) : void
57- post ( url : string , options ?: FormOptions ) : void
58- put ( url : string , options ?: FormOptions ) : void
59- patch ( url : string , options ?: FormOptions ) : void
60- delete ( url : string , options ?: FormOptions ) : void
52+ submit : ( ...args : UseFormSubmitArguments ) => void
53+ get ( url : string , options ?: UseFormSubmitOptions ) : void
54+ post ( url : string , options ?: UseFormSubmitOptions ) : void
55+ put ( url : string , options ?: UseFormSubmitOptions ) : void
56+ patch ( url : string , options ?: UseFormSubmitOptions ) : void
57+ delete ( url : string , options ?: UseFormSubmitOptions ) : void
6158 cancel ( ) : void
62- withPrecognition ( ...args : WithPrecognitionArgs ) : InertiaPrecognitiveForm < TForm >
59+ withPrecognition ( ...args : UseFormWithPrecognitionArguments ) : InertiaPrecognitiveForm < TForm >
6360}
6461
6562type PrecognitionValidationConfig < TKeys > = ValidationConfig & {
@@ -127,25 +124,10 @@ export default function useForm<TForm extends FormDataType<TForm>>(
127124 let defaults = typeof data === 'function' ? cloneDeep ( data ( ) ) : cloneDeep ( data )
128125 let cancelToken : CancelToken | null = null
129126 let recentlySuccessfulTimeoutId : ReturnType < typeof setTimeout >
130- let transform : TransformCallback < TForm > = ( data ) => data
127+ let transform : UseFormTransformCallback < TForm > = ( data ) => data
131128
132129 let validatorRef : Validator | null = null
133130
134- const parseSubmitArgs = ( args : SubmitArgs ) : { method : Method ; url : string ; options : FormOptions } => {
135- if ( args . length === 3 || ( args . length === 2 && typeof args [ 0 ] === 'string' ) ) {
136- // All arguments passed, or method + url
137- return { method : args [ 0 ] , url : args [ 1 ] , options : args [ 2 ] ?? { } }
138- }
139-
140- if ( isUrlMethodPair ( args [ 0 ] ) ) {
141- // Wayfinder + optional options
142- return { ...args [ 0 ] , options : ( args [ 1 ] ?? { } ) as FormOptions }
143- }
144-
145- // No arguments, or only options passed, use precognition endpoint...
146- return { ...precognitionEndpoint ! ( ) , options : ( args [ 0 ] ?? { } ) as FormOptions }
147- }
148-
149131 // Track if defaults was called manually during onSuccess to avoid
150132 // overriding user's custom defaults with automatic behavior.
151133 let defaultsCalledInOnSuccess = false
@@ -159,7 +141,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
159141 progress : null as Progress | null ,
160142 wasSuccessful : false ,
161143 recentlySuccessful : false ,
162- withPrecognition ( ...args : WithPrecognitionArgs ) : InertiaPrecognitiveForm < TForm > {
144+ withPrecognition ( ...args : UseFormWithPrecognitionArguments ) : InertiaPrecognitiveForm < TForm > {
163145 precognitionEndpoint = UseFormUtils . normalizeWayfinderArgsToCallback ( ...args )
164146
165147 // Type assertion helper for accessing precognition state
@@ -253,7 +235,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
253235 return set ( carry , key , get ( this , key ) )
254236 } , { } as Partial < TForm > ) as TForm
255237 } ,
256- transform ( callback : TransformCallback < TForm > ) {
238+ transform ( callback : UseFormTransformCallback < TForm > ) {
257239 transform = callback
258240
259241 return this
@@ -333,8 +315,8 @@ export default function useForm<TForm extends FormDataType<TForm>>(
333315 this . clearErrors ( ...fields )
334316 return this
335317 } ,
336- submit ( ...args : SubmitArgs ) {
337- const { method, url, options } = parseSubmitArgs ( args )
318+ submit ( ...args : UseFormSubmitArguments ) {
319+ const { method, url, options } = UseFormUtils . parseSubmitArgs ( args , precognitionEndpoint )
338320
339321 defaultsCalledInOnSuccess = false
340322
0 commit comments