Skip to content

Commit

Permalink
Merge pull request #2052 from saml-dev/useform
Browse files Browse the repository at this point in the history
use default empty object in useForm Vue and Svelte
  • Loading branch information
joetannenbaum authored Jan 9, 2025
2 parents ba57a60 + 27c2cb4 commit 59ee7aa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/svelte/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function useForm<TForm extends FormDataType>(
maybeData?: TForm | (() => TForm),
): Writable<InertiaForm<TForm>> {
const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null
const inputData = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData
const inputData = (typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData) ?? {}
const data: TForm = typeof inputData === 'function' ? inputData() : (inputData as TForm)
const restored = rememberKey
? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> } | null)
Expand Down
6 changes: 3 additions & 3 deletions packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export default function useForm<TForm extends FormDataType>(
maybeData?: TForm | (() => TForm),
): InertiaForm<TForm> {
const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null
const data = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData
const data = (typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData) ?? {}
const restored = rememberKey
? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> })
: null
let defaults = typeof data === 'object' ? cloneDeep(data) : cloneDeep(data())
let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
let cancelToken = null
let recentlySuccessfulTimeoutId = null
let transform = (data) => data
Expand Down Expand Up @@ -91,7 +91,7 @@ export default function useForm<TForm extends FormDataType>(
return this
},
reset(...fields) {
const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data())
const resolvedData = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data)
const clonedData = cloneDeep(resolvedData)
if (fields.length === 0) {
defaults = clonedData
Expand Down

0 comments on commit 59ee7aa

Please sign in to comment.