From 19c0efce733d0508cdab06423cb95cb6260aa7a8 Mon Sep 17 00:00:00 2001 From: Pedro Borges Date: Fri, 9 Aug 2024 21:41:33 -0300 Subject: [PATCH] Review useForm types --- packages/svelte/src/lib/useForm.ts | 46 +++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/packages/svelte/src/lib/useForm.ts b/packages/svelte/src/lib/useForm.ts index 82aa20aa1..853984b1b 100644 --- a/packages/svelte/src/lib/useForm.ts +++ b/packages/svelte/src/lib/useForm.ts @@ -57,11 +57,12 @@ export default function useForm( maybeData?: TForm | (() => TForm), ): Writable> { const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null - const data = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData + const inputData = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData + const data: TForm = typeof inputData === 'function' ? inputData() : inputData as TForm const restored = rememberKey - ? (router.restore(rememberKey) as { data: TForm; errors: Record }) + ? (router.restore(rememberKey) as { data: TForm; errors: Record } | null) : null - let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data) + let defaults = cloneDeep(data) let cancelToken: { cancel: () => void } | null = null let recentlySuccessfulTimeoutId: ReturnType | null = null let transform = (data: TForm) => data as object @@ -75,39 +76,33 @@ export default function useForm( wasSuccessful: false, recentlySuccessful: false, processing: false, - setStore(keyOrData, maybeData = undefined) { + setStore(keyOrData, maybeValue = undefined) { store.update((store) => { - return Object.assign(store, typeof keyOrData === 'string' ? { [keyOrData]: maybeData } : keyOrData) + return Object.assign(store, typeof keyOrData === 'string' ? { [keyOrData]: maybeValue } : keyOrData) }) }, data() { - return Object.keys((typeof data === 'function' ? data() : data) as FormDataType).reduce((carry, key) => { + return Object.keys(data).reduce((carry, key) => { carry[key] = this[key] return carry }, {} as FormDataType) as TForm }, transform(callback) { transform = callback - return this }, - defaults(fieldOrFields?: keyof TForm | Record, maybeValue?: string) { - if (typeof fieldOrFields === 'undefined') { - defaults = Object.assign(defaults, cloneDeep(this.data())) - - return this - } - - defaults = Object.assign( - cloneDeep(defaults), - cloneDeep(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields), - ) + defaults(fieldOrFields?: keyof TForm | Partial, maybeValue?: FormDataConvertible) { + defaults = typeof fieldOrFields === 'undefined' + ? cloneDeep(this.data()) + : Object.assign( + cloneDeep(defaults), + typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields, + ) return this }, reset(...fields) { - const resolvedData: TForm = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!()) - const clonedData = cloneDeep(resolvedData) as TForm + const clonedData = cloneDeep(defaults) if (fields.length === 0) { this.setStore(clonedData) } else { @@ -126,7 +121,7 @@ export default function useForm( setError(fieldOrFields: keyof TForm | Errors, maybeValue?: string) { this.setStore('errors', { ...this.errors, - ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as any), + ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Errors), }) return this @@ -140,12 +135,11 @@ export default function useForm( ...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}), }), {}, - ), + ) as Errors, ) - return this }, - submit(method, url, options = {}) { + submit(method, url, options: Partial = {}) { const data = transform(this.data()) as RequestPayload const _options: Omit = { ...options, @@ -262,5 +256,5 @@ export default function useForm( } }) - return store as Writable> -} + return store +} \ No newline at end of file