Skip to content

Commit b5f25eb

Browse files
committed
Refactor
1 parent ea91780 commit b5f25eb

File tree

5 files changed

+73
-666
lines changed

5 files changed

+73
-666
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Config } from './config'
22
import { Router } from './router'
33

4+
export { UseFormUtils } from './useFormUtils'
5+
46
export { config } from './config'
57
export { getScrollableParent } from './domUtils'
68
export { objectToFormData } from './formData'

packages/core/src/types.ts

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

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

586+
type UseFormInertiaArguments<TForm> = [data: TForm | (() => TForm)] | [rememberKey: string, data: TForm | (() => TForm)]
587+
type UseFormPrecognitionArguments<TForm> =
588+
| [urlMethodPair: UrlMethodPair | (() => UrlMethodPair), data: TForm | (() => TForm)]
589+
| [method: Method | (() => Method), url: string | (() => string), data: TForm | (() => TForm)]
590+
export type UseFormArguments<TForm> = UseFormInertiaArguments<TForm> | UseFormPrecognitionArguments<TForm>
591+
586592
export type FormComponentOptions = Pick<
587593
VisitOptions,
588594
'preserveScroll' | 'preserveState' | 'preserveUrl' | 'replace' | 'only' | 'except' | 'reset'

packages/core/src/useFormUtils.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { FormDataType, Method, UrlMethodPair, UseFormArguments } from './types'
2+
3+
export class UseFormUtils {
4+
public static normalizeWayfinderArgsToCallback(
5+
...args: [UrlMethodPair | (() => UrlMethodPair)] | [Method | (() => Method), string | (() => string)]
6+
): () => UrlMethodPair {
7+
return () => {
8+
if (args.length === 2) {
9+
return {
10+
method: typeof args[0] === 'function' ? args[0]() : args[0],
11+
url: typeof args[1] === 'function' ? args[1]() : args[1],
12+
}
13+
}
14+
15+
return typeof args[0] === 'function' ? args[0]() : args[0]
16+
}
17+
}
18+
19+
public static parseUseFormArgs<TForm extends FormDataType<TForm>>(
20+
...args: UseFormArguments<TForm>
21+
): {
22+
rememberKey: string | null
23+
data: TForm | (() => TForm)
24+
precognitionEndpoint: (() => UrlMethodPair) | null
25+
} {
26+
// Pattern 1: [data: TForm | (() => TForm)]
27+
if (args.length === 1) {
28+
return {
29+
rememberKey: null,
30+
data: args[0] as TForm | (() => TForm),
31+
precognitionEndpoint: null,
32+
}
33+
}
34+
35+
// Pattern 2 & 3: Two arguments - need to distinguish by first arg type
36+
if (args.length === 2) {
37+
if (typeof args[0] === 'string') {
38+
// Pattern 2: [rememberKey: string, data: TForm | (() => TForm)]
39+
return {
40+
rememberKey: args[0],
41+
data: args[1] as TForm | (() => TForm),
42+
precognitionEndpoint: null,
43+
}
44+
} else {
45+
// Pattern 3: [urlMethodPair: UrlMethodPair | (() => UrlMethodPair), data: TForm | (() => TForm)]
46+
return {
47+
rememberKey: null,
48+
data: args[1] as TForm | (() => TForm),
49+
precognitionEndpoint: this.normalizeWayfinderArgsToCallback(args[0]),
50+
}
51+
}
52+
}
53+
54+
// Pattern 4: [method: Method | (() => Method), url: string | (() => string), data: TForm | (() => TForm)]
55+
return {
56+
rememberKey: null,
57+
data: args[2] as TForm | (() => TForm),
58+
precognitionEndpoint: this.normalizeWayfinderArgsToCallback(args[0], args[1]),
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)