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..d624f65d6 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,24 +39,25 @@ 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 }, 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 }, {}), )