Skip to content

Commit

Permalink
Update useForm to accept interfaces (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spice-King authored Aug 17, 2023
1 parent 42ecb10 commit 5415b92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
9 changes: 5 additions & 4 deletions packages/react/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ 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;

export interface InertiaFormProps<TForm extends Record<string, unknown>> {
export interface InertiaFormProps<TForm extends FormDataType> {
data: TForm
isDirty: boolean
errors: Partial<Record<keyof TForm, string>>
Expand All @@ -33,12 +34,12 @@ export interface InertiaFormProps<TForm extends Record<string, unknown>> {
delete: (url: string, options?: VisitOptions) => void
cancel: () => void
}
export default function useForm<TForm extends Record<string, unknown>>(initialValues?: TForm): InertiaFormProps<TForm>
export default function useForm<TForm extends Record<string, unknown>>(
export default function useForm<TForm extends FormDataType>(initialValues?: TForm): InertiaFormProps<TForm>
export default function useForm<TForm extends FormDataType>(
rememberKey: string,
initialValues?: TForm,
): InertiaFormProps<TForm>
export default function useForm<TForm extends Record<string, unknown>>(
export default function useForm<TForm extends FormDataType>(
rememberKeyOrInitialValues?: string | TForm,
maybeInitialValues?: TForm,
): InertiaFormProps<TForm> {
Expand Down
16 changes: 9 additions & 7 deletions packages/vue2/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import cloneDeep from 'lodash.clonedeep'
import isEqual from 'lodash.isequal'
import { reactive, watch } from 'vue'

interface InertiaFormProps<TForm> {
type FormDataType = object;

interface InertiaFormProps<TForm extends FormDataType> {
isDirty: boolean
errors: Record<keyof TForm, string>
hasErrors: boolean
Expand All @@ -29,16 +31,16 @@ interface InertiaFormProps<TForm> {
cancel(): void
}

export type InertiaForm<TForm> = TForm & InertiaFormProps<TForm>
export type InertiaForm<TForm extends FormDataType> = TForm & InertiaFormProps<TForm>

export interface InertiaFormTrait {
form<TForm>(data: TForm): InertiaForm<TForm>
form<TForm>(rememberKey: string, data: TForm): InertiaForm<TForm>
form<TForm extends FormDataType>(data: TForm): InertiaForm<TForm>
form<TForm extends FormDataType>(rememberKey: string, data: TForm): InertiaForm<TForm>
}

export default function useForm<TForm>(data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm>(rememberKey: string, data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm>(...args): InertiaForm<TForm> {
export default function useForm<TForm extends FormDataType>(data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm extends FormDataType>(rememberKey: string, data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm extends FormDataType>(...args): InertiaForm<TForm> {
const rememberKey = typeof args[0] === 'string' ? args[0] : null
const data = (typeof args[0] === 'string' ? args[1] : args[0]) || {}
const restored = rememberKey ? (router.restore(rememberKey) as { data: any; errors: any }) : null
Expand Down
12 changes: 7 additions & 5 deletions packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import cloneDeep from 'lodash.clonedeep'
import isEqual from 'lodash.isequal'
import { reactive, watch } from 'vue'

interface InertiaFormProps<TForm extends Record<string, unknown>> {
type FormDataType = object;

interface InertiaFormProps<TForm extends FormDataType> {
isDirty: boolean
errors: Partial<Record<keyof TForm, string>>
hasErrors: boolean
Expand All @@ -29,14 +31,14 @@ interface InertiaFormProps<TForm extends Record<string, unknown>> {
cancel(): void
}

export type InertiaForm<TForm extends Record<string, unknown>> = TForm & InertiaFormProps<TForm>
export type InertiaForm<TForm extends FormDataType> = TForm & InertiaFormProps<TForm>

export default function useForm<TForm extends Record<string, unknown>>(data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm extends Record<string, unknown>>(
export default function useForm<TForm extends FormDataType>(data: TForm | (() => TForm)): InertiaForm<TForm>
export default function useForm<TForm extends FormDataType>(
rememberKey: string,
data: TForm | (() => TForm),
): InertiaForm<TForm>
export default function useForm<TForm extends Record<string, unknown>>(
export default function useForm<TForm extends FormDataType>(
rememberKeyOrData: string | TForm | (() => TForm),
maybeData?: TForm | (() => TForm),
): InertiaForm<TForm> {
Expand Down

0 comments on commit 5415b92

Please sign in to comment.