@formisch/solid 0.3.1 → 0.4.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.js +133 -94
- package/dist/dev.jsx +133 -94
- package/dist/index.d.ts +9 -4
- package/dist/index.js +133 -94
- package/dist/index.jsx +133 -94
- package/package.json +3 -3
package/dist/dev.js
CHANGED
|
@@ -15,16 +15,21 @@ function createSignal(initialValue) {
|
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
-
function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
18
|
+
function initializeFieldStore(internalFieldStore, schema, initialInput, path, nullish = false) {
|
|
19
19
|
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`);
|
|
20
20
|
else if (schema.type === "lazy") initializeFieldStore(internalFieldStore, schema.getter(void 0), initialInput, path);
|
|
21
|
-
else if (schema.type === "exact_optional" || schema.type === "
|
|
21
|
+
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);
|
|
22
|
+
else if (schema.type === "non_nullable" || schema.type === "non_nullish" || schema.type === "non_optional") initializeFieldStore(internalFieldStore, schema.wrapped, initialInput, path);
|
|
22
23
|
else if (schema.type === "intersect" || schema.type === "union" || schema.type === "variant") for (const schemaOption of schema.options) initializeFieldStore(internalFieldStore, schemaOption, initialInput, path);
|
|
23
24
|
else {
|
|
24
25
|
internalFieldStore.schema = schema;
|
|
25
26
|
internalFieldStore.name = JSON.stringify(path);
|
|
26
|
-
|
|
27
|
+
const initialElements = [];
|
|
28
|
+
internalFieldStore.initialElements = initialElements;
|
|
29
|
+
internalFieldStore.elements = initialElements;
|
|
27
30
|
internalFieldStore.errors = createSignal(null);
|
|
31
|
+
internalFieldStore.isTouched = createSignal(false);
|
|
32
|
+
internalFieldStore.isDirty = createSignal(false);
|
|
28
33
|
if (schema.type === "array" || schema.type === "loose_tuple" || schema.type === "strict_tuple" || schema.type === "tuple") {
|
|
29
34
|
if (internalFieldStore.kind && internalFieldStore.kind !== "array") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "array"`);
|
|
30
35
|
internalFieldStore.kind = "array";
|
|
@@ -43,12 +48,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
43
48
|
initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput?.[index], path);
|
|
44
49
|
path.pop();
|
|
45
50
|
}
|
|
51
|
+
const arrayInput = nullish && initialInput == null ? initialInput : true;
|
|
52
|
+
internalFieldStore.initialInput = createSignal(arrayInput);
|
|
53
|
+
internalFieldStore.startInput = createSignal(arrayInput);
|
|
54
|
+
internalFieldStore.input = createSignal(arrayInput);
|
|
46
55
|
const initialItems = internalFieldStore.children.map(createUniqueId);
|
|
47
56
|
internalFieldStore.initialItems = createSignal(initialItems);
|
|
48
57
|
internalFieldStore.startItems = createSignal(initialItems);
|
|
49
58
|
internalFieldStore.items = createSignal(initialItems);
|
|
50
|
-
internalFieldStore.isTouched = createSignal(false);
|
|
51
|
-
internalFieldStore.isDirty = createSignal(false);
|
|
52
59
|
}
|
|
53
60
|
} else if (schema.type === "loose_object" || schema.type === "object" || schema.type === "strict_object") {
|
|
54
61
|
if (internalFieldStore.kind && internalFieldStore.kind !== "object") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "object"`);
|
|
@@ -61,6 +68,10 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
61
68
|
initializeFieldStore(internalFieldStore.children[key], schema.entries[key], initialInput?.[key], path);
|
|
62
69
|
path.pop();
|
|
63
70
|
}
|
|
71
|
+
const objectInput = nullish && initialInput == null ? initialInput : true;
|
|
72
|
+
internalFieldStore.initialInput = createSignal(objectInput);
|
|
73
|
+
internalFieldStore.startInput = createSignal(objectInput);
|
|
74
|
+
internalFieldStore.input = createSignal(objectInput);
|
|
64
75
|
}
|
|
65
76
|
} else {
|
|
66
77
|
internalFieldStore.kind = "value";
|
|
@@ -68,8 +79,6 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
68
79
|
internalFieldStore.initialInput = createSignal(initialInput);
|
|
69
80
|
internalFieldStore.startInput = createSignal(initialInput);
|
|
70
81
|
internalFieldStore.input = createSignal(initialInput);
|
|
71
|
-
internalFieldStore.isTouched = createSignal(false);
|
|
72
|
-
internalFieldStore.isDirty = createSignal(false);
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
84
|
}
|
|
@@ -77,10 +86,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
77
86
|
function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
|
|
78
87
|
batch(() => {
|
|
79
88
|
untrack(() => {
|
|
89
|
+
toInternalFieldStore.elements = fromInternalFieldStore.elements;
|
|
90
|
+
toInternalFieldStore.errors.value = fromInternalFieldStore.errors.value;
|
|
91
|
+
toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
|
|
92
|
+
toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
|
|
93
|
+
toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
|
|
94
|
+
toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
|
|
80
95
|
if (fromInternalFieldStore.kind === "array" && toInternalFieldStore.kind === "array") {
|
|
81
96
|
const fromItems = fromInternalFieldStore.items.value;
|
|
82
|
-
toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
|
|
83
|
-
toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
|
|
84
97
|
toInternalFieldStore.startItems.value = fromInternalFieldStore.startItems.value;
|
|
85
98
|
toInternalFieldStore.items.value = fromItems;
|
|
86
99
|
let path;
|
|
@@ -95,22 +108,20 @@ function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
|
|
|
95
108
|
copyItemState(fromInternalFieldStore.children[index], toInternalFieldStore.children[index]);
|
|
96
109
|
}
|
|
97
110
|
} else if (fromInternalFieldStore.kind === "object" && toInternalFieldStore.kind === "object") for (const key in fromInternalFieldStore.children) copyItemState(fromInternalFieldStore.children[key], toInternalFieldStore.children[key]);
|
|
98
|
-
else if (fromInternalFieldStore.kind === "value" && toInternalFieldStore.kind === "value") {
|
|
99
|
-
toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
|
|
100
|
-
toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
|
|
101
|
-
toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
|
|
102
|
-
toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
|
|
103
|
-
}
|
|
104
111
|
});
|
|
105
112
|
});
|
|
106
113
|
}
|
|
107
114
|
function resetItemState(internalFieldStore, initialInput) {
|
|
108
115
|
batch(() => {
|
|
116
|
+
internalFieldStore.elements = [];
|
|
109
117
|
internalFieldStore.errors.value = null;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
118
|
+
internalFieldStore.isTouched.value = false;
|
|
119
|
+
internalFieldStore.isDirty.value = false;
|
|
120
|
+
if (internalFieldStore.kind === "array" || internalFieldStore.kind === "object") {
|
|
121
|
+
const objectInput = initialInput == null ? initialInput : true;
|
|
122
|
+
internalFieldStore.startInput.value = objectInput;
|
|
123
|
+
internalFieldStore.input.value = objectInput;
|
|
124
|
+
if (internalFieldStore.kind === "array") if (initialInput) {
|
|
114
125
|
const newItems = initialInput.map(createUniqueId);
|
|
115
126
|
internalFieldStore.startItems.value = newItems;
|
|
116
127
|
internalFieldStore.items.value = newItems;
|
|
@@ -119,10 +130,8 @@ function resetItemState(internalFieldStore, initialInput) {
|
|
|
119
130
|
internalFieldStore.startItems.value = [];
|
|
120
131
|
internalFieldStore.items.value = [];
|
|
121
132
|
}
|
|
122
|
-
|
|
123
|
-
else {
|
|
124
|
-
internalFieldStore.isTouched.value = false;
|
|
125
|
-
internalFieldStore.isDirty.value = false;
|
|
133
|
+
else for (const key in internalFieldStore.children) resetItemState(internalFieldStore.children[key], initialInput?.[key]);
|
|
134
|
+
} else {
|
|
126
135
|
internalFieldStore.startInput.value = initialInput;
|
|
127
136
|
internalFieldStore.input.value = initialInput;
|
|
128
137
|
}
|
|
@@ -131,15 +140,27 @@ function resetItemState(internalFieldStore, initialInput) {
|
|
|
131
140
|
function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
|
|
132
141
|
batch(() => {
|
|
133
142
|
untrack(() => {
|
|
143
|
+
const tempElements = firstInternalFieldStore.elements;
|
|
144
|
+
firstInternalFieldStore.elements = secondInternalFieldStore.elements;
|
|
145
|
+
secondInternalFieldStore.elements = tempElements;
|
|
146
|
+
const tempErrors = firstInternalFieldStore.errors.value;
|
|
147
|
+
firstInternalFieldStore.errors.value = secondInternalFieldStore.errors.value;
|
|
148
|
+
secondInternalFieldStore.errors.value = tempErrors;
|
|
149
|
+
const tempStartInput = firstInternalFieldStore.startInput.value;
|
|
150
|
+
firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
|
|
151
|
+
secondInternalFieldStore.startInput.value = tempStartInput;
|
|
152
|
+
const tempInput = firstInternalFieldStore.input.value;
|
|
153
|
+
firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
|
|
154
|
+
secondInternalFieldStore.input.value = tempInput;
|
|
155
|
+
const tempIsTouched = firstInternalFieldStore.isTouched.value;
|
|
156
|
+
firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
|
|
157
|
+
secondInternalFieldStore.isTouched.value = tempIsTouched;
|
|
158
|
+
const tempIsDirty = firstInternalFieldStore.isDirty.value;
|
|
159
|
+
firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
|
|
160
|
+
secondInternalFieldStore.isDirty.value = tempIsDirty;
|
|
134
161
|
if (firstInternalFieldStore.kind === "array" && secondInternalFieldStore.kind === "array") {
|
|
135
162
|
const firstItems = firstInternalFieldStore.items.value;
|
|
136
163
|
const secondItems = secondInternalFieldStore.items.value;
|
|
137
|
-
const tempIsTouched = firstInternalFieldStore.isTouched.value;
|
|
138
|
-
firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
|
|
139
|
-
secondInternalFieldStore.isTouched.value = tempIsTouched;
|
|
140
|
-
const tempIsDirty = firstInternalFieldStore.isDirty.value;
|
|
141
|
-
firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
|
|
142
|
-
secondInternalFieldStore.isDirty.value = tempIsDirty;
|
|
143
164
|
const tempStartItems = firstInternalFieldStore.startItems.value;
|
|
144
165
|
firstInternalFieldStore.startItems.value = secondInternalFieldStore.startItems.value;
|
|
145
166
|
secondInternalFieldStore.startItems.value = tempStartItems;
|
|
@@ -166,33 +187,25 @@ function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
|
|
|
166
187
|
swapItemState(firstInternalFieldStore.children[index], secondInternalFieldStore.children[index]);
|
|
167
188
|
}
|
|
168
189
|
} else if (firstInternalFieldStore.kind === "object" && secondInternalFieldStore.kind === "object") for (const key in firstInternalFieldStore.children) swapItemState(firstInternalFieldStore.children[key], secondInternalFieldStore.children[key]);
|
|
169
|
-
else if (firstInternalFieldStore.kind === "value" && secondInternalFieldStore.kind === "value") {
|
|
170
|
-
const tempIsTouched = firstInternalFieldStore.isTouched.value;
|
|
171
|
-
firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
|
|
172
|
-
secondInternalFieldStore.isTouched.value = tempIsTouched;
|
|
173
|
-
const tempIsDirty = firstInternalFieldStore.isDirty.value;
|
|
174
|
-
firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
|
|
175
|
-
secondInternalFieldStore.isDirty.value = tempIsDirty;
|
|
176
|
-
const tempStartInput = firstInternalFieldStore.startInput.value;
|
|
177
|
-
firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
|
|
178
|
-
secondInternalFieldStore.startInput.value = tempStartInput;
|
|
179
|
-
const tempInput = firstInternalFieldStore.input.value;
|
|
180
|
-
firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
|
|
181
|
-
secondInternalFieldStore.input.value = tempInput;
|
|
182
|
-
}
|
|
183
190
|
});
|
|
184
191
|
});
|
|
185
192
|
}
|
|
186
193
|
function getFieldInput(internalFieldStore) {
|
|
187
194
|
if (internalFieldStore.kind === "array") {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
195
|
+
if (internalFieldStore.input.value) {
|
|
196
|
+
const value = [];
|
|
197
|
+
for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = getFieldInput(internalFieldStore.children[index]);
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
return internalFieldStore.input.value;
|
|
191
201
|
}
|
|
192
202
|
if (internalFieldStore.kind === "object") {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
203
|
+
if (internalFieldStore.input.value) {
|
|
204
|
+
const value = {};
|
|
205
|
+
for (const key in internalFieldStore.children) value[key] = getFieldInput(internalFieldStore.children[key]);
|
|
206
|
+
return value;
|
|
207
|
+
}
|
|
208
|
+
return internalFieldStore.input.value;
|
|
196
209
|
}
|
|
197
210
|
return internalFieldStore.input.value;
|
|
198
211
|
}
|
|
@@ -215,17 +228,16 @@ function getElementInput(element, internalFieldStore) {
|
|
|
215
228
|
return element.value;
|
|
216
229
|
}
|
|
217
230
|
function getFieldBool(internalFieldStore, type) {
|
|
231
|
+
if (internalFieldStore[type].value) return true;
|
|
218
232
|
if (internalFieldStore.kind === "array") {
|
|
219
|
-
if (internalFieldStore[type].value) return true;
|
|
220
233
|
for (let index = 0; index < internalFieldStore.items.value.length; index++) if (getFieldBool(internalFieldStore.children[index], type)) return true;
|
|
221
234
|
return false;
|
|
222
235
|
}
|
|
223
236
|
if (internalFieldStore.kind == "object") {
|
|
224
|
-
if (type === "errors" && internalFieldStore[type].value) return true;
|
|
225
237
|
for (const key in internalFieldStore.children) if (getFieldBool(internalFieldStore.children[key], type)) return true;
|
|
226
238
|
return false;
|
|
227
239
|
}
|
|
228
|
-
return
|
|
240
|
+
return false;
|
|
229
241
|
}
|
|
230
242
|
function getFieldStore(internalFormStore, path) {
|
|
231
243
|
let internalFieldStore = internalFormStore;
|
|
@@ -241,38 +253,53 @@ function setFieldBool(internalFieldStore, type, bool) {
|
|
|
241
253
|
else internalFieldStore[type].value = bool;
|
|
242
254
|
});
|
|
243
255
|
}
|
|
244
|
-
function
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
}
|
|
256
|
+
function setNestedInput(internalFieldStore, input) {
|
|
257
|
+
internalFieldStore.isTouched.value = true;
|
|
258
|
+
if (internalFieldStore.kind === "array") {
|
|
259
|
+
const arrayInput = input ?? [];
|
|
260
|
+
const items = internalFieldStore.items.value;
|
|
261
|
+
if (arrayInput.length < items.length) internalFieldStore.items.value = items.slice(0, arrayInput.length);
|
|
262
|
+
else if (arrayInput.length > items.length) {
|
|
263
|
+
if (arrayInput.length > internalFieldStore.children.length) {
|
|
264
|
+
const path = JSON.parse(internalFieldStore.name);
|
|
265
|
+
for (let index = internalFieldStore.children.length; index < arrayInput.length; index++) {
|
|
266
|
+
internalFieldStore.children[index] = {};
|
|
267
|
+
path.push(index);
|
|
268
|
+
initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], path);
|
|
269
|
+
path.pop();
|
|
259
270
|
}
|
|
260
|
-
internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createUniqueId)];
|
|
261
271
|
}
|
|
262
|
-
|
|
263
|
-
internalFieldStore.isDirty.value = untrack(() => internalFieldStore.startItems.value).length !== items.length;
|
|
264
|
-
} else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setFieldInput(internalFieldStore.children[key], input?.[key]);
|
|
265
|
-
else {
|
|
266
|
-
internalFieldStore.input.value = input;
|
|
267
|
-
internalFieldStore.isTouched.value = true;
|
|
268
|
-
const startInput = untrack(() => internalFieldStore.startInput.value);
|
|
269
|
-
internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
|
|
272
|
+
internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createUniqueId)];
|
|
270
273
|
}
|
|
274
|
+
for (let index = 0; index < items.length; index++) setNestedInput(internalFieldStore.children[index], arrayInput[index]);
|
|
275
|
+
internalFieldStore.input.value = input == null ? input : true;
|
|
276
|
+
internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== items.length;
|
|
277
|
+
} else if (internalFieldStore.kind === "object") {
|
|
278
|
+
for (const key in internalFieldStore.children) setNestedInput(internalFieldStore.children[key], input?.[key]);
|
|
279
|
+
internalFieldStore.input.value = input == null ? input : true;
|
|
280
|
+
internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value;
|
|
281
|
+
} else {
|
|
282
|
+
internalFieldStore.input.value = input;
|
|
283
|
+
const startInput = internalFieldStore.startInput.value;
|
|
284
|
+
internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function setFieldInput(internalFormStore, path, input) {
|
|
288
|
+
batch(() => {
|
|
289
|
+
untrack(() => {
|
|
290
|
+
let internalFieldStore = internalFormStore;
|
|
291
|
+
for (let index = 0; index < path.length; index++) {
|
|
292
|
+
internalFieldStore = internalFieldStore.children[path[index]];
|
|
293
|
+
if (index < path.length - 1) internalFieldStore.input.value = true;
|
|
294
|
+
else setNestedInput(internalFieldStore, input);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
271
297
|
});
|
|
272
298
|
}
|
|
273
299
|
function setInitialFieldInput(internalFieldStore, initialInput) {
|
|
274
300
|
batch(() => {
|
|
275
301
|
if (internalFieldStore.kind === "array") {
|
|
302
|
+
internalFieldStore.input.value = initialInput == null ? initialInput : true;
|
|
276
303
|
const initialArrayInput = initialInput ?? [];
|
|
277
304
|
if (initialArrayInput.length > internalFieldStore.children.length) {
|
|
278
305
|
const path = JSON.parse(internalFieldStore.name);
|
|
@@ -285,8 +312,10 @@ function setInitialFieldInput(internalFieldStore, initialInput) {
|
|
|
285
312
|
}
|
|
286
313
|
internalFieldStore.initialItems.value = initialArrayInput.map(createUniqueId);
|
|
287
314
|
for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index], initialArrayInput[index]);
|
|
288
|
-
} else if (internalFieldStore.kind === "object")
|
|
289
|
-
|
|
315
|
+
} else if (internalFieldStore.kind === "object") {
|
|
316
|
+
internalFieldStore.input.value = initialInput == null ? initialInput : true;
|
|
317
|
+
for (const key in internalFieldStore.children) setInitialFieldInput(internalFieldStore.children[key], initialInput?.[key]);
|
|
318
|
+
} else internalFieldStore.initialInput.value = initialInput;
|
|
290
319
|
});
|
|
291
320
|
}
|
|
292
321
|
function walkFieldStore(internalFieldStore, callback) {
|
|
@@ -347,8 +376,8 @@ async function validateFormInput(internalFormStore, config) {
|
|
|
347
376
|
});
|
|
348
377
|
return result;
|
|
349
378
|
}
|
|
350
|
-
function validateIfRequired(internalFormStore, internalFieldStore,
|
|
351
|
-
if (
|
|
379
|
+
function validateIfRequired(internalFormStore, internalFieldStore, validationMode) {
|
|
380
|
+
if (validationMode === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
|
|
352
381
|
}
|
|
353
382
|
var INTERNAL = "~internal";
|
|
354
383
|
|
|
@@ -389,7 +418,12 @@ function handleSubmit(form, handler) {
|
|
|
389
418
|
}
|
|
390
419
|
function insert(form, config) {
|
|
391
420
|
const internalFormStore = form[INTERNAL];
|
|
392
|
-
|
|
421
|
+
let internalFieldStore = internalFormStore;
|
|
422
|
+
for (let index = 0; index < config.path.length; index++) {
|
|
423
|
+
internalFieldStore = internalFieldStore.children[config.path[index]];
|
|
424
|
+
if (index < config.path.length - 1) internalFieldStore.input.value = true;
|
|
425
|
+
}
|
|
426
|
+
const internalArrayStore = internalFieldStore;
|
|
393
427
|
const items = untrack(() => internalArrayStore.items.value);
|
|
394
428
|
const insertIndex = config.at === void 0 ? items.length : config.at;
|
|
395
429
|
if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
|
|
@@ -403,6 +437,7 @@ function insert(form, config) {
|
|
|
403
437
|
path.push(insertIndex);
|
|
404
438
|
initializeFieldStore(internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, path);
|
|
405
439
|
} else resetItemState(internalArrayStore.children[insertIndex], config.initialInput);
|
|
440
|
+
internalArrayStore.input.value = true;
|
|
406
441
|
internalArrayStore.isTouched.value = true;
|
|
407
442
|
internalArrayStore.isDirty.value = true;
|
|
408
443
|
validateIfRequired(internalFormStore, internalArrayStore, "input");
|
|
@@ -462,17 +497,20 @@ function reset(form, config) {
|
|
|
462
497
|
const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
|
|
463
498
|
if (config?.initialInput) setInitialFieldInput(internalFieldStore, config.initialInput);
|
|
464
499
|
walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
|
|
500
|
+
internalFieldStore$1.elements = internalFieldStore$1.initialElements;
|
|
465
501
|
if (!config?.keepErrors) internalFieldStore$1.errors.value = null;
|
|
502
|
+
if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
|
|
503
|
+
internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
|
|
504
|
+
if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
|
|
466
505
|
if (internalFieldStore$1.kind === "array") {
|
|
467
506
|
internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
|
|
468
507
|
if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
|
|
508
|
+
internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value || internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
|
|
509
|
+
} else if (internalFieldStore$1.kind === "object") internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
|
|
510
|
+
else {
|
|
511
|
+
const startInput = internalFieldStore$1.startInput.value;
|
|
512
|
+
const input = internalFieldStore$1.input.value;
|
|
513
|
+
internalFieldStore$1.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
|
|
476
514
|
for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
|
|
477
515
|
}
|
|
478
516
|
});
|
|
@@ -489,9 +527,8 @@ function setErrors(form, config) {
|
|
|
489
527
|
function setInput(form, config) {
|
|
490
528
|
batch(() => {
|
|
491
529
|
const internalFormStore = form[INTERNAL];
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
validateIfRequired(internalFormStore, internalFieldStore, "input");
|
|
530
|
+
setFieldInput(internalFormStore, config.path ?? [], config.input);
|
|
531
|
+
validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
|
|
495
532
|
});
|
|
496
533
|
}
|
|
497
534
|
function submit(form) {
|
|
@@ -570,9 +607,10 @@ function unwrap(value) {
|
|
|
570
607
|
|
|
571
608
|
// src/primitives/useField/useField.ts
|
|
572
609
|
function useField(form, config) {
|
|
610
|
+
const getPath = createMemo(() => unwrap(config).path);
|
|
573
611
|
const getInternalFormStore = createMemo(() => unwrap(form)[INTERNAL]);
|
|
574
612
|
const getInternalFieldStore = createMemo(
|
|
575
|
-
() => getFieldStore(getInternalFormStore(),
|
|
613
|
+
() => getFieldStore(getInternalFormStore(), getPath())
|
|
576
614
|
);
|
|
577
615
|
const getInput2 = createMemo(() => getFieldInput(getInternalFieldStore()));
|
|
578
616
|
const getIsTouched = createMemo(
|
|
@@ -586,7 +624,7 @@ function useField(form, config) {
|
|
|
586
624
|
);
|
|
587
625
|
return {
|
|
588
626
|
get path() {
|
|
589
|
-
return
|
|
627
|
+
return getPath();
|
|
590
628
|
},
|
|
591
629
|
get input() {
|
|
592
630
|
return getInput2();
|
|
@@ -629,7 +667,8 @@ function useField(form, config) {
|
|
|
629
667
|
onInput(event) {
|
|
630
668
|
const internalFieldStore = getInternalFieldStore();
|
|
631
669
|
setFieldInput(
|
|
632
|
-
|
|
670
|
+
getInternalFormStore(),
|
|
671
|
+
getPath(),
|
|
633
672
|
getElementInput(event.currentTarget, internalFieldStore)
|
|
634
673
|
);
|
|
635
674
|
validateIfRequired(getInternalFormStore(), internalFieldStore, "input");
|