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

Support nested paths in form error types #2081

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 19 additions & 11 deletions packages/react/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import isEqual from 'lodash.isequal'
import { useCallback, useEffect, useRef, useState } from 'react'
import useRemember from './useRemember'

type FormDataKeys<T> = {
[K in keyof T & string]: T[K] extends object
? T[K] extends Array<any>
? K
: `${K}.${FormDataKeys<T[K]>}` | K
: K;
}[keyof T & string];

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
Expand All @@ -11,7 +19,7 @@ type FormDataType = object
export interface InertiaFormProps<TForm extends FormDataType> {
data: TForm
isDirty: boolean
errors: Partial<Record<keyof TForm, string>>
errors: Partial<Record<FormDataKeys<TForm>, string>>
hasErrors: boolean
processing: boolean
progress: Progress | null
Expand All @@ -23,9 +31,9 @@ export interface InertiaFormProps<TForm extends FormDataType> {
setDefaults(field: keyof TForm, value: FormDataConvertible): void
setDefaults(fields: Partial<TForm>): void
reset: (...fields: (keyof TForm)[]) => void
clearErrors: (...fields: (keyof TForm)[]) => void
setError(field: keyof TForm, value: string): void
setError(errors: Record<keyof TForm, string>): void
clearErrors: (...fields: FormDataKeys<TForm>[]) => void
setError(field: FormDataKeys<TForm>, value: string): void
setError(errors: Partial<Record<FormDataKeys<TForm>, string>>): void
submit: (method: Method, url: string, options?: VisitOptions) => void
get: (url: string, options?: VisitOptions) => void
patch: (url: string, options?: VisitOptions) => void
Expand All @@ -52,8 +60,8 @@ export default function useForm<TForm extends FormDataType>(
const recentlySuccessfulTimeoutId = useRef(null)
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : useState(defaults)
const [errors, setErrors] = rememberKey
? useRemember({} as Partial<Record<keyof TForm, string>>, `${rememberKey}:errors`)
: useState({} as Partial<Record<keyof TForm, string>>)
? useRemember({} as Partial<Record<FormDataKeys<TForm>, string>>, `${rememberKey}:errors`)
: useState({} as Partial<Record<FormDataKeys<TForm>, string>>)
const [hasErrors, setHasErrors] = useState(false)
const [processing, setProcessing] = useState(false)
const [progress, setProgress] = useState(null)
Expand Down Expand Up @@ -125,7 +133,7 @@ export default function useForm<TForm extends FormDataType>(
if (isMounted.current) {
setProcessing(false)
setProgress(null)
setErrors(errors)
setErrors(errors as Partial<Record<FormDataKeys<TForm>, string>>)
setHasErrors(true)
}

Expand Down Expand Up @@ -214,24 +222,24 @@ export default function useForm<TForm extends FormDataType>(
)
}
},
setError(fieldOrFields: keyof TForm | Record<keyof TForm, string>, maybeValue?: string) {
setError(fieldOrFields: FormDataKeys<TForm> | Partial<Record<FormDataKeys<TForm>, string>>, maybeValue?: string) {
setErrors((errors) => {
const newErrors = {
...errors,
...(typeof fieldOrFields === 'string'
? { [fieldOrFields]: maybeValue }
: (fieldOrFields as Record<keyof TForm, string>)),
: fieldOrFields),
}
setHasErrors(Object.keys(newErrors).length > 0)
return newErrors
})
},
clearErrors(...fields) {
setErrors((errors) => {
const newErrors = (Object.keys(errors) as Array<keyof TForm>).reduce(
const newErrors = Object.keys(errors).reduce(
(carry, field) => ({
...carry,
...(fields.length > 0 && !fields.includes(field) ? { [field]: errors[field] } : {}),
...(fields.length > 0 && !fields.includes(field as FormDataKeys<TForm>) ? { [field]: errors[field as FormDataKeys<TForm>] } : {}),
}),
{},
)
Expand Down
28 changes: 18 additions & 10 deletions packages/vue3/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import cloneDeep from 'lodash.clonedeep'
import isEqual from 'lodash.isequal'
import { reactive, watch } from 'vue'

type FormDataKeys<T> = {
[K in keyof T & string]: T[K] extends object
? T[K] extends Array<any>
? K
: `${K}.${FormDataKeys<T[K]>}` | K
: K;
}[keyof T & string];

type FormDataType = object

interface InertiaFormProps<TForm extends FormDataType> {
isDirty: boolean
errors: Partial<Record<keyof TForm, string>>
errors: Partial<Record<FormDataKeys<TForm>, string>>
hasErrors: boolean
processing: boolean
progress: Progress | null
Expand All @@ -19,9 +27,9 @@ interface InertiaFormProps<TForm extends FormDataType> {
defaults(field: keyof TForm, value: FormDataConvertible): this
defaults(fields: Partial<TForm>): this
reset(...fields: (keyof TForm)[]): this
clearErrors(...fields: (keyof TForm)[]): this
setError(field: keyof TForm, value: string): this
setError(errors: Record<keyof TForm, string>): this
clearErrors(...fields: FormDataKeys<TForm>[]): this
setError(field: FormDataKeys<TForm>, value: string): this
setError(errors: Partial<Record<FormDataKeys<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
Expand All @@ -45,7 +53,7 @@ export default function useForm<TForm extends FormDataType>(
const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null
const data = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData
const restored = rememberKey
? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> })
? (router.restore(rememberKey) as { data: TForm; errors: Partial<Record<FormDataKeys<TForm>, string>> })
: null
let defaults = typeof data === 'object' ? cloneDeep(data) : cloneDeep(data())
let cancelToken = null
Expand Down Expand Up @@ -106,18 +114,18 @@ export default function useForm<TForm extends FormDataType>(

return this
},
setError(fieldOrFields: keyof TForm | Record<keyof TForm, string>, maybeValue?: string) {
setError(fieldOrFields: FormDataKeys<TForm> | Partial<Record<FormDataKeys<TForm>, string>>, maybeValue?: string) {
Object.assign(this.errors, typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields)

this.hasErrors = Object.keys(this.errors).length > 0

return this
},
clearErrors(...fields) {
this.errors = Object.keys(this.errors).reduce(
clearErrors(...fields: FormDataKeys<TForm>[]) {
this.errors = Object.keys(this.errors as object).reduce(
(carry, field) => ({
...carry,
...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}),
...(fields.length > 0 && !fields.includes(field as FormDataKeys<TForm>) ? { [field]: this.errors[field as FormDataKeys<TForm>] } : {}),
}),
{},
)
Expand Down Expand Up @@ -176,7 +184,7 @@ export default function useForm<TForm extends FormDataType>(
onError: (errors) => {
this.processing = false
this.progress = null
this.clearErrors().setError(errors)
this.clearErrors().setError(errors as Partial<Record<FormDataKeys<TForm>, string>>)

if (options.onError) {
return options.onError(errors)
Expand Down
Loading