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