@teamnovu/kit-vue-forms 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +36 -31
- package/package.json +1 -1
- package/src/composables/useForm.ts +7 -2
- package/tests/integration.test.ts +1 -1
- package/tests/nestedPath.test.ts +3 -5
- package/tests/useForm.test.ts +25 -7
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
var x = Object.defineProperty;
|
|
2
2
|
var K = (e, r, t) => r in e ? x(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
|
|
3
3
|
var S = (e, r, t) => K(e, typeof r != "symbol" ? r + "" : r, t);
|
|
4
|
-
import { toValue as W, toRaw as I, computed as
|
|
4
|
+
import { toValue as W, toRaw as I, computed as f, unref as u, reactive as m, watch as F, toRefs as O, toRef as D, ref as A, isRef as _, getCurrentScope as J, onBeforeUnmount as T, defineComponent as B, renderSlot as G, normalizeProps as L, guardReactiveProps as U } from "vue";
|
|
5
5
|
import "zod";
|
|
6
|
+
import "@vueuse/core";
|
|
6
7
|
function y(e) {
|
|
7
8
|
const r = W(e), t = I(r);
|
|
8
9
|
return structuredClone(t);
|
|
@@ -27,7 +28,7 @@ function Z(e, r, t) {
|
|
|
27
28
|
);
|
|
28
29
|
n[a] = t;
|
|
29
30
|
}
|
|
30
|
-
const z = (e, r) =>
|
|
31
|
+
const z = (e, r) => f({
|
|
31
32
|
get() {
|
|
32
33
|
return w(u(e), u(r));
|
|
33
34
|
},
|
|
@@ -56,22 +57,22 @@ function q(e) {
|
|
|
56
57
|
const r = m({
|
|
57
58
|
value: e.value,
|
|
58
59
|
path: e.path,
|
|
59
|
-
initialValue:
|
|
60
|
+
initialValue: f(() => Object.freeze(y(e.initialValue))),
|
|
60
61
|
errors: u(e.errors) || [],
|
|
61
62
|
touched: !1
|
|
62
63
|
});
|
|
63
|
-
F(() => u(e.errors), (
|
|
64
|
-
r.errors =
|
|
64
|
+
F(() => u(e.errors), (d) => {
|
|
65
|
+
r.errors = d || [];
|
|
65
66
|
});
|
|
66
|
-
const t =
|
|
67
|
-
r.value =
|
|
67
|
+
const t = f(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), s = (d) => {
|
|
68
|
+
r.value = d;
|
|
68
69
|
}, a = () => {
|
|
69
70
|
r.touched = !0;
|
|
70
71
|
}, n = () => {
|
|
71
72
|
}, o = () => {
|
|
72
73
|
r.value = y(r.initialValue), r.touched = !1, r.errors = [];
|
|
73
|
-
}, v = (
|
|
74
|
-
r.errors =
|
|
74
|
+
}, v = (d) => {
|
|
75
|
+
r.errors = d;
|
|
75
76
|
}, l = () => {
|
|
76
77
|
r.errors = [];
|
|
77
78
|
}, c = O(r);
|
|
@@ -99,7 +100,7 @@ function H(e) {
|
|
|
99
100
|
const v = q({
|
|
100
101
|
path: o,
|
|
101
102
|
value: z(D(e, "data"), o),
|
|
102
|
-
initialValue:
|
|
103
|
+
initialValue: f(() => w(e.initialData, o))
|
|
103
104
|
});
|
|
104
105
|
return t(v), v;
|
|
105
106
|
}
|
|
@@ -114,7 +115,7 @@ function H(e) {
|
|
|
114
115
|
};
|
|
115
116
|
}
|
|
116
117
|
function Q(e) {
|
|
117
|
-
const r =
|
|
118
|
+
const r = f(() => e.getFields().some((s) => u(s.dirty))), t = f(() => e.getFields().some((s) => u(s.touched)));
|
|
118
119
|
return {
|
|
119
120
|
isDirty: r,
|
|
120
121
|
isTouched: t
|
|
@@ -246,7 +247,7 @@ class sr {
|
|
|
246
247
|
}
|
|
247
248
|
}
|
|
248
249
|
function b(e) {
|
|
249
|
-
return
|
|
250
|
+
return f(() => new sr(
|
|
250
251
|
u(e.schema),
|
|
251
252
|
u(e.validateFn)
|
|
252
253
|
));
|
|
@@ -278,7 +279,7 @@ function ar(e, r) {
|
|
|
278
279
|
const c = _(l) ? l : b(l);
|
|
279
280
|
return t.validators.push(c), J() && T(() => {
|
|
280
281
|
t.validators = t.validators.filter(
|
|
281
|
-
(
|
|
282
|
+
(d) => d !== c
|
|
282
283
|
);
|
|
283
284
|
}), c;
|
|
284
285
|
};
|
|
@@ -286,13 +287,13 @@ function ar(e, r) {
|
|
|
286
287
|
const l = await Promise.all(
|
|
287
288
|
t.validators.filter((V) => u(V) !== void 0).map((V) => u(V).validate(e.data))
|
|
288
289
|
), c = l.every((V) => V.isValid);
|
|
289
|
-
let { errors:
|
|
290
|
+
let { errors: d } = p;
|
|
290
291
|
if (!c) {
|
|
291
292
|
const V = l.map((R) => R.errors);
|
|
292
|
-
|
|
293
|
+
d = P(...V);
|
|
293
294
|
}
|
|
294
295
|
return {
|
|
295
|
-
errors:
|
|
296
|
+
errors: d,
|
|
296
297
|
isValid: c
|
|
297
298
|
};
|
|
298
299
|
}
|
|
@@ -304,7 +305,7 @@ function ar(e, r) {
|
|
|
304
305
|
isValid: !j(l.errors),
|
|
305
306
|
errors: t.errors
|
|
306
307
|
};
|
|
307
|
-
}, v =
|
|
308
|
+
}, v = f(() => !j(t.errors));
|
|
308
309
|
return {
|
|
309
310
|
...O(t),
|
|
310
311
|
validateForm: o,
|
|
@@ -336,9 +337,9 @@ class ir {
|
|
|
336
337
|
}
|
|
337
338
|
}
|
|
338
339
|
function nr(e, r, t) {
|
|
339
|
-
const s = z(e.data, r), a =
|
|
340
|
+
const s = z(e.data, r), a = f(() => w(e.initialData.value, r)), n = (i) => ({
|
|
340
341
|
...i,
|
|
341
|
-
path:
|
|
342
|
+
path: f(() => u(i.path).replace(r + ".", "")),
|
|
342
343
|
setData: (h) => {
|
|
343
344
|
i.setData(h);
|
|
344
345
|
}
|
|
@@ -357,20 +358,20 @@ function nr(e, r, t) {
|
|
|
357
358
|
}).map((i) => n(i)), c = () => e.getFields().filter((i) => {
|
|
358
359
|
const h = i.path.value;
|
|
359
360
|
return h.startsWith(r + ".") || h === r;
|
|
360
|
-
}),
|
|
361
|
+
}), d = f(() => c().some((i) => i.dirty.value)), V = f(() => c().some((i) => i.touched.value)), R = f(() => e.isValid.value), M = f(() => e.isValidated.value), $ = f(() => k(u(e.errors), r));
|
|
361
362
|
return {
|
|
362
363
|
data: s,
|
|
363
364
|
initialData: a,
|
|
364
365
|
defineField: v,
|
|
365
366
|
getField: o,
|
|
366
367
|
getFields: l,
|
|
367
|
-
isDirty:
|
|
368
|
+
isDirty: d,
|
|
368
369
|
isTouched: V,
|
|
369
370
|
isValid: R,
|
|
370
371
|
isValidated: M,
|
|
371
372
|
errors: $,
|
|
372
373
|
defineValidator: (i) => {
|
|
373
|
-
const h = _(i) ? i : b(i), g =
|
|
374
|
+
const h = _(i) ? i : b(i), g = f(
|
|
374
375
|
() => new ir(r, u(h))
|
|
375
376
|
);
|
|
376
377
|
return e.defineValidator(g), h;
|
|
@@ -386,17 +387,21 @@ function nr(e, r, t) {
|
|
|
386
387
|
}
|
|
387
388
|
};
|
|
388
389
|
}
|
|
389
|
-
function
|
|
390
|
-
const r =
|
|
390
|
+
function Vr(e) {
|
|
391
|
+
const r = f(() => Object.freeze(y(e.initialData))), t = A(y(r)), s = m({
|
|
391
392
|
initialData: r,
|
|
392
393
|
data: t
|
|
393
|
-
})
|
|
394
|
+
});
|
|
395
|
+
F(r, (d) => {
|
|
396
|
+
s.data = y(d);
|
|
397
|
+
});
|
|
398
|
+
const a = H(s), n = ar(s, e), o = Q(a), v = () => {
|
|
394
399
|
t.value = y(r), a.getFields().forEach(
|
|
395
|
-
(
|
|
400
|
+
(d) => d.reset()
|
|
396
401
|
);
|
|
397
402
|
};
|
|
398
|
-
function l(
|
|
399
|
-
return nr(c,
|
|
403
|
+
function l(d, V) {
|
|
404
|
+
return nr(c, d);
|
|
400
405
|
}
|
|
401
406
|
const c = {
|
|
402
407
|
...a,
|
|
@@ -409,7 +414,7 @@ function vr(e) {
|
|
|
409
414
|
};
|
|
410
415
|
return c;
|
|
411
416
|
}
|
|
412
|
-
const
|
|
417
|
+
const pr = /* @__PURE__ */ B({
|
|
413
418
|
__name: "Field",
|
|
414
419
|
props: {
|
|
415
420
|
form: {},
|
|
@@ -426,6 +431,6 @@ const Vr = /* @__PURE__ */ B({
|
|
|
426
431
|
}
|
|
427
432
|
});
|
|
428
433
|
export {
|
|
429
|
-
|
|
430
|
-
|
|
434
|
+
pr as Field,
|
|
435
|
+
Vr as useForm
|
|
431
436
|
};
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { computed, reactive, ref, toRef, type MaybeRef, type MaybeRefOrGetter, type Ref } from 'vue'
|
|
1
|
+
import { computed, reactive, ref, toRef, watch, type MaybeRef, type MaybeRefOrGetter, type Ref } from 'vue'
|
|
2
2
|
import type { AnyField, Form, FormDataDefault } from '../types/form'
|
|
3
3
|
import type { EntityPaths, PickEntity } from '../types/util'
|
|
4
4
|
import type { ValidationStrategy } from '../types/validation'
|
|
@@ -7,6 +7,7 @@ import { useFieldRegistry } from './useFieldRegistry'
|
|
|
7
7
|
import { useFormState } from './useFormState'
|
|
8
8
|
import { createSubformInterface, type SubformOptions } from './useSubform'
|
|
9
9
|
import { useValidation, type ValidationOptions } from './useValidation'
|
|
10
|
+
import { syncRef } from '@vueuse/core'
|
|
10
11
|
|
|
11
12
|
// TODO @Elias implement validation strategy handling
|
|
12
13
|
|
|
@@ -22,10 +23,14 @@ export function useForm<T extends FormDataDefault>(options: UseFormOptions<T>) {
|
|
|
22
23
|
const data = ref<T>(cloneRefValue(initialData)) as Ref<T>
|
|
23
24
|
|
|
24
25
|
const state = reactive({
|
|
25
|
-
initialData
|
|
26
|
+
initialData,
|
|
26
27
|
data,
|
|
27
28
|
})
|
|
28
29
|
|
|
30
|
+
watch(initialData, (newValue) => {
|
|
31
|
+
state.data = cloneRefValue(newValue)
|
|
32
|
+
})
|
|
33
|
+
|
|
29
34
|
const fields = useFieldRegistry(state)
|
|
30
35
|
const validationState = useValidation(state, options)
|
|
31
36
|
const formState = useFormState(fields)
|
package/tests/nestedPath.test.ts
CHANGED
|
@@ -540,16 +540,14 @@ describe('Nested Path Handling', () => {
|
|
|
540
540
|
})
|
|
541
541
|
|
|
542
542
|
describe('Performance and Memory', () => {
|
|
543
|
-
it('should
|
|
543
|
+
it('should return the same field when defining same path twice', () => {
|
|
544
544
|
const form = useForm({ initialData })
|
|
545
545
|
|
|
546
546
|
const field1 = form.defineField({ path: 'user.name' })
|
|
547
547
|
const field2 = form.defineField({ path: 'user.name' })
|
|
548
548
|
|
|
549
|
-
// Fields are
|
|
550
|
-
expect(field1).
|
|
551
|
-
expect(form.getField('user.name')).toBe(field2)
|
|
552
|
-
expect(form.getFields().length).toBe(1)
|
|
549
|
+
// Fields are the same object
|
|
550
|
+
expect(field1).toBe(field2)
|
|
553
551
|
})
|
|
554
552
|
|
|
555
553
|
it('should handle large numbers of nested fields', () => {
|
package/tests/useForm.test.ts
CHANGED
|
@@ -40,6 +40,31 @@ describe('useForm', () => {
|
|
|
40
40
|
})
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
+
it('should reinitialize form with new initial data', async () => {
|
|
44
|
+
const initialData = ref({
|
|
45
|
+
name: 'John',
|
|
46
|
+
age: 30,
|
|
47
|
+
})
|
|
48
|
+
const form = useForm({ initialData })
|
|
49
|
+
|
|
50
|
+
expect(form.data.value).toEqual({
|
|
51
|
+
name: 'John',
|
|
52
|
+
age: 30,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// Update reactive initial data
|
|
56
|
+
initialData.value = {
|
|
57
|
+
name: 'Jane',
|
|
58
|
+
age: 25,
|
|
59
|
+
}
|
|
60
|
+
await nextTick()
|
|
61
|
+
|
|
62
|
+
expect(form.data.value).toEqual({
|
|
63
|
+
name: 'Jane',
|
|
64
|
+
age: 25,
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
43
68
|
it('should have initial state values', () => {
|
|
44
69
|
const form = useForm({ initialData: { name: 'John' } })
|
|
45
70
|
|
|
@@ -81,13 +106,6 @@ describe('useForm', () => {
|
|
|
81
106
|
expect(retrievedField).toBe(nameField)
|
|
82
107
|
})
|
|
83
108
|
|
|
84
|
-
it('should return undefined for non-existent fields', () => {
|
|
85
|
-
const form = useForm({ initialData: { name: 'John' } })
|
|
86
|
-
|
|
87
|
-
const field = form.getField('nonexistent')
|
|
88
|
-
expect(field).toBeUndefined()
|
|
89
|
-
})
|
|
90
|
-
|
|
91
109
|
it('should handle nested object initial data', () => {
|
|
92
110
|
const initialData = {
|
|
93
111
|
user: {
|