From d9d0454f884d07055b2b30038952509691f0bccb Mon Sep 17 00:00:00 2001 From: Edgars Vasiljevs Date: Thu, 13 Jul 2023 15:40:48 +0300 Subject: [PATCH 1/4] fixes for svelte's useForm helper --- packages/svelte/package.json | 1 + packages/svelte/src/useForm.js | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/svelte/package.json b/packages/svelte/package.json index f2525218c..74e020c66 100755 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -29,6 +29,7 @@ }, "dependencies": { "@inertiajs/core": "1.0.9", + "lodash.clonedeep": "^4.5.0", "lodash.isequal": "^4.5.0" } } diff --git a/packages/svelte/src/useForm.js b/packages/svelte/src/useForm.js index 958066f5a..31176b053 100644 --- a/packages/svelte/src/useForm.js +++ b/packages/svelte/src/useForm.js @@ -1,12 +1,13 @@ import { router } from '@inertiajs/core' import isEqual from 'lodash.isequal' +import cloneDeep from 'lodash.clonedeep' import { writable } from 'svelte/store' function useForm(...args) { 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) : null - let defaults = data + let defaults = cloneDeep(data); let cancelToken = null let recentlySuccessfulTimeoutId = null let transform = (data) => data @@ -22,7 +23,7 @@ function useForm(...args) { processing: false, setStore(key, value) { store.update((store) => { - return Object.assign({}, store, typeof key === 'string' ? { [key]: value } : key) + return Object.assign(store, typeof key === 'string' ? { [key]: value } : key) }) }, data() { @@ -38,12 +39,12 @@ function useForm(...args) { }, defaults(key, value) { if (typeof key === 'undefined') { - defaults = Object.assign(defaults, this.data()) + defaults = Object.assign(defaults, cloneDeep(this.data())) return this } - defaults = Object.assign(defaults, value ? { [key]: value } : key) + defaults = Object.assign(defaults, cloneDeep(value ? { [key]: value } : key)) return this }, From 3df43e68d2edda38dd76d80352e762e515ab3900 Mon Sep 17 00:00:00 2001 From: Edgars Vasiljevs Date: Fri, 14 Jul 2023 00:21:16 +0300 Subject: [PATCH 2/4] fix useForm reset function --- packages/svelte/src/useForm.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/svelte/src/useForm.js b/packages/svelte/src/useForm.js index 31176b053..d624f65d6 100644 --- a/packages/svelte/src/useForm.js +++ b/packages/svelte/src/useForm.js @@ -7,7 +7,7 @@ function useForm(...args) { 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) : null - let defaults = cloneDeep(data); + let defaults = cloneDeep(data) let cancelToken = null let recentlySuccessfulTimeoutId = null let transform = (data) => data @@ -49,14 +49,15 @@ function useForm(...args) { return this }, reset(...fields) { + let clonedDefaults = cloneDeep(defaults) if (fields.length === 0) { - this.setStore(defaults) + this.setStore(clonedDefaults) } else { this.setStore( - Object.keys(defaults) + Object.keys(clonedDefaults) .filter((key) => fields.includes(key)) .reduce((carry, key) => { - carry[key] = defaults[key] + carry[key] = clonedDefaults[key] return carry }, {}), ) From 7190e0dbf7853ce1c6fc7d0ddb6c575a2567a52c Mon Sep 17 00:00:00 2001 From: Edgars Vasiljevs Date: Fri, 14 Jul 2023 00:22:51 +0300 Subject: [PATCH 3/4] make data default after successful submit request --- packages/svelte/src/useForm.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/svelte/src/useForm.js b/packages/svelte/src/useForm.js index d624f65d6..e99698208 100644 --- a/packages/svelte/src/useForm.js +++ b/packages/svelte/src/useForm.js @@ -125,6 +125,7 @@ function useForm(...args) { this.setStore('processing', false) this.setStore('progress', null) this.clearErrors() + this.defaults() this.setStore('wasSuccessful', true) this.setStore('recentlySuccessful', true) recentlySuccessfulTimeoutId = setTimeout(() => this.setStore('recentlySuccessful', false), 2000) From ab6b5dc89d6c45543f1b275ab51078525d53eb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgars=20Vasi=C4=BCjevs?= Date: Mon, 24 Jul 2023 18:33:30 +0300 Subject: [PATCH 4/4] Update useForm.js --- packages/svelte/src/useForm.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/svelte/src/useForm.js b/packages/svelte/src/useForm.js index e99698208..d624f65d6 100644 --- a/packages/svelte/src/useForm.js +++ b/packages/svelte/src/useForm.js @@ -125,7 +125,6 @@ function useForm(...args) { this.setStore('processing', false) this.setStore('progress', null) this.clearErrors() - this.defaults() this.setStore('wasSuccessful', true) this.setStore('recentlySuccessful', true) recentlySuccessfulTimeoutId = setTimeout(() => this.setStore('recentlySuccessful', false), 2000)