Skip to content

Commit 59ee7aa

Browse files
Merge pull request #2052 from saml-dev/useform
use default empty object in useForm Vue and Svelte
2 parents ba57a60 + 27c2cb4 commit 59ee7aa

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

packages/svelte/src/useForm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function useForm<TForm extends FormDataType>(
5858
maybeData?: TForm | (() => TForm),
5959
): Writable<InertiaForm<TForm>> {
6060
const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null
61-
const inputData = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData
61+
const inputData = (typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData) ?? {}
6262
const data: TForm = typeof inputData === 'function' ? inputData() : (inputData as TForm)
6363
const restored = rememberKey
6464
? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> } | null)

packages/vue3/src/useForm.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ export default function useForm<TForm extends FormDataType>(
4444
maybeData?: TForm | (() => TForm),
4545
): InertiaForm<TForm> {
4646
const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null
47-
const data = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData
47+
const data = (typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData) ?? {}
4848
const restored = rememberKey
4949
? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> })
5050
: null
51-
let defaults = typeof data === 'object' ? cloneDeep(data) : cloneDeep(data())
51+
let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
5252
let cancelToken = null
5353
let recentlySuccessfulTimeoutId = null
5454
let transform = (data) => data
@@ -91,7 +91,7 @@ export default function useForm<TForm extends FormDataType>(
9191
return this
9292
},
9393
reset(...fields) {
94-
const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data())
94+
const resolvedData = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
9595
const clonedData = cloneDeep(resolvedData)
9696
if (fields.length === 0) {
9797
defaults = clonedData

0 commit comments

Comments
 (0)