Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
15cdc32
Vue integration
pascalbaljet Nov 4, 2025
350b6e4
Update Files.vue
pascalbaljet Nov 4, 2025
065ff96
Update Files.vue
pascalbaljet Nov 4, 2025
9d830e7
Update useForm.ts
pascalbaljet Nov 4, 2025
2f95dae
Update useForm.ts
pascalbaljet Nov 5, 2025
2d6d65f
Update useForm.ts
pascalbaljet Nov 5, 2025
e4605ef
Update useForm.ts
pascalbaljet Nov 5, 2025
3613db0
Update useForm.ts
pascalbaljet Nov 5, 2025
9817deb
Update useForm.ts
pascalbaljet Nov 5, 2025
dc9c36e
Update useForm.ts
pascalbaljet Nov 5, 2025
3541439
Update useForm.ts
pascalbaljet Nov 5, 2025
ea91780
Refactor
pascalbaljet Nov 5, 2025
b5f25eb
Refactor
pascalbaljet Nov 5, 2025
5adf9c9
Refactor
pascalbaljet Nov 5, 2025
ac38be7
Update useForm.ts
pascalbaljet Nov 5, 2025
bd8d77a
Revert "Update useForm.ts"
pascalbaljet Nov 5, 2025
49d4cc2
wip
pascalbaljet Nov 5, 2025
18e36d8
refactor
pascalbaljet Nov 5, 2025
569908b
Update useForm.ts
pascalbaljet Nov 5, 2025
e18a2fb
Update useForm.ts
pascalbaljet Nov 5, 2025
9bac610
wip
pascalbaljet Nov 5, 2025
a6c5c5c
wip
pascalbaljet Nov 6, 2025
212c510
wip
pascalbaljet Nov 6, 2025
7fe0e3c
Update useForm.ts
pascalbaljet Nov 6, 2025
0117544
Update useForm.ts
pascalbaljet Nov 6, 2025
5efe445
wip svelte
pascalbaljet Nov 6, 2025
d40a8ae
Update useForm.ts
pascalbaljet Nov 6, 2025
22a5c45
Update useForm.ts
pascalbaljet Nov 6, 2025
59a2987
wip
pascalbaljet Nov 6, 2025
056bd69
Update useForm.ts
pascalbaljet Nov 6, 2025
d576077
Update useForm.ts
pascalbaljet Nov 6, 2025
a65ec86
Update useForm.ts
pascalbaljet Nov 6, 2025
5a42e34
Update useForm.ts
pascalbaljet Nov 6, 2025
dfb3446
refactor
pascalbaljet Nov 6, 2025
a268c79
Update Instantiate.svelte
pascalbaljet Nov 6, 2025
07e35c1
Fix code style
pascalbaljet Nov 6, 2025
3e038e8
compatibility
pascalbaljet Nov 7, 2025
0ef0f89
svelte+vue
pascalbaljet Nov 7, 2025
fbc3673
eslint
pascalbaljet Nov 7, 2025
e52d95c
ts improvements
pascalbaljet Nov 7, 2025
86e4e47
ts
pascalbaljet Nov 7, 2025
03264ac
Update precognition.spec.ts
pascalbaljet Nov 7, 2025
6baf1db
Update server.js
pascalbaljet Nov 11, 2025
7289566
Merge branch 'master' into precognition-useform
pascalbaljet Nov 13, 2025
842278f
Update precognition.spec.ts
pascalbaljet Nov 13, 2025
30f01ab
Fix code style
pascalbaljet Nov 13, 2025
c8980fc
Renamed `withFullErrors` to `withArrayErrors`
pascalbaljet Nov 14, 2025
3364fcd
Merge branch 'master' into precognition-useform
pascalbaljet Nov 14, 2025
d377985
Renamed `withArrayErrors` to `withAllErrors`
pascalbaljet Nov 20, 2025
c3cce26
Update test
pascalbaljet Nov 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"@types/lodash-es": "^4.17.12",
"laravel-precognition": "^0.7.3",
"axios": "^1.13.2",
"lodash-es": "^4.17.21",
"qs": "^6.14.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Config } from './config'
import { Router } from './router'

export { UseFormUtils } from './useFormUtils'

export { config } from './config'
export { getScrollableParent } from './domUtils'
export { objectToFormData } from './formData'
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,23 @@ export type ProgressSettings = {

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

export type UseFormTransformCallback<TForm> = (data: TForm) => object
export type UseFormWithPrecognitionArguments =
| [Method | (() => Method), string | (() => string)]
| [UrlMethodPair | (() => UrlMethodPair)]

type UseFormInertiaArguments<TForm> = [data: TForm | (() => TForm)] | [rememberKey: string, data: TForm | (() => TForm)]
type UseFormPrecognitionArguments<TForm> =
| [urlMethodPair: UrlMethodPair | (() => UrlMethodPair), data: TForm | (() => TForm)]
| [method: Method | (() => Method), url: string | (() => string), data: TForm | (() => TForm)]
export type UseFormArguments<TForm> = UseFormInertiaArguments<TForm> | UseFormPrecognitionArguments<TForm>

export type UseFormSubmitOptions = Omit<VisitOptions, 'data'>
export type UseFormSubmitArguments =
| [Method, string, UseFormSubmitOptions?]
| [UrlMethodPair, UseFormSubmitOptions?]
| [UseFormSubmitOptions?]

export type FormComponentOptions = Pick<
VisitOptions,
'preserveScroll' | 'preserveState' | 'preserveUrl' | 'replace' | 'only' | 'except' | 'reset'
Expand Down
116 changes: 116 additions & 0 deletions packages/core/src/useFormUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import {
FormDataType,
Method,
UrlMethodPair,
UseFormArguments,
UseFormSubmitArguments,
UseFormSubmitOptions,
} from './types'
import { isUrlMethodPair } from './url'

export class UseFormUtils {
/**
* Creates a callback that returns a UrlMethodPair.
*
* createWayfinderCallback(urlMethodPair)
* createWayfinderCallback(method, url)
* createWayfinderCallback(() => urlMethodPair)
* createWayfinderCallback(() => method, () => url)
*/
public static createWayfinderCallback(
...args: [UrlMethodPair | (() => UrlMethodPair)] | [Method | (() => Method), string | (() => string)]
): () => UrlMethodPair {
return () => {
if (args.length === 1) {
// Wayfinder object, return as-is or call function...
return isUrlMethodPair(args[0]) ? args[0] : args[0]()
}

// Separate method and url, reconstruct Wayfinder object...
return {
method: typeof args[0] === 'function' ? args[0]() : args[0],
url: typeof args[1] === 'function' ? args[1]() : args[1],
}
}
}

/**
* Parses all useForm() arguments into { rememberKey, data, precognitionEndpoint }.
*
* useForm(data)
* useForm(rememberKey, data)
* useForm(method, url, data)
* useForm(urlMethodPair, data)
*
*/
public static parseUseFormArguments<TForm extends FormDataType<TForm>>(
...args: UseFormArguments<TForm>
): {
rememberKey: string | null
data: TForm | (() => TForm)
precognitionEndpoint: (() => UrlMethodPair) | null
} {
if (args.length === 1) {
// Basic form: useForm(data)
return {
rememberKey: null,
data: args[0],
precognitionEndpoint: null,
}
}

if (args.length === 2) {
if (typeof args[0] === 'string') {
// Rememberable form: useForm(rememberKey, data)
return {
rememberKey: args[0],
data: args[1],
precognitionEndpoint: null,
}
}

// Form with Precognition + Wayfinder: useForm(wayfinder, data)
return {
rememberKey: null,
data: args[1],
precognitionEndpoint: this.createWayfinderCallback(args[0]),
}
}

// Form with Precognition: useForm(method, url, data)
return {
rememberKey: null,
data: args[2],
precognitionEndpoint: this.createWayfinderCallback(args[0], args[1]),
}
}

/**
* Parses all submission arguments into { method, url, options }.
* It uses the Precognition endpoint if no explicit method/url are provided.
*
* form.submit(method, url)
* form.submit(method, url, options)
* form.submit(urlMethodPair)
* form.submit(urlMethodPair, options)
* form.submit()
* form.submit(options)
*/
public static parseSubmitArguments(
args: UseFormSubmitArguments,
precognitionEndpoint: (() => UrlMethodPair) | null,
): { method: Method; url: string; options: UseFormSubmitOptions } {
if (args.length === 3 || (args.length === 2 && typeof args[0] === 'string')) {
// Explicit method and url provided...
return { method: args[0], url: args[1], options: args[2] ?? {} }
}

if (isUrlMethodPair(args[0])) {
// Wayfinder object provided...
return { ...args[0], options: (args[1] as UseFormSubmitOptions) ?? {} }
}

// Use Precognition endpoint with optional options...
return { ...precognitionEndpoint!(), options: (args[0] as UseFormSubmitOptions) ?? {} }
}
}
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"dependencies": {
"@inertiajs/core": "workspace:*",
"@types/lodash-es": "^4.17.12",
"laravel-precognition": "^0.7.3",
"lodash-es": "^4.17.21"
}
}
Loading