Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript return type for React Inertia useForm() #110

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-inertia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Config, NamedInputEvent, RequestMethod, SimpleValidationErrors, toSimpl
import { useForm as usePrecognitiveForm, client } from 'laravel-precognition-react'
import { useForm as useInertiaForm } from '@inertiajs/react'
import { useRef } from 'react'
import { Form } from './types'

export { client }

export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data, config: ValidationConfig = {}): any => {
export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data, config: ValidationConfig = {}): Form<Data> => {
const booted = useRef<boolean>(false)

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/react-inertia/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NamedInputEvent, RequestMethod, SimpleValidationErrors, ValidationConfig, ValidationErrors } from 'laravel-precognition'
import { Form as PrecognitiveForm } from 'laravel-precognition-react/dist/types'
import { InertiaFormProps } from '@inertiajs/react'
import { VisitOptions } from '@inertiajs/core'

type RedefinedProperties = 'setErrors' | 'touch' | 'forgetError' | 'setValidationTimeout' | 'submit' | 'reset' | 'validateFiles' | 'setData' | 'validate'

export type Form<Data extends Record<string, unknown>> = Omit<PrecognitiveForm<Data>, RedefinedProperties> & InertiaFormProps<Data> & {
setErrors(errors: SimpleValidationErrors | ValidationErrors): Data & Form<Data>,
touch(name: Array<string> | string | NamedInputEvent): Data & Form<Data>,
forgetError(string: keyof Data | NamedInputEvent): Data & Form<Data>,
setValidationTimeout(duration: number): Data & Form<Data>,
submit(config?: Partial<VisitOptions>): void,
submit(method: RequestMethod, url: string, options?: Partial<VisitOptions>): void,
reset(...keys: (keyof Partial<Data>)[]): Data & Form<Data>,
validateFiles(): Data & Form<Data>,
setData(data: Record<string, unknown>): Data & Form<Data>,
validate(name?: (keyof Data | NamedInputEvent) | ValidationConfig, config?: ValidationConfig): Data & Form<Data>,
}
Loading