From 3dd626bf28a784e1a56e4082d29e5a81de00ef91 Mon Sep 17 00:00:00 2001 From: Kyle Filz <590498+Spice-King@users.noreply.github.com> Date: Fri, 2 May 2025 15:53:42 -0500 Subject: [PATCH 1/5] Unify useForm's generic type across adaptors to new type in core --- packages/core/src/types.ts | 19 ++++++++++++------- packages/react/src/useForm.ts | 12 ++++++------ packages/svelte/src/useForm.ts | 18 ++++++++++-------- packages/vue3/src/useForm.ts | 13 ++++++------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index a301f06a6..8dd80e1e1 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -10,16 +10,21 @@ declare module 'axios' { export type Errors = Record export type ErrorBag = Record +export type FormDataConvertibleValue = Blob | FormDataEntryValue | Date | boolean | number | null | undefined export type FormDataConvertible = | Array | { [key: string]: FormDataConvertible } - | Blob - | FormDataEntryValue - | Date - | boolean - | number - | null - | undefined + | FormDataConvertibleValue + +export type FormDataType = { + [K in keyof T]: T[K] extends FormDataConvertibleValue + ? T[K] + : T[K] extends (...args: unknown[]) => unknown + ? never + : T[K] extends object | Array + ? FormDataType + : never +} export type FormDataKeys> = T extends T ? keyof T extends infer Key extends Extract diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index df60ec7f4..01bc317d7 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -1,6 +1,7 @@ import { FormDataConvertible, FormDataKeys, + FormDataType, FormDataValues, Method, Progress, @@ -14,11 +15,10 @@ import useRemember from './useRemember' type SetDataByObject = (data: TForm) => void type SetDataByMethod = (data: (previousData: TForm) => TForm) => void -type SetDataByKeyValuePair> = >(key: K, value: FormDataValues) => void -type FormDataType = Record +type SetDataByKeyValuePair = >(key: K, value: FormDataValues) => void type FormOptions = Omit -export interface InertiaFormProps { +export interface InertiaFormProps> { data: TForm isDirty: boolean errors: Partial, string>> @@ -44,12 +44,12 @@ export interface InertiaFormProps { delete: (url: string, options?: FormOptions) => void cancel: () => void } -export default function useForm(initialValues?: TForm): InertiaFormProps -export default function useForm( +export default function useForm>(initialValues?: TForm): InertiaFormProps +export default function useForm>( rememberKey: string, initialValues?: TForm, ): InertiaFormProps -export default function useForm( +export default function useForm>( rememberKeyOrInitialValues?: string | TForm, maybeInitialValues?: TForm, ): InertiaFormProps { diff --git a/packages/svelte/src/useForm.ts b/packages/svelte/src/useForm.ts index 86ac78c5a..ae31fc744 100644 --- a/packages/svelte/src/useForm.ts +++ b/packages/svelte/src/useForm.ts @@ -3,6 +3,7 @@ import type { Errors, FormDataConvertible, FormDataKeys, + FormDataType, Method, Page, PendingVisit, @@ -16,10 +17,9 @@ import { cloneDeep, isEqual } from 'es-toolkit' import { get, has, set } from 'es-toolkit/compat' import { writable, type Writable } from 'svelte/store' -type FormDataType = Record type FormOptions = Omit -export interface InertiaFormProps { +export interface InertiaFormProps> { isDirty: boolean errors: Partial, string>> hasErrors: boolean @@ -47,14 +47,16 @@ export interface InertiaFormProps { cancel(): void } -export type InertiaForm = InertiaFormProps & TForm +export type InertiaForm> = InertiaFormProps & TForm -export default function useForm(data: TForm | (() => TForm)): Writable> -export default function useForm( +export default function useForm>( + data: TForm | (() => TForm), +): Writable> +export default function useForm>( rememberKey: string, data: TForm | (() => TForm), ): Writable> -export default function useForm( +export default function useForm>( rememberKeyOrData: string | TForm | (() => TForm), maybeData?: TForm | (() => TForm), ): Writable> { @@ -86,7 +88,7 @@ export default function useForm( data() { return Object.keys(data).reduce((carry, key) => { return set(carry, key, get(this, key)) - }, {} as FormDataType) as TForm + }, {} as TForm) }, transform(callback) { transform = callback @@ -114,7 +116,7 @@ export default function useForm( .filter((key) => has(clonedData, key)) .reduce((carry, key) => { return set(carry, key, get(clonedData, key)) - }, {} as FormDataType) as TForm, + }, {} as TForm), ) } diff --git a/packages/vue3/src/useForm.ts b/packages/vue3/src/useForm.ts index 6b4f56929..46fb5ea59 100644 --- a/packages/vue3/src/useForm.ts +++ b/packages/vue3/src/useForm.ts @@ -1,12 +1,11 @@ -import { FormDataConvertible, FormDataKeys, Method, Progress, router, VisitOptions } from '@inertiajs/core' +import { FormDataKeys, FormDataType, Method, Progress, router, VisitOptions } from '@inertiajs/core' import { cloneDeep, isEqual } from 'es-toolkit' import { get, has, set } from 'es-toolkit/compat' import { reactive, watch } from 'vue' -type FormDataType = Record type FormOptions = Omit -export interface InertiaFormProps { +export interface InertiaFormProps> { isDirty: boolean errors: Partial, string>> hasErrors: boolean @@ -32,14 +31,14 @@ export interface InertiaFormProps { cancel(): void } -export type InertiaForm = TForm & InertiaFormProps +export type InertiaForm> = TForm & InertiaFormProps -export default function useForm(data: TForm | (() => TForm)): InertiaForm -export default function useForm( +export default function useForm>(data: TForm | (() => TForm)): InertiaForm +export default function useForm>( rememberKey: string, data: TForm | (() => TForm), ): InertiaForm -export default function useForm( +export default function useForm>( rememberKeyOrData: string | TForm | (() => TForm), maybeData?: TForm | (() => TForm), ): InertiaForm { From 3cc76deafe3853b76e3fba91757ea2fa564e0157 Mon Sep 17 00:00:00 2001 From: Kyle Filz <590498+Spice-King@users.noreply.github.com> Date: Fri, 2 May 2025 15:53:42 -0500 Subject: [PATCH 2/5] Update FormDataValues & FormDataKeys, make sure adaptors use it --- packages/core/src/types.ts | 44 ++++++++++++++++++++++------------ packages/react/src/useForm.ts | 5 ++-- packages/svelte/src/useForm.ts | 10 ++++---- packages/vue3/src/useForm.ts | 6 ++--- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 8dd80e1e1..1a46a359b 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -26,25 +26,39 @@ export type FormDataType = { : never } -export type FormDataKeys> = T extends T - ? keyof T extends infer Key extends Extract - ? Key extends Key - ? T[Key] extends Record - ? `${Key}.${FormDataKeys}` | Key - : Key +export type FormDataKeys = T extends Function | FormDataConvertibleValue + ? never + : T extends Array + ? number extends T['length'] + ? `${number}` | `${number}.${FormDataKeys}` + : + | Extract + | { + [Key in Extract]: `${Key & string}.${FormDataKeys & string}` + }[Extract] + : + | Extract + | { + [Key in Extract]: `${Key}.${FormDataKeys & string}` + }[Extract] + +export type FormDataValues> = K extends `${infer P}.${infer Rest}` + ? T extends unknown[] + ? P extends `${infer I extends number}` + ? Rest extends FormDataKeys + ? FormDataValues + : never : never - : never - : never - -export type FormDataValues, K extends FormDataKeys> = K extends `${infer P}.${infer Rest}` - ? P extends keyof T - ? Rest extends FormDataKeys - ? FormDataValues + : P extends keyof T + ? Rest extends FormDataKeys + ? FormDataValues + : never : never - : never : K extends keyof T ? T[K] - : never + : T extends unknown[] + ? T[K & number] + : never export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index 01bc317d7..5105763fc 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -1,5 +1,4 @@ import { - FormDataConvertible, FormDataKeys, FormDataType, FormDataValues, @@ -30,7 +29,7 @@ export interface InertiaFormProps> { setData: SetDataByObject & SetDataByMethod & SetDataByKeyValuePair transform: (callback: (data: TForm) => object) => void setDefaults(): void - setDefaults(field: FormDataKeys, value: FormDataConvertible): void + setDefaults>(field: T, value: FormDataValues): void setDefaults(fields: Partial): void reset: (...fields: FormDataKeys[]) => void clearErrors: (...fields: FormDataKeys[]) => void @@ -197,7 +196,7 @@ export default function useForm>( ) const setDefaultsFunction = useCallback( - (fieldOrFields?: FormDataKeys | Partial, maybeValue?: FormDataConvertible) => { + (fieldOrFields?: FormDataKeys | Partial, maybeValue?: unknown) => { if (typeof fieldOrFields === 'undefined') { setDefaults(() => data) } else { diff --git a/packages/svelte/src/useForm.ts b/packages/svelte/src/useForm.ts index ae31fc744..9466344b5 100644 --- a/packages/svelte/src/useForm.ts +++ b/packages/svelte/src/useForm.ts @@ -1,9 +1,9 @@ import type { ActiveVisit, Errors, - FormDataConvertible, FormDataKeys, FormDataType, + FormDataValues, Method, Page, PendingVisit, @@ -28,12 +28,12 @@ export interface InertiaFormProps> { recentlySuccessful: boolean processing: boolean setStore(data: TForm): void - setStore(key: FormDataKeys, value?: FormDataConvertible): void + setStore>(key: T, value: FormDataValues): void data(): TForm transform(callback: (data: TForm) => object): this defaults(): this defaults(fields: Partial): this - defaults(field?: FormDataKeys, value?: FormDataConvertible): this + defaults>(field: T, value: FormDataValues): this reset(...fields: FormDataKeys[]): this clearErrors(...fields: FormDataKeys[]): this setError(field: FormDataKeys, value: string): this @@ -80,7 +80,7 @@ export default function useForm>( wasSuccessful: false, recentlySuccessful: false, processing: false, - setStore(keyOrData, maybeValue = undefined) { + setStore(keyOrData: keyof InertiaFormProps | FormDataKeys | TForm, maybeValue = undefined) { store.update((store) => { return typeof keyOrData === 'string' ? set(store, keyOrData, maybeValue) : Object.assign(store, keyOrData) }) @@ -94,7 +94,7 @@ export default function useForm>( transform = callback return this }, - defaults(fieldOrFields?: FormDataKeys | Partial, maybeValue?: FormDataConvertible) { + defaults(fieldOrFields?: FormDataKeys | Partial, maybeValue?: unknown) { if (typeof fieldOrFields === 'undefined') { defaults = cloneDeep(this.data()) } else { diff --git a/packages/vue3/src/useForm.ts b/packages/vue3/src/useForm.ts index 46fb5ea59..11c2883f0 100644 --- a/packages/vue3/src/useForm.ts +++ b/packages/vue3/src/useForm.ts @@ -1,4 +1,4 @@ -import { FormDataKeys, FormDataType, Method, Progress, router, VisitOptions } from '@inertiajs/core' +import { FormDataKeys, FormDataType, FormDataValues, Method, Progress, router, VisitOptions } from '@inertiajs/core' import { cloneDeep, isEqual } from 'es-toolkit' import { get, has, set } from 'es-toolkit/compat' import { reactive, watch } from 'vue' @@ -16,7 +16,7 @@ export interface InertiaFormProps> { data(): TForm transform(callback: (data: TForm) => object): this defaults(): this - defaults(field: FormDataKeys, value: FormDataConvertible): this + defaults>(field: T, value: FormDataValues): this defaults(fields: Partial): this reset(...fields: FormDataKeys[]): this clearErrors(...fields: FormDataKeys[]): this @@ -71,7 +71,7 @@ export default function useForm>( return this }, - defaults(fieldOrFields?: FormDataKeys | Partial, maybeValue?: FormDataConvertible) { + defaults(fieldOrFields?: FormDataKeys | Partial, maybeValue?: unknown) { if (typeof data === 'function') { throw new Error('You cannot call `defaults()` when using a function to define your form data.') } From 0300e75120894e40fec7851ec426b578d2d8e08a Mon Sep 17 00:00:00 2001 From: Kyle Filz <590498+Spice-King@users.noreply.github.com> Date: Fri, 2 May 2025 15:53:42 -0500 Subject: [PATCH 3/5] Create error type for forms, apply to adaptors --- packages/core/src/types.ts | 2 ++ packages/react/src/useForm.ts | 15 +++++++-------- packages/svelte/src/useForm.ts | 7 ++++--- packages/vue3/src/useForm.ts | 15 ++++++++++++--- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 1a46a359b..21dd66ff1 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -60,6 +60,8 @@ export type FormDataValues> = K extends `${infer P} ? T[K & number] : never +export type FormDataError = Partial, string>> + export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' export type RequestPayload = Record | FormData diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index 5105763fc..ec8e5cead 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -1,4 +1,5 @@ import { + FormDataError, FormDataKeys, FormDataType, FormDataValues, @@ -20,7 +21,7 @@ type FormOptions = Omit export interface InertiaFormProps> { data: TForm isDirty: boolean - errors: Partial, string>> + errors: FormDataError hasErrors: boolean processing: boolean progress: Progress | null @@ -34,7 +35,7 @@ export interface InertiaFormProps> { reset: (...fields: FormDataKeys[]) => void clearErrors: (...fields: FormDataKeys[]) => void setError(field: FormDataKeys, value: string): void - setError(errors: Record, string>): void + setError(errors: FormDataError): void submit: (...args: [Method, string, FormOptions?] | [{ url: string; method: Method }, FormOptions?]) => void get: (url: string, options?: FormOptions) => void patch: (url: string, options?: FormOptions) => void @@ -61,8 +62,8 @@ export default function useForm>( const recentlySuccessfulTimeoutId = useRef(null) const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : useState(defaults) const [errors, setErrors] = rememberKey - ? useRemember({} as Partial, string>>, `${rememberKey}:errors`) - : useState({} as Partial, string>>) + ? useRemember({} as FormDataError, `${rememberKey}:errors`) + : useState({} as FormDataError) const [hasErrors, setHasErrors] = useState(false) const [processing, setProcessing] = useState(false) const [progress, setProgress] = useState(null) @@ -231,13 +232,11 @@ export default function useForm>( ) const setError = useCallback( - (fieldOrFields: FormDataKeys | Record, string>, maybeValue?: string) => { + (fieldOrFields: FormDataKeys | FormDataError, maybeValue?: string) => { setErrors((errors) => { const newErrors = { ...errors, - ...(typeof fieldOrFields === 'string' - ? { [fieldOrFields]: maybeValue } - : (fieldOrFields as Record, string>)), + ...(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields), } setHasErrors(Object.keys(newErrors).length > 0) return newErrors diff --git a/packages/svelte/src/useForm.ts b/packages/svelte/src/useForm.ts index 9466344b5..c13365225 100644 --- a/packages/svelte/src/useForm.ts +++ b/packages/svelte/src/useForm.ts @@ -1,6 +1,7 @@ import type { ActiveVisit, Errors, + FormDataError, FormDataKeys, FormDataType, FormDataValues, @@ -21,7 +22,7 @@ type FormOptions = Omit export interface InertiaFormProps> { isDirty: boolean - errors: Partial, string>> + errors: FormDataError hasErrors: boolean progress: Progress | null wasSuccessful: boolean @@ -37,7 +38,7 @@ export interface InertiaFormProps> { reset(...fields: FormDataKeys[]): this clearErrors(...fields: FormDataKeys[]): this setError(field: FormDataKeys, value: string): this - setError(errors: Errors): this + setError(errors: FormDataError): this submit: (...args: [Method, string, FormOptions?] | [{ url: string; method: Method }, FormOptions?]) => void get(url: string, options?: FormOptions): void post(url: string, options?: FormOptions): void @@ -122,7 +123,7 @@ export default function useForm>( return this }, - setError(fieldOrFields: FormDataKeys | Errors, maybeValue?: string) { + setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: string) { this.setStore('errors', { ...this.errors, ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Errors), diff --git a/packages/vue3/src/useForm.ts b/packages/vue3/src/useForm.ts index 11c2883f0..65dedaac4 100644 --- a/packages/vue3/src/useForm.ts +++ b/packages/vue3/src/useForm.ts @@ -1,4 +1,13 @@ -import { FormDataKeys, FormDataType, FormDataValues, Method, Progress, router, VisitOptions } from '@inertiajs/core' +import { + FormDataError, + FormDataKeys, + FormDataType, + FormDataValues, + Method, + Progress, + router, + VisitOptions, +} from '@inertiajs/core' import { cloneDeep, isEqual } from 'es-toolkit' import { get, has, set } from 'es-toolkit/compat' import { reactive, watch } from 'vue' @@ -7,7 +16,7 @@ type FormOptions = Omit export interface InertiaFormProps> { isDirty: boolean - errors: Partial, string>> + errors: FormDataError hasErrors: boolean processing: boolean progress: Progress | null @@ -105,7 +114,7 @@ export default function useForm>( return this }, - setError(fieldOrFields: FormDataKeys | Record, string>, maybeValue?: string) { + setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: string) { Object.assign(this.errors, typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) this.hasErrors = Object.keys(this.errors).length > 0 From 42fbe8c1d0527f39eb5bcb3728dd06930de8ae70 Mon Sep 17 00:00:00 2001 From: Kyle Filz <590498+Spice-King@users.noreply.github.com> Date: Tue, 6 May 2025 15:35:13 -0500 Subject: [PATCH 4/5] Add ability to globally override error type for Pages and Forms This will allow backends to send an array of strings and have the front end have correct types. --- packages/core/src/types.ts | 27 +++++++++++++++++++++++++-- packages/react/src/useForm.ts | 3 ++- packages/svelte/src/useForm.ts | 5 +++-- packages/vue3/src/useForm.ts | 7 ++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 21dd66ff1..741cdcc2a 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -7,7 +7,30 @@ declare module 'axios' { } } -export type Errors = Record +export type DefaultInertiaConfig = { + errorValueType: string +} +/** + * Designed to allow overriding of some core types using TypeScript + * interface declaration merging. + * + * @see {@link DefaultInertiaConfig} for keys to override + * @example + * ```ts + * declare module '@inertiajs/core' { + * export interface InertiaConfig { + * errorValueType: string[] + * } + * } + * ``` + */ +export interface InertiaConfig {} +export type InertiaConfigFor = Key extends keyof InertiaConfig + ? InertiaConfig[Key] + : DefaultInertiaConfig[Key] +export type ErrorValue = InertiaConfigFor<'errorValueType'> + +export type Errors = Record export type ErrorBag = Record export type FormDataConvertibleValue = Blob | FormDataEntryValue | Date | boolean | number | null | undefined @@ -60,7 +83,7 @@ export type FormDataValues> = K extends `${infer P} ? T[K & number] : never -export type FormDataError = Partial, string>> +export type FormDataError = Partial, ErrorValue>> export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index ec8e5cead..250b2a71a 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -1,4 +1,5 @@ import { + ErrorValue, FormDataError, FormDataKeys, FormDataType, @@ -34,7 +35,7 @@ export interface InertiaFormProps> { setDefaults(fields: Partial): void reset: (...fields: FormDataKeys[]) => void clearErrors: (...fields: FormDataKeys[]) => void - setError(field: FormDataKeys, value: string): void + setError(field: FormDataKeys, value: ErrorValue): void setError(errors: FormDataError): void submit: (...args: [Method, string, FormOptions?] | [{ url: string; method: Method }, FormOptions?]) => void get: (url: string, options?: FormOptions) => void diff --git a/packages/svelte/src/useForm.ts b/packages/svelte/src/useForm.ts index c13365225..a70735c77 100644 --- a/packages/svelte/src/useForm.ts +++ b/packages/svelte/src/useForm.ts @@ -1,6 +1,7 @@ import type { ActiveVisit, Errors, + ErrorValue, FormDataError, FormDataKeys, FormDataType, @@ -37,7 +38,7 @@ export interface InertiaFormProps> { defaults>(field: T, value: FormDataValues): this reset(...fields: FormDataKeys[]): this clearErrors(...fields: FormDataKeys[]): this - setError(field: FormDataKeys, value: string): this + setError(field: FormDataKeys, value: ErrorValue): this setError(errors: FormDataError): this submit: (...args: [Method, string, FormOptions?] | [{ url: string; method: Method }, FormOptions?]) => void get(url: string, options?: FormOptions): void @@ -123,7 +124,7 @@ export default function useForm>( return this }, - setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: string) { + setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: ErrorValue) { this.setStore('errors', { ...this.errors, ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Errors), diff --git a/packages/vue3/src/useForm.ts b/packages/vue3/src/useForm.ts index 65dedaac4..1c2844361 100644 --- a/packages/vue3/src/useForm.ts +++ b/packages/vue3/src/useForm.ts @@ -1,4 +1,5 @@ import { + ErrorValue, FormDataError, FormDataKeys, FormDataType, @@ -29,8 +30,8 @@ export interface InertiaFormProps> { defaults(fields: Partial): this reset(...fields: FormDataKeys[]): this clearErrors(...fields: FormDataKeys[]): this - setError(field: FormDataKeys, value: string): this - setError(errors: Record, string>): this + setError(field: FormDataKeys, value: ErrorValue): this + setError(errors: Record, ErrorValue>): this submit: (...args: [Method, string, FormOptions?] | [{ url: string; method: Method }, FormOptions?]) => void get(url: string, options?: FormOptions): void post(url: string, options?: FormOptions): void @@ -114,7 +115,7 @@ export default function useForm>( return this }, - setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: string) { + setError(fieldOrFields: FormDataKeys | FormDataError, maybeValue?: ErrorValue) { Object.assign(this.errors, typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) this.hasErrors = Object.keys(this.errors).length > 0 From 70d34ad8d41e57eb08cc12da2fdf2962a89cbac4 Mon Sep 17 00:00:00 2001 From: Kyle Filz <590498+Spice-King@users.noreply.github.com> Date: Thu, 17 Jul 2025 12:34:56 -0500 Subject: [PATCH 5/5] Fix React useForm to explicitly accept a function for initial value --- packages/react/src/useForm.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/react/src/useForm.ts b/packages/react/src/useForm.ts index 80838cbb0..6f5d94b44 100644 --- a/packages/react/src/useForm.ts +++ b/packages/react/src/useForm.ts @@ -48,14 +48,16 @@ export interface InertiaFormProps> { delete: (url: string, options?: FormOptions) => void cancel: () => void } -export default function useForm>(initialValues?: TForm): InertiaFormProps +export default function useForm>( + initialValues?: TForm | (() => TForm), +): InertiaFormProps export default function useForm>( rememberKey: string, - initialValues?: TForm, + initialValues?: TForm | (() => TForm), ): InertiaFormProps export default function useForm>( - rememberKeyOrInitialValues?: string | TForm, - maybeInitialValues?: TForm, + rememberKeyOrInitialValues?: string | TForm | (() => TForm), + maybeInitialValues?: TForm | (() => TForm), ): InertiaFormProps { const isMounted = useRef(null) const rememberKey = typeof rememberKeyOrInitialValues === 'string' ? rememberKeyOrInitialValues : null