Skip to content

Commit 5adf9c9

Browse files
committed
Refactor
1 parent b5f25eb commit 5adf9c9

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

packages/core/src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,23 @@ export type ProgressSettings = {
583583

584584
export type UrlMethodPair = { url: string; method: Method }
585585

586+
export type UseFormTransformCallback<TForm> = (data: TForm) => object
587+
export type UseFormWithPrecognitionArguments =
588+
| [Method | (() => Method), string | (() => string)]
589+
| [UrlMethodPair | (() => UrlMethodPair)]
590+
586591
type UseFormInertiaArguments<TForm> = [data: TForm | (() => TForm)] | [rememberKey: string, data: TForm | (() => TForm)]
587592
type UseFormPrecognitionArguments<TForm> =
588593
| [urlMethodPair: UrlMethodPair | (() => UrlMethodPair), data: TForm | (() => TForm)]
589594
| [method: Method | (() => Method), url: string | (() => string), data: TForm | (() => TForm)]
590595
export type UseFormArguments<TForm> = UseFormInertiaArguments<TForm> | UseFormPrecognitionArguments<TForm>
591596

597+
export type UseFormSubmitOptions = Omit<VisitOptions, 'data'>
598+
export type UseFormSubmitArguments =
599+
| [Method, string, UseFormSubmitOptions?]
600+
| [UrlMethodPair, UseFormSubmitOptions?]
601+
| [UseFormSubmitOptions?]
602+
592603
export type FormComponentOptions = Pick<
593604
VisitOptions,
594605
'preserveScroll' | 'preserveState' | 'preserveUrl' | 'replace' | 'only' | 'except' | 'reset'

packages/core/src/useFormUtils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { FormDataType, Method, UrlMethodPair, UseFormArguments } from './types'
1+
import { FormDataType, Method, UrlMethodPair, UseFormArguments, UseFormSubmitOptions, UseFormSubmitArguments } from './types'
2+
import { isUrlMethodPair } from './url'
23

34
export class UseFormUtils {
45
public static normalizeWayfinderArgsToCallback(
@@ -58,4 +59,22 @@ export class UseFormUtils {
5859
precognitionEndpoint: this.normalizeWayfinderArgsToCallback(args[0], args[1]),
5960
}
6061
}
62+
63+
public static parseSubmitArgs(
64+
args: UseFormSubmitArguments,
65+
precognitionEndpoint: (() => UrlMethodPair) | null,
66+
): { method: Method; url: string; options: UseFormSubmitOptions } {
67+
if (args.length === 3 || (args.length === 2 && typeof args[0] === 'string')) {
68+
// All arguments passed, or method + url
69+
return { method: args[0], url: args[1], options: args[2] ?? {} }
70+
}
71+
72+
if (isUrlMethodPair(args[0])) {
73+
// Wayfinder + optional options
74+
return { ...args[0], options: (args[1] ?? {}) as UseFormSubmitOptions }
75+
}
76+
77+
// No arguments, or only options passed, use precognition endpoint...
78+
return { ...precognitionEndpoint!(), options: (args[0] ?? {}) as UseFormSubmitOptions }
79+
}
6180
}

packages/vue3/src/useForm.ts

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
1922
import {
@@ -28,12 +31,6 @@ import { cloneDeep, get, has, isEqual, set } from 'lodash-es'
2831
import { reactive, watch } from 'vue'
2932
import { 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-
3734
export 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

6562
type 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

Comments
 (0)