@formisch/solid 0.3.1 → 0.5.0

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/dev.jsx CHANGED
@@ -13,16 +13,21 @@ function createSignal(initialValue) {
13
13
  }
14
14
  };
15
15
  }
16
- function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
16
+ function initializeFieldStore(internalFieldStore, schema, initialInput, path, nullish = false) {
17
17
  if (framework === "qwik" && schema.type === "lazy" || schema.type === "object_with_rest" || schema.type === "record" || schema.type === "tuple_with_rest" || schema.type === "promise") throw new Error(`"${schema.type}" schema is not supported`);
18
18
  else if (schema.type === "lazy") initializeFieldStore(internalFieldStore, schema.getter(void 0), initialInput, path);
19
- else if (schema.type === "exact_optional" || schema.type === "non_nullable" || schema.type === "non_nullish" || schema.type === "non_optional" || schema.type === "nullable" || schema.type === "nullish" || schema.type === "optional" || schema.type === "undefinedable") initializeFieldStore(internalFieldStore, schema.wrapped, initialInput ?? v.getDefault(schema), path);
19
+ else if (schema.type === "exact_optional" || schema.type === "nullable" || schema.type === "nullish" || schema.type === "optional" || schema.type === "undefinedable") initializeFieldStore(internalFieldStore, schema.wrapped, initialInput ?? v.getDefault(schema), path, true);
20
+ else if (schema.type === "non_nullable" || schema.type === "non_nullish" || schema.type === "non_optional") initializeFieldStore(internalFieldStore, schema.wrapped, initialInput, path);
20
21
  else if (schema.type === "intersect" || schema.type === "union" || schema.type === "variant") for (const schemaOption of schema.options) initializeFieldStore(internalFieldStore, schemaOption, initialInput, path);
21
22
  else {
22
23
  internalFieldStore.schema = schema;
23
24
  internalFieldStore.name = JSON.stringify(path);
24
- internalFieldStore.elements = [];
25
+ const initialElements = [];
26
+ internalFieldStore.initialElements = initialElements;
27
+ internalFieldStore.elements = initialElements;
25
28
  internalFieldStore.errors = createSignal(null);
29
+ internalFieldStore.isTouched = createSignal(false);
30
+ internalFieldStore.isDirty = createSignal(false);
26
31
  if (schema.type === "array" || schema.type === "loose_tuple" || schema.type === "strict_tuple" || schema.type === "tuple") {
27
32
  if (internalFieldStore.kind && internalFieldStore.kind !== "array") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "array"`);
28
33
  internalFieldStore.kind = "array";
@@ -41,12 +46,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
41
46
  initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput?.[index], path);
42
47
  path.pop();
43
48
  }
49
+ const arrayInput = nullish && initialInput == null ? initialInput : true;
50
+ internalFieldStore.initialInput = createSignal(arrayInput);
51
+ internalFieldStore.startInput = createSignal(arrayInput);
52
+ internalFieldStore.input = createSignal(arrayInput);
44
53
  const initialItems = internalFieldStore.children.map(createId);
45
54
  internalFieldStore.initialItems = createSignal(initialItems);
46
55
  internalFieldStore.startItems = createSignal(initialItems);
47
56
  internalFieldStore.items = createSignal(initialItems);
48
- internalFieldStore.isTouched = createSignal(false);
49
- internalFieldStore.isDirty = createSignal(false);
50
57
  }
51
58
  } else if (schema.type === "loose_object" || schema.type === "object" || schema.type === "strict_object") {
52
59
  if (internalFieldStore.kind && internalFieldStore.kind !== "object") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "object"`);
@@ -59,6 +66,10 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
59
66
  initializeFieldStore(internalFieldStore.children[key], schema.entries[key], initialInput?.[key], path);
60
67
  path.pop();
61
68
  }
69
+ const objectInput = nullish && initialInput == null ? initialInput : true;
70
+ internalFieldStore.initialInput = createSignal(objectInput);
71
+ internalFieldStore.startInput = createSignal(objectInput);
72
+ internalFieldStore.input = createSignal(objectInput);
62
73
  }
63
74
  } else {
64
75
  internalFieldStore.kind = "value";
@@ -66,8 +77,6 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
66
77
  internalFieldStore.initialInput = createSignal(initialInput);
67
78
  internalFieldStore.startInput = createSignal(initialInput);
68
79
  internalFieldStore.input = createSignal(initialInput);
69
- internalFieldStore.isTouched = createSignal(false);
70
- internalFieldStore.isDirty = createSignal(false);
71
80
  }
72
81
  }
73
82
  }
@@ -75,10 +84,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
75
84
  function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
76
85
  batch(() => {
77
86
  untrack(() => {
87
+ toInternalFieldStore.elements = fromInternalFieldStore.elements;
88
+ toInternalFieldStore.errors.value = fromInternalFieldStore.errors.value;
89
+ toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
90
+ toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
91
+ toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
92
+ toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
78
93
  if (fromInternalFieldStore.kind === "array" && toInternalFieldStore.kind === "array") {
79
94
  const fromItems = fromInternalFieldStore.items.value;
80
- toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
81
- toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
82
95
  toInternalFieldStore.startItems.value = fromInternalFieldStore.startItems.value;
83
96
  toInternalFieldStore.items.value = fromItems;
84
97
  let path;
@@ -93,22 +106,20 @@ function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
93
106
  copyItemState(fromInternalFieldStore.children[index], toInternalFieldStore.children[index]);
94
107
  }
95
108
  } else if (fromInternalFieldStore.kind === "object" && toInternalFieldStore.kind === "object") for (const key in fromInternalFieldStore.children) copyItemState(fromInternalFieldStore.children[key], toInternalFieldStore.children[key]);
96
- else if (fromInternalFieldStore.kind === "value" && toInternalFieldStore.kind === "value") {
97
- toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
98
- toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
99
- toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
100
- toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
101
- }
102
109
  });
103
110
  });
104
111
  }
105
112
  function resetItemState(internalFieldStore, initialInput) {
106
113
  batch(() => {
114
+ internalFieldStore.elements = [];
107
115
  internalFieldStore.errors.value = null;
108
- if (internalFieldStore.kind === "array") {
109
- internalFieldStore.isTouched.value = false;
110
- internalFieldStore.isDirty.value = false;
111
- if (initialInput) {
116
+ internalFieldStore.isTouched.value = false;
117
+ internalFieldStore.isDirty.value = false;
118
+ if (internalFieldStore.kind === "array" || internalFieldStore.kind === "object") {
119
+ const objectInput = initialInput == null ? initialInput : true;
120
+ internalFieldStore.startInput.value = objectInput;
121
+ internalFieldStore.input.value = objectInput;
122
+ if (internalFieldStore.kind === "array") if (initialInput) {
112
123
  const newItems = initialInput.map(createId);
113
124
  internalFieldStore.startItems.value = newItems;
114
125
  internalFieldStore.items.value = newItems;
@@ -117,10 +128,8 @@ function resetItemState(internalFieldStore, initialInput) {
117
128
  internalFieldStore.startItems.value = [];
118
129
  internalFieldStore.items.value = [];
119
130
  }
120
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) resetItemState(internalFieldStore.children[key], initialInput?.[key]);
121
- else {
122
- internalFieldStore.isTouched.value = false;
123
- internalFieldStore.isDirty.value = false;
131
+ else for (const key in internalFieldStore.children) resetItemState(internalFieldStore.children[key], initialInput?.[key]);
132
+ } else {
124
133
  internalFieldStore.startInput.value = initialInput;
125
134
  internalFieldStore.input.value = initialInput;
126
135
  }
@@ -129,15 +138,27 @@ function resetItemState(internalFieldStore, initialInput) {
129
138
  function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
130
139
  batch(() => {
131
140
  untrack(() => {
141
+ const tempElements = firstInternalFieldStore.elements;
142
+ firstInternalFieldStore.elements = secondInternalFieldStore.elements;
143
+ secondInternalFieldStore.elements = tempElements;
144
+ const tempErrors = firstInternalFieldStore.errors.value;
145
+ firstInternalFieldStore.errors.value = secondInternalFieldStore.errors.value;
146
+ secondInternalFieldStore.errors.value = tempErrors;
147
+ const tempStartInput = firstInternalFieldStore.startInput.value;
148
+ firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
149
+ secondInternalFieldStore.startInput.value = tempStartInput;
150
+ const tempInput = firstInternalFieldStore.input.value;
151
+ firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
152
+ secondInternalFieldStore.input.value = tempInput;
153
+ const tempIsTouched = firstInternalFieldStore.isTouched.value;
154
+ firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
155
+ secondInternalFieldStore.isTouched.value = tempIsTouched;
156
+ const tempIsDirty = firstInternalFieldStore.isDirty.value;
157
+ firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
158
+ secondInternalFieldStore.isDirty.value = tempIsDirty;
132
159
  if (firstInternalFieldStore.kind === "array" && secondInternalFieldStore.kind === "array") {
133
160
  const firstItems = firstInternalFieldStore.items.value;
134
161
  const secondItems = secondInternalFieldStore.items.value;
135
- const tempIsTouched = firstInternalFieldStore.isTouched.value;
136
- firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
137
- secondInternalFieldStore.isTouched.value = tempIsTouched;
138
- const tempIsDirty = firstInternalFieldStore.isDirty.value;
139
- firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
140
- secondInternalFieldStore.isDirty.value = tempIsDirty;
141
162
  const tempStartItems = firstInternalFieldStore.startItems.value;
142
163
  firstInternalFieldStore.startItems.value = secondInternalFieldStore.startItems.value;
143
164
  secondInternalFieldStore.startItems.value = tempStartItems;
@@ -164,33 +185,25 @@ function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
164
185
  swapItemState(firstInternalFieldStore.children[index], secondInternalFieldStore.children[index]);
165
186
  }
166
187
  } else if (firstInternalFieldStore.kind === "object" && secondInternalFieldStore.kind === "object") for (const key in firstInternalFieldStore.children) swapItemState(firstInternalFieldStore.children[key], secondInternalFieldStore.children[key]);
167
- else if (firstInternalFieldStore.kind === "value" && secondInternalFieldStore.kind === "value") {
168
- const tempIsTouched = firstInternalFieldStore.isTouched.value;
169
- firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
170
- secondInternalFieldStore.isTouched.value = tempIsTouched;
171
- const tempIsDirty = firstInternalFieldStore.isDirty.value;
172
- firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
173
- secondInternalFieldStore.isDirty.value = tempIsDirty;
174
- const tempStartInput = firstInternalFieldStore.startInput.value;
175
- firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
176
- secondInternalFieldStore.startInput.value = tempStartInput;
177
- const tempInput = firstInternalFieldStore.input.value;
178
- firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
179
- secondInternalFieldStore.input.value = tempInput;
180
- }
181
188
  });
182
189
  });
183
190
  }
184
191
  function getFieldInput(internalFieldStore) {
185
192
  if (internalFieldStore.kind === "array") {
186
- const value = [];
187
- for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = getFieldInput(internalFieldStore.children[index]);
188
- return value;
193
+ if (internalFieldStore.input.value) {
194
+ const value = [];
195
+ for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = getFieldInput(internalFieldStore.children[index]);
196
+ return value;
197
+ }
198
+ return internalFieldStore.input.value;
189
199
  }
190
200
  if (internalFieldStore.kind === "object") {
191
- const value = {};
192
- for (const key in internalFieldStore.children) value[key] = getFieldInput(internalFieldStore.children[key]);
193
- return value;
201
+ if (internalFieldStore.input.value) {
202
+ const value = {};
203
+ for (const key in internalFieldStore.children) value[key] = getFieldInput(internalFieldStore.children[key]);
204
+ return value;
205
+ }
206
+ return internalFieldStore.input.value;
194
207
  }
195
208
  return internalFieldStore.input.value;
196
209
  }
@@ -213,17 +226,16 @@ function getElementInput(element, internalFieldStore) {
213
226
  return element.value;
214
227
  }
215
228
  function getFieldBool(internalFieldStore, type) {
229
+ if (internalFieldStore[type].value) return true;
216
230
  if (internalFieldStore.kind === "array") {
217
- if (internalFieldStore[type].value) return true;
218
231
  for (let index = 0; index < internalFieldStore.items.value.length; index++) if (getFieldBool(internalFieldStore.children[index], type)) return true;
219
232
  return false;
220
233
  }
221
234
  if (internalFieldStore.kind == "object") {
222
- if (type === "errors" && internalFieldStore[type].value) return true;
223
235
  for (const key in internalFieldStore.children) if (getFieldBool(internalFieldStore.children[key], type)) return true;
224
236
  return false;
225
237
  }
226
- return !!internalFieldStore[type].value;
238
+ return false;
227
239
  }
228
240
  function getFieldStore(internalFormStore, path) {
229
241
  let internalFieldStore = internalFormStore;
@@ -239,38 +251,53 @@ function setFieldBool(internalFieldStore, type, bool) {
239
251
  else internalFieldStore[type].value = bool;
240
252
  });
241
253
  }
242
- function setFieldInput(internalFieldStore, input) {
243
- batch(() => {
244
- if (internalFieldStore.kind === "array") {
245
- const arrayInput = input ?? [];
246
- const items = untrack(() => internalFieldStore.items.value);
247
- if (arrayInput.length < items.length) internalFieldStore.items.value = items.slice(0, arrayInput.length);
248
- else if (arrayInput.length > items.length) {
249
- if (arrayInput.length > internalFieldStore.children.length) {
250
- const path = JSON.parse(internalFieldStore.name);
251
- for (let index = internalFieldStore.children.length; index < arrayInput.length; index++) {
252
- internalFieldStore.children[index] = {};
253
- path.push(index);
254
- initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], path);
255
- path.pop();
256
- }
254
+ function setNestedInput(internalFieldStore, input) {
255
+ internalFieldStore.isTouched.value = true;
256
+ if (internalFieldStore.kind === "array") {
257
+ const arrayInput = input ?? [];
258
+ const items = internalFieldStore.items.value;
259
+ if (arrayInput.length < items.length) internalFieldStore.items.value = items.slice(0, arrayInput.length);
260
+ else if (arrayInput.length > items.length) {
261
+ if (arrayInput.length > internalFieldStore.children.length) {
262
+ const path = JSON.parse(internalFieldStore.name);
263
+ for (let index = internalFieldStore.children.length; index < arrayInput.length; index++) {
264
+ internalFieldStore.children[index] = {};
265
+ path.push(index);
266
+ initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], path);
267
+ path.pop();
257
268
  }
258
- internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createId)];
259
269
  }
260
- for (let index = 0; index < items.length; index++) setFieldInput(internalFieldStore.children[index], arrayInput[index]);
261
- internalFieldStore.isDirty.value = untrack(() => internalFieldStore.startItems.value).length !== items.length;
262
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setFieldInput(internalFieldStore.children[key], input?.[key]);
263
- else {
264
- internalFieldStore.input.value = input;
265
- internalFieldStore.isTouched.value = true;
266
- const startInput = untrack(() => internalFieldStore.startInput.value);
267
- internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
270
+ internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createId)];
268
271
  }
272
+ for (let index = 0; index < items.length; index++) setNestedInput(internalFieldStore.children[index], arrayInput[index]);
273
+ internalFieldStore.input.value = input == null ? input : true;
274
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== items.length;
275
+ } else if (internalFieldStore.kind === "object") {
276
+ for (const key in internalFieldStore.children) setNestedInput(internalFieldStore.children[key], input?.[key]);
277
+ internalFieldStore.input.value = input == null ? input : true;
278
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value;
279
+ } else {
280
+ internalFieldStore.input.value = input;
281
+ const startInput = internalFieldStore.startInput.value;
282
+ internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
283
+ }
284
+ }
285
+ function setFieldInput(internalFormStore, path, input) {
286
+ batch(() => {
287
+ untrack(() => {
288
+ let internalFieldStore = internalFormStore;
289
+ for (let index = 0; index < path.length; index++) {
290
+ internalFieldStore = internalFieldStore.children[path[index]];
291
+ if (index < path.length - 1) internalFieldStore.input.value = true;
292
+ else setNestedInput(internalFieldStore, input);
293
+ }
294
+ });
269
295
  });
270
296
  }
271
297
  function setInitialFieldInput(internalFieldStore, initialInput) {
272
298
  batch(() => {
273
299
  if (internalFieldStore.kind === "array") {
300
+ internalFieldStore.input.value = initialInput == null ? initialInput : true;
274
301
  const initialArrayInput = initialInput ?? [];
275
302
  if (initialArrayInput.length > internalFieldStore.children.length) {
276
303
  const path = JSON.parse(internalFieldStore.name);
@@ -283,8 +310,10 @@ function setInitialFieldInput(internalFieldStore, initialInput) {
283
310
  }
284
311
  internalFieldStore.initialItems.value = initialArrayInput.map(createId);
285
312
  for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index], initialArrayInput[index]);
286
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setInitialFieldInput(internalFieldStore.children[key], initialInput?.[key]);
287
- else internalFieldStore.initialInput.value = initialInput;
313
+ } else if (internalFieldStore.kind === "object") {
314
+ internalFieldStore.input.value = initialInput == null ? initialInput : true;
315
+ for (const key in internalFieldStore.children) setInitialFieldInput(internalFieldStore.children[key], initialInput?.[key]);
316
+ } else internalFieldStore.initialInput.value = initialInput;
288
317
  });
289
318
  }
290
319
  function walkFieldStore(internalFieldStore, callback) {
@@ -345,8 +374,8 @@ async function validateFormInput(internalFormStore, config) {
345
374
  });
346
375
  return result;
347
376
  }
348
- function validateIfRequired(internalFormStore, internalFieldStore, validationModes) {
349
- if (validationModes === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
377
+ function validateIfRequired(internalFormStore, internalFieldStore, validationMode) {
378
+ if (validationMode === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
350
379
  }
351
380
  var INTERNAL = "~internal";
352
381
 
@@ -387,7 +416,12 @@ function handleSubmit(form, handler) {
387
416
  }
388
417
  function insert(form, config) {
389
418
  const internalFormStore = form[INTERNAL];
390
- const internalArrayStore = getFieldStore(internalFormStore, config.path);
419
+ let internalFieldStore = internalFormStore;
420
+ for (let index = 0; index < config.path.length; index++) {
421
+ internalFieldStore = internalFieldStore.children[config.path[index]];
422
+ if (index < config.path.length - 1) internalFieldStore.input.value = true;
423
+ }
424
+ const internalArrayStore = internalFieldStore;
391
425
  const items = untrack(() => internalArrayStore.items.value);
392
426
  const insertIndex = config.at === void 0 ? items.length : config.at;
393
427
  if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
@@ -401,6 +435,7 @@ function insert(form, config) {
401
435
  path.push(insertIndex);
402
436
  initializeFieldStore(internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, path);
403
437
  } else resetItemState(internalArrayStore.children[insertIndex], config.initialInput);
438
+ internalArrayStore.input.value = true;
404
439
  internalArrayStore.isTouched.value = true;
405
440
  internalArrayStore.isDirty.value = true;
406
441
  validateIfRequired(internalFormStore, internalArrayStore, "input");
@@ -460,17 +495,20 @@ function reset(form, config) {
460
495
  const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
461
496
  if (config?.initialInput) setInitialFieldInput(internalFieldStore, config.initialInput);
462
497
  walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
498
+ internalFieldStore$1.elements = internalFieldStore$1.initialElements;
463
499
  if (!config?.keepErrors) internalFieldStore$1.errors.value = null;
500
+ if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
501
+ internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
502
+ if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
464
503
  if (internalFieldStore$1.kind === "array") {
465
504
  internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
466
505
  if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
467
- if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
468
- internalFieldStore$1.isDirty.value = internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
469
- } else if (internalFieldStore$1.kind === "value") {
470
- internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
471
- if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
472
- if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
473
- internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
506
+ internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value || internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
507
+ } else if (internalFieldStore$1.kind === "object") internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
508
+ else {
509
+ const startInput = internalFieldStore$1.startInput.value;
510
+ const input = internalFieldStore$1.input.value;
511
+ internalFieldStore$1.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
474
512
  for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
475
513
  }
476
514
  });
@@ -487,9 +525,8 @@ function setErrors(form, config) {
487
525
  function setInput(form, config) {
488
526
  batch(() => {
489
527
  const internalFormStore = form[INTERNAL];
490
- const internalFieldStore = config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
491
- setFieldInput(internalFieldStore, config.input);
492
- validateIfRequired(internalFormStore, internalFieldStore, "input");
528
+ setFieldInput(internalFormStore, config.path ?? [], config.input);
529
+ validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
493
530
  });
494
531
  }
495
532
  function submit(form) {
@@ -575,9 +612,10 @@ function unwrap(value) {
575
612
 
576
613
  // src/primitives/useField/useField.ts
577
614
  function useField(form, config) {
615
+ const getPath = createMemo2(() => unwrap(config).path);
578
616
  const getInternalFormStore = createMemo2(() => unwrap(form)[INTERNAL]);
579
617
  const getInternalFieldStore = createMemo2(
580
- () => getFieldStore(getInternalFormStore(), unwrap(config).path)
618
+ () => getFieldStore(getInternalFormStore(), getPath())
581
619
  );
582
620
  const getInput2 = createMemo2(() => getFieldInput(getInternalFieldStore()));
583
621
  const getIsTouched = createMemo2(
@@ -591,7 +629,7 @@ function useField(form, config) {
591
629
  );
592
630
  return {
593
631
  get path() {
594
- return unwrap(config).path;
632
+ return getPath();
595
633
  },
596
634
  get input() {
597
635
  return getInput2();
@@ -634,7 +672,8 @@ function useField(form, config) {
634
672
  onInput(event) {
635
673
  const internalFieldStore = getInternalFieldStore();
636
674
  setFieldInput(
637
- internalFieldStore,
675
+ getInternalFormStore(),
676
+ getPath(),
638
677
  getElementInput(event.currentTarget, internalFieldStore)
639
678
  );
640
679
  validateIfRequired(getInternalFormStore(), internalFieldStore, "input");
package/dist/index.d.ts CHANGED
@@ -19,29 +19,34 @@ interface InternalBaseStore {
19
19
  kind: "array" | "object" | "value";
20
20
  name: string;
21
21
  schema: Schema;
22
+ initialElements: FieldElement[];
22
23
  elements: FieldElement[];
23
24
  errors: Signal<[string, ...string[]] | null>;
25
+ isTouched: Signal<boolean>;
26
+ isDirty: Signal<boolean>;
24
27
  }
25
28
  interface InternalArrayStore extends InternalBaseStore {
26
29
  kind: "array";
27
30
  children: InternalFieldStore[];
31
+ initialInput: Signal<true | null | undefined>;
32
+ startInput: Signal<true | null | undefined>;
33
+ input: Signal<true | null | undefined>;
28
34
  initialItems: Signal<string[]>;
29
35
  startItems: Signal<string[]>;
30
36
  items: Signal<string[]>;
31
- isTouched: Signal<boolean>;
32
- isDirty: Signal<boolean>;
33
37
  }
34
38
  interface InternalObjectStore extends InternalBaseStore {
35
39
  kind: "object";
36
40
  children: Record<string, InternalFieldStore>;
41
+ initialInput: Signal<true | null | undefined>;
42
+ startInput: Signal<true | null | undefined>;
43
+ input: Signal<true | null | undefined>;
37
44
  }
38
45
  interface InternalValueStore extends InternalBaseStore {
39
46
  kind: "value";
40
47
  initialInput: Signal<unknown>;
41
48
  startInput: Signal<unknown>;
42
49
  input: Signal<unknown>;
43
- isTouched: Signal<boolean>;
44
- isDirty: Signal<boolean>;
45
50
  }
46
51
  type InternalFieldStore = InternalArrayStore | InternalObjectStore | InternalValueStore;
47
52
  //#endregion
@@ -393,4 +398,4 @@ interface UseFieldArrayConfig<TSchema extends Schema = Schema, TFieldArrayPath e
393
398
  }
394
399
  declare function useFieldArray<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: MaybeGetter<FormStore<TSchema>>, config: MaybeGetter<UseFieldArrayConfig<TSchema, TFieldArrayPath>>): FieldArrayStore<TSchema, TFieldArrayPath>;
395
400
  //#endregion
396
- export { Field, FieldArray, FieldArrayProps, FieldArrayStore, FieldElementProps, FieldProps, FieldStore, FocusFieldConfig, Form, FormProps, FormStore, GetFieldErrorsConfig, GetFieldInputConfig, GetFormErrorsConfig, GetFormInputConfig, InsertConfig, MaybeGetter, MoveConfig, RemoveConfig, ReplaceConfig, ResetFieldConfig, ResetFormConfig, SetFieldErrorsConfig, SetFieldInputConfig, SetFormErrorsConfig, SetFormInputConfig, type SubmitHandler, SwapConfig, UseFieldArrayConfig, UseFieldConfig, ValidateFormConfig, createForm, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, validate };
401
+ export { type DeepPartial, Field, FieldArray, FieldArrayProps, FieldArrayStore, type FieldElement, FieldElementProps, FieldProps, FieldStore, FocusFieldConfig, Form, type FormConfig, FormProps, FormStore, GetFieldErrorsConfig, GetFieldInputConfig, GetFormErrorsConfig, GetFormInputConfig, InsertConfig, MaybeGetter, MoveConfig, type PartialValues, type PathValue, RemoveConfig, ReplaceConfig, type RequiredPath, ResetFieldConfig, ResetFormConfig, type Schema, SetFieldErrorsConfig, SetFieldInputConfig, SetFormErrorsConfig, SetFormInputConfig, type SubmitHandler, SwapConfig, UseFieldArrayConfig, UseFieldConfig, type ValidArrayPath, type ValidPath, ValidateFormConfig, type ValidationMode, createForm, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, validate };