@teamnovu/kit-vue-forms 0.1.8 → 0.1.10

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.
@@ -19,6 +19,7 @@ declare const _default: <TData extends object, TPath extends Paths<TData>>(__VLS
19
19
  touched: boolean;
20
20
  dirty: boolean;
21
21
  setData: (newData: PickProps<TData, TPath>) => void;
22
+ setInitialData: (newData: PickProps<TData, TPath>) => void;
22
23
  onBlur: () => void;
23
24
  onFocus: () => void;
24
25
  reset: () => void;
package/dist/index.js CHANGED
@@ -1,57 +1,58 @@
1
1
  var T = Object.defineProperty;
2
- var G = (r, e, t) => e in r ? T(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
- var E = (r, e, t) => G(r, typeof e != "symbol" ? e + "" : e, t);
4
- import { toValue as L, toRaw as Z, computed as c, unref as d, isRef as D, reactive as P, toRefs as W, shallowReactive as q, toRef as S, onScopeDispose as H, ref as k, watch as w, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as $, renderSlot as j, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as A, openBlock as C, withCtx as M, resolveDynamicComponent as x, mergeProps as ee } from "vue";
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 E = (t, e, r) => G(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { toValue as L, toRaw as Z, computed as u, unref as d, isRef as O, reactive as D, toRefs as W, shallowReactive as q, toRef as _, onScopeDispose as H, ref as k, watch as w, getCurrentScope as Q, onBeforeUnmount as X, defineComponent as $, renderSlot as j, normalizeProps as z, guardReactiveProps as B, resolveComponent as Y, createBlock as A, openBlock as C, withCtx as M, resolveDynamicComponent as x, mergeProps as ee } from "vue";
5
5
  import { cloneDeep as re } from "lodash-es";
6
6
  import "zod";
7
- function g(r) {
8
- const e = L(r), t = Z(e);
9
- return re(t);
7
+ function g(t) {
8
+ const e = L(t), r = Z(e);
9
+ return re(r);
10
10
  }
11
- function K(r) {
12
- return r === "" ? [] : r.split(/\s*\.\s*/).filter(Boolean);
11
+ function I(t) {
12
+ return t === "" ? [] : t.split(/\s*\.\s*/).filter(Boolean);
13
13
  }
14
- function _(r, e) {
15
- return (Array.isArray(e) ? e : K(e)).reduce(
14
+ function P(t, e) {
15
+ return (Array.isArray(e) ? e : I(e)).reduce(
16
16
  (s, o) => s == null ? void 0 : s[o],
17
- r
17
+ t
18
18
  );
19
19
  }
20
- function te(r, e, t) {
21
- const s = Array.isArray(e) ? e : K(e), o = s.at(-1);
22
- if (!o && D(r))
23
- r.value = t;
24
- else if (D(r)) {
25
- const n = s.slice(0, -1).reduce(
20
+ function te(t, e, r) {
21
+ const s = Array.isArray(e) ? e : I(e), o = s.at(-1);
22
+ if (o) {
23
+ const i = s.slice(0, -1).reduce(
26
24
  (p, h) => p[h],
27
25
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
- d(r)
26
+ d(t)
29
27
  );
30
- n[o] = t;
31
- } else
32
- return;
28
+ i[o] = r;
29
+ } else {
30
+ if (!O(t))
31
+ return;
32
+ t.value = r;
33
+ }
33
34
  }
34
- const U = (r, e) => c({
35
+ const K = (t, e) => u({
35
36
  get() {
36
- return _(d(r), d(e));
37
+ return P(d(t), d(e));
37
38
  },
38
- set(t) {
39
- te(r, d(e), t);
39
+ set(r) {
40
+ te(t, d(e), r);
40
41
  }
41
42
  });
42
- function R(r, e) {
43
- return !r && !e ? "" : !r && e ? e : !e && r ? r : `${r}.${e}`;
43
+ function R(t, e) {
44
+ return !t && !e ? "" : !t && e ? e : !e && t ? t : `${t}.${e}`;
44
45
  }
45
- function se(r, e) {
46
+ function se(t, e) {
46
47
  if (!e)
47
- return r;
48
- const t = `${e}.`, s = Object.fromEntries(
49
- Object.entries(r.propertyErrors).filter(([o]) => o.startsWith(t)).map(
50
- ([o, n]) => [o.slice(t.length), n]
48
+ return t;
49
+ const r = `${e}.`, s = Object.fromEntries(
50
+ Object.entries(t.propertyErrors).filter(([o]) => o.startsWith(r)).map(
51
+ ([o, i]) => [o.slice(r.length), i]
51
52
  )
52
53
  );
53
54
  return {
54
- general: r.general,
55
+ general: t.general,
55
56
  // Keep general errors
56
57
  propertyErrors: s
57
58
  };
@@ -68,59 +69,62 @@ class ae {
68
69
  this.rc > 0 && (this.rc -= 1, this.rc === 0 && this.drop && this.drop());
69
70
  }
70
71
  }
71
- function oe(r) {
72
- const e = P({
73
- value: r.value,
74
- path: r.path,
75
- initialValue: c(() => Object.freeze(g(r.initialValue))),
76
- errors: r.errors,
72
+ function oe(t) {
73
+ const e = D({
74
+ value: t.value,
75
+ path: t.path,
76
+ initialValue: u(() => Object.freeze(g(t.initialValue))),
77
+ errors: t.errors,
77
78
  touched: !1
78
- }), t = c(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (a) => {
79
- e.value = a;
79
+ }), r = u(() => JSON.stringify(e.value) !== JSON.stringify(e.initialValue)), s = (c) => {
80
+ e.value = c;
80
81
  }, o = () => {
81
82
  e.touched = !0;
82
- }, n = () => {
83
+ }, i = () => {
83
84
  }, p = () => {
84
85
  e.value = g(e.initialValue), e.touched = !1, e.errors = [];
85
- }, h = (a) => {
86
- e.errors = a;
87
- }, V = () => {
86
+ }, h = (c) => {
87
+ r.value || s(g(c)), e.initialValue = c;
88
+ }, V = (c) => {
89
+ e.errors = c;
90
+ }, n = () => {
88
91
  e.errors = [];
89
- }, i = W(e);
92
+ }, a = W(e);
90
93
  return {
91
- data: i.value,
92
- path: i.path,
93
- initialValue: i.initialValue,
94
- errors: i.errors,
95
- touched: i.touched,
96
- dirty: t,
94
+ data: a.value,
95
+ path: a.path,
96
+ initialValue: a.initialValue,
97
+ errors: a.errors,
98
+ touched: a.touched,
99
+ dirty: r,
97
100
  setData: s,
101
+ setInitialData: h,
98
102
  onBlur: o,
99
- onFocus: n,
103
+ onFocus: i,
100
104
  reset: p,
101
- setErrors: h,
102
- clearErrors: V
105
+ setErrors: V,
106
+ clearErrors: n
103
107
  };
104
108
  }
105
- function ne(r, e) {
106
- const t = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
107
- const u = d(a.path);
108
- s.set(u, a);
109
- }, n = (a) => {
109
+ function ie(t, e) {
110
+ const r = /* @__PURE__ */ new Map(), s = q(/* @__PURE__ */ new Map()), o = (a) => {
111
+ const c = d(a.path);
112
+ s.set(c, a);
113
+ }, i = (a) => {
110
114
  s.delete(a);
111
115
  }, p = (a) => {
112
- var u;
113
- t.has(a) ? (u = t.get(a)) == null || u.inc() : t.set(a, new ae(() => n(a)));
116
+ var c;
117
+ r.has(a) ? (c = r.get(a)) == null || c.inc() : r.set(a, new ae(() => i(a)));
114
118
  }, h = (a) => {
115
- var u;
116
- t.has(a) && ((u = t.get(a)) == null || u.dec());
119
+ var c;
120
+ r.has(a) && ((c = r.get(a)) == null || c.dec());
117
121
  }, V = (a) => {
118
122
  if (!s.has(a)) {
119
123
  const v = oe({
120
124
  path: a,
121
- value: U(S(r, "data"), a),
122
- initialValue: c(() => _(r.initialData, a)),
123
- errors: c({
125
+ value: K(_(t, "data"), a),
126
+ initialValue: u(() => P(t.initialData, a)),
127
+ errors: u({
124
128
  get() {
125
129
  return e.errors.value.propertyErrors[a] || [];
126
130
  },
@@ -131,84 +135,84 @@ function ne(r, e) {
131
135
  });
132
136
  o(v);
133
137
  }
134
- const u = s.get(a);
138
+ const c = s.get(a);
135
139
  return p(a), H(() => {
136
140
  h(a);
137
- }), u;
138
- }, i = (a) => V(a.path);
141
+ }), c;
142
+ }, n = (a) => V(a.path);
139
143
  return {
140
- fields: c(() => [...s.values()]),
144
+ fields: u(() => [...s.values()]),
141
145
  getField: V,
142
146
  registerField: o,
143
- deregisterField: n,
144
- defineField: i
147
+ deregisterField: i,
148
+ defineField: n
145
149
  };
146
150
  }
147
- function ie(r) {
148
- const e = c(() => r.fields.value.some((s) => d(s.dirty))), t = c(() => r.fields.value.some((s) => d(s.touched)));
151
+ function ne(t) {
152
+ const e = u(() => t.fields.value.some((s) => d(s.dirty))), r = u(() => t.fields.value.some((s) => d(s.touched)));
149
153
  return {
150
154
  isDirty: e,
151
- isTouched: t
155
+ isTouched: r
152
156
  };
153
157
  }
154
- function le(r) {
155
- return r.filter(
156
- (e, t, s) => s.indexOf(e) === t
158
+ function le(t) {
159
+ return t.filter(
160
+ (e, r, s) => s.indexOf(e) === r
157
161
  );
158
162
  }
159
- function I(...r) {
160
- return r.slice(1).reduce((e, t) => {
161
- if (!e && !t)
163
+ function U(...t) {
164
+ return t.slice(1).reduce((e, r) => {
165
+ if (!e && !r)
162
166
  return;
163
- const s = ((t == null ? void 0 : t.length) ?? 0) > 0;
164
- if (!e && ((t == null ? void 0 : t.length) ?? 0) > 0)
165
- return t;
167
+ const s = ((r == null ? void 0 : r.length) ?? 0) > 0;
168
+ if (!e && ((r == null ? void 0 : r.length) ?? 0) > 0)
169
+ return r;
166
170
  if (!s)
167
171
  return e;
168
- const o = (e ?? []).concat(t);
172
+ const o = (e ?? []).concat(r);
169
173
  return le(o);
170
- }, r[0]);
174
+ }, t[0]);
171
175
  }
172
- function ce(...r) {
173
- return r.map((t) => Object.keys(t)).flat().reduce((t, s) => {
174
- const o = r.map((n) => n[s]).filter(Boolean);
176
+ function ce(...t) {
177
+ return t.map((r) => Object.keys(r)).flat().reduce((r, s) => {
178
+ const o = t.map((i) => i[s]).filter(Boolean);
175
179
  return {
176
- ...t,
177
- [s]: I(...o)
180
+ ...r,
181
+ [s]: U(...o)
178
182
  };
179
183
  }, {});
180
184
  }
181
- function O(...r) {
182
- if (!r.length)
185
+ function S(...t) {
186
+ if (!t.length)
183
187
  return {
184
188
  general: [],
185
189
  propertyErrors: {}
186
190
  };
187
- const e = r[0];
188
- return r.length === 1 ? e : r.slice(1).reduce(
189
- (t, s) => ({
190
- general: I(t.general, s.general),
191
- propertyErrors: ce(t.propertyErrors ?? {}, s.propertyErrors ?? {})
191
+ const e = t[0];
192
+ return t.length === 1 ? e : t.slice(1).reduce(
193
+ (r, s) => ({
194
+ general: U(r.general, s.general),
195
+ propertyErrors: ce(r.propertyErrors ?? {}, s.propertyErrors ?? {})
192
196
  }),
193
197
  e
194
198
  );
195
199
  }
196
- function N(r) {
200
+ function N(t) {
197
201
  var s;
198
- const e = (((s = r.general) == null ? void 0 : s.length) ?? 0) > 0, t = Object.entries(r.propertyErrors).filter(([, o]) => o == null ? void 0 : o.length).length > 0;
199
- return e || t;
202
+ const e = (((s = t.general) == null ? void 0 : s.length) ?? 0) > 0, r = Object.entries(t.propertyErrors).filter(([, o]) => o == null ? void 0 : o.length).length > 0;
203
+ return e || r;
200
204
  }
201
- function ue(r) {
202
- const e = r.issues.filter((s) => s.path.length === 0).map((s) => s.message), t = r.issues.filter((s) => s.path.length > 0).reduce((s, o) => {
203
- const n = o.path.join(".");
205
+ function ue(t) {
206
+ const e = t.issues.filter((s) => s.path.length === 0).map((s) => s.message), r = t.issues.filter((s) => s.path.length > 0).reduce((s, o) => {
207
+ const i = o.path.join(".");
204
208
  return {
205
209
  ...s,
206
- [n]: [...s[n] ?? [], o.message]
210
+ [i]: [...s[i] ?? [], o.message]
207
211
  };
208
212
  }, {});
209
213
  return {
210
214
  general: e,
211
- propertyErrors: t
215
+ propertyErrors: r
212
216
  };
213
217
  }
214
218
  const m = {
@@ -225,10 +229,10 @@ class de {
225
229
  async validate(e) {
226
230
  if (!this.schema)
227
231
  return m;
228
- const t = await this.schema.safeParseAsync(e);
229
- if (t.success)
232
+ const r = await this.schema.safeParseAsync(e);
233
+ if (r.success)
230
234
  return m;
231
- const s = ue(t.error);
235
+ const s = ue(r.error);
232
236
  return {
233
237
  isValid: !1,
234
238
  errors: {
@@ -246,13 +250,13 @@ class fe {
246
250
  if (!this.validateFn)
247
251
  return m;
248
252
  try {
249
- const t = await this.validateFn(e);
250
- return t.isValid ? m : t;
251
- } catch (t) {
253
+ const r = await this.validateFn(e);
254
+ return r.isValid ? m : r;
255
+ } catch (r) {
252
256
  return {
253
257
  isValid: !1,
254
258
  errors: {
255
- general: [t.message || "Validation error"],
259
+ general: [r.message || "Validation error"],
256
260
  propertyErrors: {}
257
261
  }
258
262
  };
@@ -260,89 +264,89 @@ class fe {
260
264
  }
261
265
  }
262
266
  class pe {
263
- constructor(e, t) {
267
+ constructor(e, r) {
264
268
  E(this, "schemaValidator");
265
269
  E(this, "functionValidator");
266
- this.schema = e, this.validateFn = t, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
270
+ this.schema = e, this.validateFn = r, this.schemaValidator = new de(this.schema), this.functionValidator = new fe(this.validateFn);
267
271
  }
268
272
  async validate(e) {
269
- const [t, s] = await Promise.all([
273
+ const [r, s] = await Promise.all([
270
274
  this.schemaValidator.validate(e),
271
275
  this.functionValidator.validate(e)
272
276
  ]);
273
277
  return {
274
- isValid: t.isValid && s.isValid,
275
- errors: O(t.errors, s.errors)
278
+ isValid: r.isValid && s.isValid,
279
+ errors: S(r.errors, s.errors)
276
280
  };
277
281
  }
278
282
  }
279
- function b(r) {
280
- return c(() => new pe(
281
- d(r.schema),
282
- d(r.validateFn)
283
+ function b(t) {
284
+ return u(() => new pe(
285
+ d(t.schema),
286
+ d(t.validateFn)
283
287
  ));
284
288
  }
285
- function he(r, e) {
286
- const t = P({
289
+ function he(t, e) {
290
+ const r = D({
287
291
  validators: k([b(e)]),
288
292
  isValidated: !1,
289
293
  errors: d(e.errors) ?? m.errors
290
- }), s = (i = m.errors) => {
291
- t.errors = O(d(e.errors) ?? m.errors, i);
294
+ }), s = (n = m.errors) => {
295
+ r.errors = S(d(e.errors) ?? m.errors, n);
292
296
  };
293
297
  w(() => d(e.errors), async () => {
294
- if (t.isValidated) {
295
- const i = await n();
296
- s(i.errors);
298
+ if (r.isValidated) {
299
+ const n = await i();
300
+ s(n.errors);
297
301
  } else
298
302
  s();
299
303
  }, { immediate: !0 }), w(
300
- [() => t.validators],
301
- async (i) => {
302
- if (t.isValidated)
303
- if (i) {
304
- const a = await n();
305
- t.errors = a.errors;
304
+ [() => r.validators],
305
+ async (n) => {
306
+ if (r.isValidated)
307
+ if (n) {
308
+ const a = await i();
309
+ r.errors = a.errors;
306
310
  } else
307
- t.errors = m.errors;
311
+ r.errors = m.errors;
308
312
  },
309
313
  { immediate: !0 }
310
- ), w(() => r.data, () => {
311
- t.isValidated && p();
314
+ ), w(() => t.data, () => {
315
+ r.isValidated && p();
312
316
  });
313
- const o = (i) => {
314
- const a = D(i) ? i : b(i);
315
- return t.validators.push(a), Q() && X(() => {
316
- t.validators = t.validators.filter(
317
- (u) => u !== a
317
+ const o = (n) => {
318
+ const a = O(n) ? n : b(n);
319
+ return r.validators.push(a), Q() && X(() => {
320
+ r.validators = r.validators.filter(
321
+ (c) => c !== a
318
322
  );
319
323
  }), a;
320
324
  };
321
- async function n() {
322
- const i = await Promise.all(
323
- t.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(r.data))
324
- ), a = i.every((v) => v.isValid);
325
- let { errors: u } = m;
325
+ async function i() {
326
+ const n = await Promise.all(
327
+ r.validators.filter((v) => d(v) !== void 0).map((v) => d(v).validate(t.data))
328
+ ), a = n.every((v) => v.isValid);
329
+ let { errors: c } = m;
326
330
  if (!a) {
327
- const v = i.map((F) => F.errors);
328
- u = O(...v);
331
+ const v = n.map((F) => F.errors);
332
+ c = S(...v);
329
333
  }
330
334
  return {
331
- errors: u,
335
+ errors: c,
332
336
  isValid: a
333
337
  };
334
338
  }
335
339
  const p = async () => {
336
- const i = await n();
337
- return s(i.errors), t.isValidated = !0, {
338
- isValid: !N(i.errors),
339
- errors: t.errors
340
+ const n = await i();
341
+ return s(n.errors), r.isValidated = !0, {
342
+ isValid: !N(n.errors),
343
+ errors: r.errors
340
344
  };
341
- }, h = c(() => !N(t.errors)), V = () => {
342
- t.isValidated = !1, t.errors = d(e.errors) ?? m.errors;
345
+ }, h = u(() => !N(r.errors)), V = () => {
346
+ r.isValidated = !1, r.errors = d(e.errors) ?? m.errors;
343
347
  };
344
348
  return {
345
- ...W(t),
349
+ ...W(r),
346
350
  validateForm: p,
347
351
  defineValidator: o,
348
352
  isValid: h,
@@ -350,51 +354,51 @@ function he(r, e) {
350
354
  };
351
355
  }
352
356
  class ve {
353
- constructor(e, t) {
354
- this.path = e, this.validator = t;
357
+ constructor(e, r) {
358
+ this.path = e, this.validator = r;
355
359
  }
356
360
  async validate(e) {
357
- const t = _(e, this.path);
361
+ const r = P(e, this.path);
358
362
  if (!this.validator)
359
363
  return m;
360
- const s = await this.validator.validate(t);
364
+ const s = await this.validator.validate(r);
361
365
  return {
362
366
  isValid: s.isValid,
363
367
  errors: {
364
368
  general: s.errors.general || [],
365
369
  propertyErrors: s.errors.propertyErrors ? Object.fromEntries(
366
- Object.entries(s.errors.propertyErrors).map(([o, n]) => [
370
+ Object.entries(s.errors.propertyErrors).map(([o, i]) => [
367
371
  R(this.path, o),
368
- n
372
+ i
369
373
  ])
370
374
  ) : {}
371
375
  }
372
376
  };
373
377
  }
374
378
  }
375
- function me(r, e, t) {
376
- const s = U(r.data, e), o = c(() => _(r.initialData.value, e)), n = (l) => ({
379
+ function me(t, e, r) {
380
+ const s = K(t.data, e), o = u(() => P(t.initialData.value, e)), i = (l) => ({
377
381
  ...l,
378
- path: c(() => d(l.path).replace(e + ".", "")),
382
+ path: u(() => d(l.path).replace(e + ".", "")),
379
383
  setData: (f) => {
380
384
  l.setData(f);
381
385
  }
382
386
  }), p = (l) => {
383
- const f = R(e, l), y = r.getField(f);
384
- return y ? n(y) : {};
387
+ const f = R(e, l), y = t.getField(f);
388
+ return y ? i(y) : {};
385
389
  }, h = (l) => {
386
- const f = R(e, l.path), y = r.defineField({
390
+ const f = R(e, l.path), y = t.defineField({
387
391
  ...l,
388
392
  path: f
389
393
  });
390
- return n(y);
391
- }, V = c(() => r.fields.value.filter((l) => {
394
+ return i(y);
395
+ }, V = u(() => t.fields.value.filter((l) => {
392
396
  const f = l.path.value;
393
397
  return f.startsWith(e + ".") || f === e;
394
- }).map((l) => n(l))), i = () => r.fields.value.filter((l) => {
398
+ }).map((l) => i(l))), n = () => t.fields.value.filter((l) => {
395
399
  const f = l.path.value;
396
400
  return f.startsWith(e + ".") || f === e;
397
- }), a = c(() => i().some((l) => l.dirty.value)), u = c(() => i().some((l) => l.touched.value)), v = c(() => r.isValid.value), F = c(() => r.isValidated.value), J = c(() => se(d(r.errors), e));
401
+ }), a = u(() => n().some((l) => l.dirty.value)), c = u(() => n().some((l) => l.touched.value)), v = u(() => t.isValid.value), F = u(() => t.isValidated.value), J = u(() => se(d(t.errors), e));
398
402
  return {
399
403
  data: s,
400
404
  fields: V,
@@ -402,53 +406,53 @@ function me(r, e, t) {
402
406
  defineField: h,
403
407
  getField: p,
404
408
  isDirty: a,
405
- isTouched: u,
409
+ isTouched: c,
406
410
  isValid: v,
407
411
  isValidated: F,
408
412
  errors: J,
409
413
  defineValidator: (l) => {
410
- const f = D(l) ? l : b(l), y = c(
414
+ const f = O(l) ? l : b(l), y = u(
411
415
  () => new ve(e, d(f))
412
416
  );
413
- return r.defineValidator(y), f;
417
+ return t.defineValidator(y), f;
414
418
  },
415
- reset: () => i().forEach((l) => l.reset()),
416
- validateForm: () => r.validateForm(),
419
+ reset: () => n().forEach((l) => l.reset()),
420
+ validateForm: () => t.validateForm(),
417
421
  getSubForm: (l, f) => {
418
422
  const y = R(e, l);
419
- return r.getSubForm(
423
+ return t.getSubForm(
420
424
  y,
421
425
  f
422
426
  );
423
427
  }
424
428
  };
425
429
  }
426
- function Pe(r) {
427
- const e = c(() => Object.freeze(g(r.initialData))), t = k(g(e)), s = P({
430
+ function Pe(t) {
431
+ const e = u(() => Object.freeze(g(t.initialData))), r = k(g(e)), s = D({
428
432
  initialData: e,
429
- data: t
433
+ data: r
430
434
  });
431
435
  w(e, (a) => {
432
436
  s.data = g(a);
433
437
  });
434
- const o = he(s, r), n = ne(s, o), p = ie(n), h = () => {
435
- t.value = g(e), o.reset(), n.fields.value.forEach(
438
+ const o = he(s, t), i = ie(s, o), p = ne(i), h = () => {
439
+ r.value = g(e), o.reset(), i.fields.value.forEach(
436
440
  (a) => a.reset()
437
441
  );
438
442
  };
439
- function V(a, u) {
440
- return me(i, a);
443
+ function V(a, c) {
444
+ return me(n, a);
441
445
  }
442
- const i = {
443
- ...n,
446
+ const n = {
447
+ ...i,
444
448
  ...o,
445
449
  ...p,
446
450
  reset: h,
447
451
  getSubForm: V,
448
- initialData: S(s, "initialData"),
449
- data: S(s, "data")
452
+ initialData: _(s, "initialData"),
453
+ data: _(s, "data")
450
454
  };
451
- return i;
455
+ return n;
452
456
  }
453
457
  const _e = /* @__PURE__ */ $({
454
458
  __name: "Field",
@@ -459,11 +463,11 @@ const _e = /* @__PURE__ */ $({
459
463
  path: {},
460
464
  errors: {}
461
465
  },
462
- setup(r) {
463
- const e = r, t = e.form.defineField({
466
+ setup(t) {
467
+ const e = t, r = e.form.defineField({
464
468
  path: e.path
465
- }), s = P(t);
466
- return (o, n) => j(o.$slots, "default", z(B(s)));
469
+ }), s = D(r);
470
+ return (o, i) => j(o.$slots, "default", z(B(s)));
467
471
  }
468
472
  }), Se = /* @__PURE__ */ $({
469
473
  inheritAttrs: !1,
@@ -474,16 +478,16 @@ const _e = /* @__PURE__ */ $({
474
478
  form: {},
475
479
  path: {}
476
480
  },
477
- setup(r) {
478
- return (e, t) => {
481
+ setup(t) {
482
+ return (e, r) => {
479
483
  const s = Y("Field");
480
484
  return C(), A(s, {
481
485
  form: e.form,
482
486
  path: e.path
483
487
  }, {
484
- default: M(({ errors: o, data: n, setData: p }) => [
488
+ default: M(({ errors: o, data: i, setData: p }) => [
485
489
  (C(), A(x(e.component), ee({ ...e.componentProps, ...e.$attrs }, {
486
- "model-value": n,
490
+ "model-value": i,
487
491
  errors: o,
488
492
  name: e.path,
489
493
  "onUpdate:modelValue": p
@@ -498,20 +502,20 @@ const _e = /* @__PURE__ */ $({
498
502
  }, 8, ["form", "path"]);
499
503
  };
500
504
  }
501
- }), Oe = /* @__PURE__ */ $({
505
+ }), be = /* @__PURE__ */ $({
502
506
  __name: "FormPart",
503
507
  props: {
504
508
  form: {},
505
509
  path: {}
506
510
  },
507
- setup(r) {
508
- const e = r, t = c(() => e.form.getSubForm(e.path));
509
- return (s, o) => j(s.$slots, "default", z(B({ subform: t.value })));
511
+ setup(t) {
512
+ const e = t, r = u(() => e.form.getSubForm(e.path));
513
+ return (s, o) => j(s.$slots, "default", z(B({ subform: r.value })));
510
514
  }
511
515
  });
512
516
  export {
513
517
  _e as Field,
514
518
  Se as FormFieldWrapper,
515
- Oe as FormPart,
519
+ be as FormPart,
516
520
  Pe as useForm
517
521
  };
@@ -13,6 +13,7 @@ export interface FormField<T, P extends string> {
13
13
  touched: Ref<boolean>;
14
14
  dirty: Ref<boolean>;
15
15
  setData: (newData: T) => void;
16
+ setInitialData: (newData: T) => void;
16
17
  onBlur: () => void;
17
18
  onFocus: () => void;
18
19
  reset: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnovu/kit-vue-forms",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -41,6 +41,17 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
41
41
  state.errors = []
42
42
  }
43
43
 
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
+ const setInitialData = (newData: T): void => {
49
+ if (!dirty.value) {
50
+ setData(cloneRefValue(newData))
51
+ }
52
+ state.initialValue = newData
53
+ }
54
+
44
55
  const setErrors = (newErrors: ValidationErrorMessage[]): void => {
45
56
  state.errors = newErrors
46
57
  }
@@ -59,6 +70,7 @@ export function useField<T, K extends string>(options: UseFieldOptions<T, K>): F
59
70
  touched: refs.touched as FormField<T, K>['touched'],
60
71
  dirty,
61
72
  setData,
73
+ setInitialData,
62
74
  onBlur,
63
75
  onFocus,
64
76
  reset,
package/src/types/form.ts CHANGED
@@ -15,6 +15,7 @@ export interface FormField<T, P extends string> {
15
15
  touched: Ref<boolean>
16
16
  dirty: Ref<boolean>
17
17
  setData: (newData: T) => void
18
+ setInitialData: (newData: T) => void
18
19
  onBlur: () => void
19
20
  onFocus: () => void
20
21
  reset: () => void
package/src/utils/path.ts CHANGED
@@ -22,11 +22,13 @@ export function setNestedValue<T, K extends Paths<T>>(obj: MaybeRef<T>, path: K
22
22
 
23
23
  const lastKey = keys.at(-1)!
24
24
 
25
- if (!lastKey && isRef(obj)) {
25
+ if (!lastKey) {
26
+ if (!isRef(obj)) {
27
+ // We cannot do anything here as we have nothing we can assign to
28
+ return
29
+ }
30
+
26
31
  obj.value = value
27
- } else if (!isRef(obj)) {
28
- // We cannot do anything here as we have nothing we can assign to
29
- return
30
32
  } else {
31
33
  const target = keys
32
34
  .slice(0, -1)