Skip to content

Commit

Permalink
React: Fix useForm type overload
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodekker committed Dec 22, 2021
1 parent bdcb1d1 commit b98ee69
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/inertia-react/index.d.ts
Expand Up @@ -97,7 +97,8 @@ export interface InertiaFormProps<TForm = Record<string, any>> {
delete: (url: string, options?: Inertia.VisitOptions) => void
}

export function useForm<TForm = Record<string, any>>(rememberKeyOrInitialValues: string | TForm, initialValues?: TForm): InertiaFormProps<TForm>;
export function useForm<TForm = Record<string, any>>(initialValues?: TForm): InertiaFormProps<TForm>;
export function useForm<TForm = Record<string, any>>(rememberKey: string, initialValues?: TForm): InertiaFormProps<TForm>;

export type SetupOptions<ElementType, SharedProps> = {
el: ElementType,
Expand Down
11 changes: 5 additions & 6 deletions packages/inertia-react/src/useForm.js
Expand Up @@ -3,15 +3,14 @@ import useRemember from './useRemember'
import { Inertia } from '@inertiajs/inertia'
import { useCallback, useEffect, useRef, useState } from 'react'

export default function useForm(rememberKeyOrInitialData, initialData) {

export default function useForm(...args) {
const isMounted = useRef(null)
const useRememberKey = typeof rememberKey === 'string' ? rememberKeyOrInitialData : null
const defaults = useRememberKey === null ? rememberKeyOrInitialData: initialData
const rememberKey = typeof args[0] === 'string' ? args[0] : null
const defaults = (typeof args[0] === 'string' ? args[1] : args[0]) || {}
const cancelToken = useRef(null)
const recentlySuccessfulTimeoutId = useRef(null)
const [data, setData] = useRememberKey ? useRemember(defaults, `${useRememberKey}:data`) : useState(defaults)
const [errors, setErrors] = useRememberKey ? useRemember({}, `${useRememberKey}:errors`) : useState({})
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : useState(defaults)
const [errors, setErrors] = rememberKey ? useRemember({}, `${rememberKey}:errors`) : useState({})
const [hasErrors, setHasErrors] = useState(false)
const [processing, setProcessing] = useState(false)
const [progress, setProgress] = useState(null)
Expand Down

0 comments on commit b98ee69

Please sign in to comment.