Skip to content

Commit

Permalink
Merge pull request #2060 from inertiajs/remove-data-option-from-useForm
Browse files Browse the repository at this point in the history
[2.x] Remove `data` option from `useForm` options type
  • Loading branch information
joetannenbaum authored Jan 9, 2025
2 parents e268902 + 8bc7fc4 commit ba57a60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
15 changes: 8 additions & 7 deletions packages/react/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import useRemember from './useRemember'
type setDataByObject<TForm> = (data: TForm) => void
type setDataByMethod<TForm> = (data: (previousData: TForm) => TForm) => void
type setDataByKeyValuePair<TForm> = <K extends keyof TForm>(key: K, value: TForm[K]) => void
type FormDataType = object
type FormDataType = Record<string, FormDataConvertible>
type FormOptions = Omit<VisitOptions, 'data'>

export interface InertiaFormProps<TForm extends FormDataType> {
data: TForm
Expand All @@ -26,12 +27,12 @@ export interface InertiaFormProps<TForm extends FormDataType> {
clearErrors: (...fields: (keyof TForm)[]) => void
setError(field: keyof TForm, value: string): void
setError(errors: Record<keyof TForm, string>): void
submit: (method: Method, url: string, options?: VisitOptions) => void
get: (url: string, options?: VisitOptions) => void
patch: (url: string, options?: VisitOptions) => void
post: (url: string, options?: VisitOptions) => void
put: (url: string, options?: VisitOptions) => void
delete: (url: string, options?: VisitOptions) => void
submit: (method: Method, url: string, options?: FormOptions) => void
get: (url: string, options?: FormOptions) => void
patch: (url: string, options?: FormOptions) => void
post: (url: string, options?: FormOptions) => void
put: (url: string, options?: FormOptions) => void
delete: (url: string, options?: FormOptions) => void
cancel: () => void
}
export default function useForm<TForm extends FormDataType>(initialValues?: TForm): InertiaFormProps<TForm>
Expand Down
15 changes: 8 additions & 7 deletions packages/svelte/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import isEqual from 'lodash/isEqual'
import { writable, type Writable } from 'svelte/store'

type FormDataType = Record<string, FormDataConvertible>
type FormOptions = Omit<VisitOptions, 'data'>

export interface InertiaFormProps<TForm extends FormDataType> {
isDirty: boolean
Expand All @@ -36,12 +37,12 @@ export interface InertiaFormProps<TForm extends FormDataType> {
clearErrors(...fields: (keyof TForm)[]): this
setError(field: keyof TForm, value: string): this
setError(errors: Errors): this
submit(method: Method, url: string, options?: Partial<VisitOptions>): void
get(url: string, options?: Partial<VisitOptions>): void
post(url: string, options?: Partial<VisitOptions>): void
put(url: string, options?: Partial<VisitOptions>): void
patch(url: string, options?: Partial<VisitOptions>): void
delete(url: string, options?: Partial<VisitOptions>): void
submit(method: Method, url: string, options?: FormOptions): void
get(url: string, options?: FormOptions): void
post(url: string, options?: FormOptions): void
put(url: string, options?: FormOptions): void
patch(url: string, options?: FormOptions): void
delete(url: string, options?: FormOptions): void
cancel(): void
}

Expand Down Expand Up @@ -140,7 +141,7 @@ export default function useForm<TForm extends FormDataType>(
)
return this
},
submit(method, url, options: Partial<VisitOptions> = {}) {
submit(method, url, options: FormOptions = {}) {
const data = transform(this.data()) as RequestPayload
const _options: Omit<VisitOptions, 'method'> = {
...options,
Expand Down
17 changes: 9 additions & 8 deletions packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import cloneDeep from 'lodash.clonedeep'
import isEqual from 'lodash.isequal'
import { reactive, watch } from 'vue'

type FormDataType = object
type FormDataType = Record<string, FormDataConvertible>
type FormOptions = Omit<VisitOptions, 'data'>

interface InertiaFormProps<TForm extends FormDataType> {
isDirty: boolean
Expand All @@ -22,12 +23,12 @@ interface InertiaFormProps<TForm extends FormDataType> {
clearErrors(...fields: (keyof TForm)[]): this
setError(field: keyof TForm, value: string): this
setError(errors: Record<keyof TForm, string>): this
submit(method: Method, url: string, options?: Partial<VisitOptions>): void
get(url: string, options?: Partial<VisitOptions>): void
post(url: string, options?: Partial<VisitOptions>): void
put(url: string, options?: Partial<VisitOptions>): void
patch(url: string, options?: Partial<VisitOptions>): void
delete(url: string, options?: Partial<VisitOptions>): void
submit(method: Method, url: string, options?: FormOptions): void
get(url: string, options?: FormOptions): void
post(url: string, options?: FormOptions): void
put(url: string, options?: FormOptions): void
patch(url: string, options?: FormOptions): void
delete(url: string, options?: FormOptions): void
cancel(): void
}

Expand Down Expand Up @@ -126,7 +127,7 @@ export default function useForm<TForm extends FormDataType>(

return this
},
submit(method, url, options: VisitOptions = {}) {
submit(method, url, options: FormOptions = {}) {
const data = transform(this.data())
const _options = {
...options,
Expand Down

0 comments on commit ba57a60

Please sign in to comment.