|
1 | 1 | import { FormDataConvertible, Method, Progress, router, VisitOptions } from '@inertiajs/core'
|
2 |
| -import cloneDeep from 'lodash.clonedeep' |
3 |
| -import isEqual from 'lodash.isequal' |
| 2 | +import { klona } from 'klona/full' |
| 3 | +import isDeepEqual from '@gilbarbara/deep-equal' |
4 | 4 | import { reactive, watch } from 'vue'
|
5 | 5 |
|
6 | 6 | type FormDataType = Record<string, FormDataConvertible>
|
@@ -48,13 +48,13 @@ export default function useForm<TForm extends FormDataType>(
|
48 | 48 | const restored = rememberKey
|
49 | 49 | ? (router.restore(rememberKey) as { data: TForm; errors: Record<keyof TForm, string> })
|
50 | 50 | : null
|
51 |
| - let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data) |
| 51 | + let defaults = typeof data === 'function' ? klona(data()) : klona(data) |
52 | 52 | let cancelToken = null
|
53 | 53 | let recentlySuccessfulTimeoutId = null
|
54 | 54 | let transform = (data) => data
|
55 | 55 |
|
56 | 56 | const form = reactive({
|
57 |
| - ...(restored ? restored.data : cloneDeep(defaults)), |
| 57 | + ...(restored ? restored.data : klona(defaults)), |
58 | 58 | isDirty: false,
|
59 | 59 | errors: restored ? restored.errors : {},
|
60 | 60 | hasErrors: false,
|
@@ -84,16 +84,16 @@ export default function useForm<TForm extends FormDataType>(
|
84 | 84 | } else {
|
85 | 85 | defaults = Object.assign(
|
86 | 86 | {},
|
87 |
| - cloneDeep(defaults), |
| 87 | + klona(defaults), |
88 | 88 | typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields,
|
89 | 89 | )
|
90 | 90 | }
|
91 | 91 |
|
92 | 92 | return this
|
93 | 93 | },
|
94 | 94 | reset(...fields) {
|
95 |
| - const resolvedData = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(defaults) |
96 |
| - const clonedData = cloneDeep(resolvedData) |
| 95 | + const resolvedData = typeof data === 'function' ? klona(data()) : klona(defaults) |
| 96 | + const clonedData = klona(resolvedData) |
97 | 97 | if (fields.length === 0) {
|
98 | 98 | defaults = clonedData
|
99 | 99 | Object.assign(this, resolvedData)
|
@@ -171,7 +171,7 @@ export default function useForm<TForm extends FormDataType>(
|
171 | 171 | recentlySuccessfulTimeoutId = setTimeout(() => (this.recentlySuccessful = false), 2000)
|
172 | 172 |
|
173 | 173 | const onSuccess = options.onSuccess ? await options.onSuccess(page) : null
|
174 |
| - defaults = cloneDeep(this.data()) |
| 174 | + defaults = klona(this.data()) |
175 | 175 | this.isDirty = false
|
176 | 176 | return onSuccess
|
177 | 177 | },
|
@@ -242,9 +242,9 @@ export default function useForm<TForm extends FormDataType>(
|
242 | 242 | watch(
|
243 | 243 | form,
|
244 | 244 | (newValue) => {
|
245 |
| - form.isDirty = !isEqual(form.data(), defaults) |
| 245 | + form.isDirty = !isDeepEqual(form.data(), defaults) |
246 | 246 | if (rememberKey) {
|
247 |
| - router.remember(cloneDeep(newValue.__remember()), rememberKey) |
| 247 | + router.remember(klona(newValue.__remember()), rememberKey) |
248 | 248 | }
|
249 | 249 | },
|
250 | 250 | { immediate: true, deep: true },
|
|
0 commit comments