@teamnovu/kit-vue-forms 0.1.10 → 0.1.11
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.js +112 -105
- package/dist/types/form.d.ts +4 -0
- package/package.json +1 -1
- package/src/composables/useField.ts +11 -6
- package/src/types/form.ts +4 -0
- package/tests/useField.test.ts +42 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var T = Object.defineProperty;
|
|
2
2
|
var G = (t, e, r) => e in t ? T(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
-
var
|
|
4
|
-
import { toValue as L, toRaw as Z, computed as
|
|
3
|
+
var w = (t, e, r) => G(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { toValue as L, toRaw as Z, computed as d, unref as f, isRef as O, ref as j, watch as E, reactive as D, toRefs as W, shallowReactive as q, toRef as _, onScopeDispose as H, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as $, renderSlot as A, normalizeProps as k, guardReactiveProps as B, resolveComponent as Y, createBlock as C, openBlock as M, withCtx as z, resolveDynamicComponent as x, mergeProps as ee } from "vue";
|
|
5
5
|
import { cloneDeep as re } from "lodash-es";
|
|
6
6
|
import "zod";
|
|
7
7
|
function g(t) {
|
|
@@ -21,9 +21,9 @@ function te(t, e, r) {
|
|
|
21
21
|
const s = Array.isArray(e) ? e : I(e), o = s.at(-1);
|
|
22
22
|
if (o) {
|
|
23
23
|
const i = s.slice(0, -1).reduce(
|
|
24
|
-
(
|
|
24
|
+
(h, v) => h[v],
|
|
25
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
-
|
|
26
|
+
f(t)
|
|
27
27
|
);
|
|
28
28
|
i[o] = r;
|
|
29
29
|
} else {
|
|
@@ -32,12 +32,12 @@ function te(t, e, r) {
|
|
|
32
32
|
t.value = r;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
const K = (t, e) =>
|
|
35
|
+
const K = (t, e) => d({
|
|
36
36
|
get() {
|
|
37
|
-
return P(
|
|
37
|
+
return P(f(t), f(e));
|
|
38
38
|
},
|
|
39
39
|
set(r) {
|
|
40
|
-
te(t,
|
|
40
|
+
te(t, f(e), r);
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
function R(t, e) {
|
|
@@ -59,7 +59,7 @@ function se(t, e) {
|
|
|
59
59
|
}
|
|
60
60
|
class ae {
|
|
61
61
|
constructor(e) {
|
|
62
|
-
|
|
62
|
+
w(this, "rc", 1);
|
|
63
63
|
this.drop = e;
|
|
64
64
|
}
|
|
65
65
|
inc() {
|
|
@@ -70,61 +70,68 @@ class ae {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
function oe(t) {
|
|
73
|
-
const e =
|
|
73
|
+
const e = j(Object.freeze(g(t.initialValue)));
|
|
74
|
+
E(
|
|
75
|
+
() => f(t.initialValue),
|
|
76
|
+
(u) => {
|
|
77
|
+
e.value = Object.freeze(g(u));
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
const r = D({
|
|
74
81
|
value: t.value,
|
|
75
82
|
path: t.path,
|
|
76
|
-
initialValue:
|
|
83
|
+
initialValue: e,
|
|
77
84
|
errors: t.errors,
|
|
78
85
|
touched: !1
|
|
79
|
-
}),
|
|
80
|
-
|
|
81
|
-
}, o = () => {
|
|
82
|
-
e.touched = !0;
|
|
86
|
+
}), s = d(() => JSON.stringify(r.value) !== JSON.stringify(r.initialValue)), o = (u) => {
|
|
87
|
+
r.value = u;
|
|
83
88
|
}, i = () => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
},
|
|
87
|
-
r.value
|
|
88
|
-
}, V = (
|
|
89
|
-
|
|
90
|
-
}, n = () => {
|
|
91
|
-
|
|
92
|
-
}, a =
|
|
89
|
+
r.touched = !0;
|
|
90
|
+
}, h = () => {
|
|
91
|
+
}, v = () => {
|
|
92
|
+
r.value = g(r.initialValue), r.touched = !1, r.errors = [];
|
|
93
|
+
}, V = (u) => {
|
|
94
|
+
s.value || o(g(u)), r.initialValue = u;
|
|
95
|
+
}, n = (u) => {
|
|
96
|
+
r.errors = u;
|
|
97
|
+
}, a = () => {
|
|
98
|
+
r.errors = [];
|
|
99
|
+
}, c = W(r);
|
|
93
100
|
return {
|
|
94
|
-
data:
|
|
95
|
-
path:
|
|
96
|
-
initialValue:
|
|
97
|
-
errors:
|
|
98
|
-
touched:
|
|
99
|
-
dirty:
|
|
100
|
-
setData:
|
|
101
|
-
setInitialData:
|
|
102
|
-
onBlur:
|
|
103
|
-
onFocus:
|
|
104
|
-
reset:
|
|
105
|
-
setErrors:
|
|
106
|
-
clearErrors:
|
|
101
|
+
data: c.value,
|
|
102
|
+
path: c.path,
|
|
103
|
+
initialValue: c.initialValue,
|
|
104
|
+
errors: c.errors,
|
|
105
|
+
touched: c.touched,
|
|
106
|
+
dirty: s,
|
|
107
|
+
setData: o,
|
|
108
|
+
setInitialData: V,
|
|
109
|
+
onBlur: i,
|
|
110
|
+
onFocus: h,
|
|
111
|
+
reset: v,
|
|
112
|
+
setErrors: n,
|
|
113
|
+
clearErrors: a
|
|
107
114
|
};
|
|
108
115
|
}
|
|
109
116
|
function ie(t, e) {
|
|
110
117
|
const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
|
|
111
|
-
const c =
|
|
118
|
+
const c = f(a.path);
|
|
112
119
|
s.set(c, a);
|
|
113
120
|
}, i = (a) => {
|
|
114
121
|
s.delete(a);
|
|
115
|
-
},
|
|
122
|
+
}, h = (a) => {
|
|
116
123
|
var c;
|
|
117
124
|
r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ae(() => i(a)));
|
|
118
|
-
},
|
|
125
|
+
}, v = (a) => {
|
|
119
126
|
var c;
|
|
120
127
|
r.has(a) && ((c = r.get(a)) == null || c.dec());
|
|
121
128
|
}, V = (a) => {
|
|
122
129
|
if (!s.has(a)) {
|
|
123
|
-
const
|
|
130
|
+
const u = oe({
|
|
124
131
|
path: a,
|
|
125
132
|
value: K(_(t, "data"), a),
|
|
126
|
-
initialValue:
|
|
127
|
-
errors:
|
|
133
|
+
initialValue: d(() => P(t.initialData, a)),
|
|
134
|
+
errors: d({
|
|
128
135
|
get() {
|
|
129
136
|
return e.errors.value.propertyErrors[a] || [];
|
|
130
137
|
},
|
|
@@ -133,15 +140,15 @@ function ie(t, e) {
|
|
|
133
140
|
}
|
|
134
141
|
})
|
|
135
142
|
});
|
|
136
|
-
o(
|
|
143
|
+
o(u);
|
|
137
144
|
}
|
|
138
145
|
const c = s.get(a);
|
|
139
|
-
return
|
|
140
|
-
|
|
146
|
+
return h(a), H(() => {
|
|
147
|
+
v(a);
|
|
141
148
|
}), c;
|
|
142
149
|
}, n = (a) => V(a.path);
|
|
143
150
|
return {
|
|
144
|
-
fields:
|
|
151
|
+
fields: d(() => [...s.values()]),
|
|
145
152
|
getField: V,
|
|
146
153
|
registerField: o,
|
|
147
154
|
deregisterField: i,
|
|
@@ -149,7 +156,7 @@ function ie(t, e) {
|
|
|
149
156
|
};
|
|
150
157
|
}
|
|
151
158
|
function ne(t) {
|
|
152
|
-
const e =
|
|
159
|
+
const e = d(() => t.fields.value.some((s) => f(s.dirty))), r = d(() => t.fields.value.some((s) => f(s.touched)));
|
|
153
160
|
return {
|
|
154
161
|
isDirty: e,
|
|
155
162
|
isTouched: r
|
|
@@ -265,8 +272,8 @@ class fe {
|
|
|
265
272
|
}
|
|
266
273
|
class pe {
|
|
267
274
|
constructor(e, r) {
|
|
268
|
-
|
|
269
|
-
|
|
275
|
+
w(this, "schemaValidator");
|
|
276
|
+
w(this, "functionValidator");
|
|
270
277
|
this.schema = e, this.validateFn = r, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
|
|
271
278
|
}
|
|
272
279
|
async validate(e) {
|
|
@@ -281,26 +288,26 @@ class pe {
|
|
|
281
288
|
}
|
|
282
289
|
}
|
|
283
290
|
function b(t) {
|
|
284
|
-
return
|
|
285
|
-
|
|
286
|
-
|
|
291
|
+
return d(() => new pe(
|
|
292
|
+
f(t.schema),
|
|
293
|
+
f(t.validateFn)
|
|
287
294
|
));
|
|
288
295
|
}
|
|
289
296
|
function he(t, e) {
|
|
290
297
|
const r = D({
|
|
291
|
-
validators:
|
|
298
|
+
validators: j([b(e)]),
|
|
292
299
|
isValidated: !1,
|
|
293
|
-
errors:
|
|
300
|
+
errors: f(e.errors) ?? m.errors
|
|
294
301
|
}), s = (n = m.errors) => {
|
|
295
|
-
r.errors = S(
|
|
302
|
+
r.errors = S(f(e.errors) ?? m.errors, n);
|
|
296
303
|
};
|
|
297
|
-
|
|
304
|
+
E(() => f(e.errors), async () => {
|
|
298
305
|
if (r.isValidated) {
|
|
299
306
|
const n = await i();
|
|
300
307
|
s(n.errors);
|
|
301
308
|
} else
|
|
302
309
|
s();
|
|
303
|
-
}, { immediate: !0 }),
|
|
310
|
+
}, { immediate: !0 }), E(
|
|
304
311
|
[() => r.validators],
|
|
305
312
|
async (n) => {
|
|
306
313
|
if (r.isValidated)
|
|
@@ -311,8 +318,8 @@ function he(t, e) {
|
|
|
311
318
|
r.errors = m.errors;
|
|
312
319
|
},
|
|
313
320
|
{ immediate: !0 }
|
|
314
|
-
),
|
|
315
|
-
r.isValidated &&
|
|
321
|
+
), E(() => t.data, () => {
|
|
322
|
+
r.isValidated && h();
|
|
316
323
|
});
|
|
317
324
|
const o = (n) => {
|
|
318
325
|
const a = O(n) ? n : b(n);
|
|
@@ -324,32 +331,32 @@ function he(t, e) {
|
|
|
324
331
|
};
|
|
325
332
|
async function i() {
|
|
326
333
|
const n = await Promise.all(
|
|
327
|
-
r.validators.filter((
|
|
328
|
-
), a = n.every((
|
|
334
|
+
r.validators.filter((u) => f(u) !== void 0).map((u) => f(u).validate(t.data))
|
|
335
|
+
), a = n.every((u) => u.isValid);
|
|
329
336
|
let { errors: c } = m;
|
|
330
337
|
if (!a) {
|
|
331
|
-
const
|
|
332
|
-
c = S(...
|
|
338
|
+
const u = n.map((F) => F.errors);
|
|
339
|
+
c = S(...u);
|
|
333
340
|
}
|
|
334
341
|
return {
|
|
335
342
|
errors: c,
|
|
336
343
|
isValid: a
|
|
337
344
|
};
|
|
338
345
|
}
|
|
339
|
-
const
|
|
346
|
+
const h = async () => {
|
|
340
347
|
const n = await i();
|
|
341
348
|
return s(n.errors), r.isValidated = !0, {
|
|
342
349
|
isValid: !N(n.errors),
|
|
343
350
|
errors: r.errors
|
|
344
351
|
};
|
|
345
|
-
},
|
|
346
|
-
r.isValidated = !1, r.errors =
|
|
352
|
+
}, v = d(() => !N(r.errors)), V = () => {
|
|
353
|
+
r.isValidated = !1, r.errors = f(e.errors) ?? m.errors;
|
|
347
354
|
};
|
|
348
355
|
return {
|
|
349
356
|
...W(r),
|
|
350
|
-
validateForm:
|
|
357
|
+
validateForm: h,
|
|
351
358
|
defineValidator: o,
|
|
352
|
-
isValid:
|
|
359
|
+
isValid: v,
|
|
353
360
|
reset: V
|
|
354
361
|
};
|
|
355
362
|
}
|
|
@@ -377,65 +384,65 @@ class ve {
|
|
|
377
384
|
}
|
|
378
385
|
}
|
|
379
386
|
function me(t, e, r) {
|
|
380
|
-
const s = K(t.data, e), o =
|
|
387
|
+
const s = K(t.data, e), o = d(() => P(t.initialData.value, e)), i = (l) => ({
|
|
381
388
|
...l,
|
|
382
|
-
path:
|
|
383
|
-
setData: (
|
|
384
|
-
l.setData(
|
|
389
|
+
path: d(() => f(l.path).replace(e + ".", "")),
|
|
390
|
+
setData: (p) => {
|
|
391
|
+
l.setData(p);
|
|
385
392
|
}
|
|
386
|
-
}),
|
|
387
|
-
const
|
|
393
|
+
}), h = (l) => {
|
|
394
|
+
const p = R(e, l), y = t.getField(p);
|
|
388
395
|
return y ? i(y) : {};
|
|
389
|
-
},
|
|
390
|
-
const
|
|
396
|
+
}, v = (l) => {
|
|
397
|
+
const p = R(e, l.path), y = t.defineField({
|
|
391
398
|
...l,
|
|
392
|
-
path:
|
|
399
|
+
path: p
|
|
393
400
|
});
|
|
394
401
|
return i(y);
|
|
395
|
-
}, V =
|
|
396
|
-
const
|
|
397
|
-
return
|
|
402
|
+
}, V = d(() => t.fields.value.filter((l) => {
|
|
403
|
+
const p = l.path.value;
|
|
404
|
+
return p.startsWith(e + ".") || p === e;
|
|
398
405
|
}).map((l) => i(l))), n = () => t.fields.value.filter((l) => {
|
|
399
|
-
const
|
|
400
|
-
return
|
|
401
|
-
}), a =
|
|
406
|
+
const p = l.path.value;
|
|
407
|
+
return p.startsWith(e + ".") || p === e;
|
|
408
|
+
}), a = d(() => n().some((l) => l.dirty.value)), c = d(() => n().some((l) => l.touched.value)), u = d(() => t.isValid.value), F = d(() => t.isValidated.value), J = d(() => se(f(t.errors), e));
|
|
402
409
|
return {
|
|
403
410
|
data: s,
|
|
404
411
|
fields: V,
|
|
405
412
|
initialData: o,
|
|
406
|
-
defineField:
|
|
407
|
-
getField:
|
|
413
|
+
defineField: v,
|
|
414
|
+
getField: h,
|
|
408
415
|
isDirty: a,
|
|
409
416
|
isTouched: c,
|
|
410
|
-
isValid:
|
|
417
|
+
isValid: u,
|
|
411
418
|
isValidated: F,
|
|
412
419
|
errors: J,
|
|
413
420
|
defineValidator: (l) => {
|
|
414
|
-
const
|
|
415
|
-
() => new ve(e,
|
|
421
|
+
const p = O(l) ? l : b(l), y = d(
|
|
422
|
+
() => new ve(e, f(p))
|
|
416
423
|
);
|
|
417
|
-
return t.defineValidator(y),
|
|
424
|
+
return t.defineValidator(y), p;
|
|
418
425
|
},
|
|
419
426
|
reset: () => n().forEach((l) => l.reset()),
|
|
420
427
|
validateForm: () => t.validateForm(),
|
|
421
|
-
getSubForm: (l,
|
|
428
|
+
getSubForm: (l, p) => {
|
|
422
429
|
const y = R(e, l);
|
|
423
430
|
return t.getSubForm(
|
|
424
431
|
y,
|
|
425
|
-
|
|
432
|
+
p
|
|
426
433
|
);
|
|
427
434
|
}
|
|
428
435
|
};
|
|
429
436
|
}
|
|
430
437
|
function Pe(t) {
|
|
431
|
-
const e =
|
|
438
|
+
const e = d(() => Object.freeze(g(t.initialData))), r = j(g(e)), s = D({
|
|
432
439
|
initialData: e,
|
|
433
440
|
data: r
|
|
434
441
|
});
|
|
435
|
-
|
|
442
|
+
E(e, (a) => {
|
|
436
443
|
s.data = g(a);
|
|
437
444
|
});
|
|
438
|
-
const o = he(s, t), i = ie(s, o),
|
|
445
|
+
const o = he(s, t), i = ie(s, o), h = ne(i), v = () => {
|
|
439
446
|
r.value = g(e), o.reset(), i.fields.value.forEach(
|
|
440
447
|
(a) => a.reset()
|
|
441
448
|
);
|
|
@@ -446,8 +453,8 @@ function Pe(t) {
|
|
|
446
453
|
const n = {
|
|
447
454
|
...i,
|
|
448
455
|
...o,
|
|
449
|
-
...
|
|
450
|
-
reset:
|
|
456
|
+
...h,
|
|
457
|
+
reset: v,
|
|
451
458
|
getSubForm: V,
|
|
452
459
|
initialData: _(s, "initialData"),
|
|
453
460
|
data: _(s, "data")
|
|
@@ -467,7 +474,7 @@ const _e = /* @__PURE__ */ $({
|
|
|
467
474
|
const e = t, r = e.form.defineField({
|
|
468
475
|
path: e.path
|
|
469
476
|
}), s = D(r);
|
|
470
|
-
return (o, i) =>
|
|
477
|
+
return (o, i) => A(o.$slots, "default", k(B(s)));
|
|
471
478
|
}
|
|
472
479
|
}), Se = /* @__PURE__ */ $({
|
|
473
480
|
inheritAttrs: !1,
|
|
@@ -481,19 +488,19 @@ const _e = /* @__PURE__ */ $({
|
|
|
481
488
|
setup(t) {
|
|
482
489
|
return (e, r) => {
|
|
483
490
|
const s = Y("Field");
|
|
484
|
-
return
|
|
491
|
+
return M(), C(s, {
|
|
485
492
|
form: e.form,
|
|
486
493
|
path: e.path
|
|
487
494
|
}, {
|
|
488
|
-
default:
|
|
489
|
-
(
|
|
495
|
+
default: z(({ errors: o, data: i, setData: h }) => [
|
|
496
|
+
(M(), C(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
|
|
490
497
|
"model-value": i,
|
|
491
498
|
errors: o,
|
|
492
499
|
name: e.path,
|
|
493
|
-
"onUpdate:modelValue":
|
|
500
|
+
"onUpdate:modelValue": h
|
|
494
501
|
}), {
|
|
495
|
-
default:
|
|
496
|
-
|
|
502
|
+
default: z(() => [
|
|
503
|
+
A(e.$slots, "default")
|
|
497
504
|
]),
|
|
498
505
|
_: 2
|
|
499
506
|
}, 1040, ["model-value", "errors", "name", "onUpdate:modelValue"]))
|
|
@@ -509,8 +516,8 @@ const _e = /* @__PURE__ */ $({
|
|
|
509
516
|
path: {}
|
|
510
517
|
},
|
|
511
518
|
setup(t) {
|
|
512
|
-
const e = t, r =
|
|
513
|
-
return (s, o) =>
|
|
519
|
+
const e = t, r = d(() => e.form.getSubForm(e.path));
|
|
520
|
+
return (s, o) => A(s.$slots, "default", k(B({ subform: r.value })));
|
|
514
521
|
}
|
|
515
522
|
});
|
|
516
523
|
export {
|
package/dist/types/form.d.ts
CHANGED
|
@@ -13,6 +13,10 @@ export interface FormField<T, P extends string> {
|
|
|
13
13
|
touched: Ref<boolean>;
|
|
14
14
|
dirty: Ref<boolean>;
|
|
15
15
|
setData: (newData: T) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Sets the initial data for the field. If the field is not dirty, it also updates the current data.
|
|
18
|
+
* @param newData - The new initial data to set.
|
|
19
|
+
*/
|
|
16
20
|
setInitialData: (newData: T) => void;
|
|
17
21
|
onBlur: () => void;
|
|
18
22
|
onFocus: () => void;
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { computed, reactive, toRefs, type MaybeRef, type MaybeRefOrGetter, type WritableComputedRef } from 'vue'
|
|
1
|
+
import { computed, reactive, ref, toRefs, unref, watch, type MaybeRef, type MaybeRefOrGetter, type Ref, type WritableComputedRef } from 'vue'
|
|
2
2
|
import type { FormField } from '../types/form'
|
|
3
3
|
import type { ValidationErrorMessage, ValidationErrors } from '../types/validation'
|
|
4
4
|
import { cloneRefValue } from '../utils/general'
|
|
@@ -11,10 +11,19 @@ export interface UseFieldOptions<T, K extends string> {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function useField<T, K extends string>(options: UseFieldOptions<T, K>): FormField<T, K> {
|
|
14
|
+
const initialValue = ref(Object.freeze(cloneRefValue(options.initialValue))) as Ref<Readonly<T | undefined>>
|
|
15
|
+
|
|
16
|
+
watch(
|
|
17
|
+
() => unref(options.initialValue),
|
|
18
|
+
(newInitialValue) => {
|
|
19
|
+
initialValue.value = Object.freeze(cloneRefValue(newInitialValue))
|
|
20
|
+
},
|
|
21
|
+
)
|
|
22
|
+
|
|
14
23
|
const state = reactive({
|
|
15
24
|
value: options.value,
|
|
16
25
|
path: options.path,
|
|
17
|
-
initialValue
|
|
26
|
+
initialValue,
|
|
18
27
|
errors: options.errors,
|
|
19
28
|
touched: false,
|
|
20
29
|
})
|
|
@@ -41,10 +50,6 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
|
|
|
41
50
|
state.errors = []
|
|
42
51
|
}
|
|
43
52
|
|
|
44
|
-
/**
|
|
45
|
-
* Sets the initial data for the field. If the field is not dirty, it also updates the current data.
|
|
46
|
-
* @param newData - The new initial data to set.
|
|
47
|
-
*/
|
|
48
53
|
const setInitialData = (newData: T): void => {
|
|
49
54
|
if (!dirty.value) {
|
|
50
55
|
setData(cloneRefValue(newData))
|
package/src/types/form.ts
CHANGED
|
@@ -15,6 +15,10 @@ export interface FormField<T, P extends string> {
|
|
|
15
15
|
touched: Ref<boolean>
|
|
16
16
|
dirty: Ref<boolean>
|
|
17
17
|
setData: (newData: T) => void
|
|
18
|
+
/**
|
|
19
|
+
* Sets the initial data for the field. If the field is not dirty, it also updates the current data.
|
|
20
|
+
* @param newData - The new initial data to set.
|
|
21
|
+
*/
|
|
18
22
|
setInitialData: (newData: T) => void
|
|
19
23
|
onBlur: () => void
|
|
20
24
|
onFocus: () => void
|
package/tests/useField.test.ts
CHANGED
|
@@ -160,10 +160,51 @@ describe('useField', () => {
|
|
|
160
160
|
|
|
161
161
|
watch(initialValue, () => {
|
|
162
162
|
resolve(true)
|
|
163
|
-
}, {
|
|
163
|
+
}, {
|
|
164
|
+
once: true,
|
|
165
|
+
deep: true,
|
|
166
|
+
})
|
|
164
167
|
|
|
165
168
|
field.setData(['a', 'b', 'c', 'd'])
|
|
166
169
|
expect(field.dirty.value).toBe(true)
|
|
167
170
|
})
|
|
168
171
|
}))
|
|
172
|
+
|
|
173
|
+
it('it should set the initial value if the field is not dirty', { timeout: 500 }, () => {
|
|
174
|
+
const field = useField({
|
|
175
|
+
initialValue: 'foo',
|
|
176
|
+
value: 'foo',
|
|
177
|
+
path: 'name',
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
expect(field.data.value).toBe('foo')
|
|
181
|
+
expect(field.path.value).toBe('name')
|
|
182
|
+
expect(field.touched.value).toBe(false)
|
|
183
|
+
expect(field.dirty.value).toBe(false)
|
|
184
|
+
|
|
185
|
+
field.setInitialData('bar')
|
|
186
|
+
|
|
187
|
+
expect(field.initialValue.value).toBe('bar')
|
|
188
|
+
expect(field.data.value).toBe('bar')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('it should set the initial value but not the data if the field is dirty', { timeout: 500 }, () => {
|
|
192
|
+
const field = useField({
|
|
193
|
+
initialValue: 'foo',
|
|
194
|
+
value: 'foo',
|
|
195
|
+
path: 'name',
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
expect(field.data.value).toBe('foo')
|
|
199
|
+
expect(field.path.value).toBe('name')
|
|
200
|
+
expect(field.touched.value).toBe(false)
|
|
201
|
+
expect(field.dirty.value).toBe(false)
|
|
202
|
+
|
|
203
|
+
field.data.value = 'modified'
|
|
204
|
+
|
|
205
|
+
field.setInitialData('bar')
|
|
206
|
+
|
|
207
|
+
expect(field.initialValue.value).toBe('bar')
|
|
208
|
+
expect(field.data.value).toBe('modified')
|
|
209
|
+
})
|
|
169
210
|
})
|