@formisch/solid 0.3.0 → 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/README.md +2 -1
- package/dist/dev.js +142 -101
- package/dist/dev.jsx +142 -101
- package/dist/index.d.ts +10 -5
- package/dist/index.js +142 -101
- package/dist/index.jsx +142 -101
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Formisch is a schema-based, headless form library for SolidJS. It manages form state and validation. It is type-safe, fast by default and its bundle size is small due to its modular design. Try it out in our [playground](https://stackblitz.com/edit/formisch-playground-solid)!
|
|
4
4
|
|
|
5
|
-
Formisch is also available for [Preact][formisch-preact], [Qwik][formisch-qwik], and [Vue][formisch-vue].
|
|
5
|
+
Formisch is also available for [Preact][formisch-preact], [Qwik][formisch-qwik], [Svelte][formisch-svelte] and [Vue][formisch-vue].
|
|
6
6
|
|
|
7
7
|
## Highlights
|
|
8
8
|
|
|
@@ -77,4 +77,5 @@ This project is available free of charge and licensed under the [MIT license](ht
|
|
|
77
77
|
|
|
78
78
|
[formisch-preact]: https://github.com/fabian-hiller/formisch/tree/main/frameworks/preact
|
|
79
79
|
[formisch-qwik]: https://github.com/fabian-hiller/formisch/tree/main/frameworks/qwik
|
|
80
|
+
[formisch-svelte]: https://github.com/fabian-hiller/formisch/tree/main/frameworks/svelte
|
|
80
81
|
[formisch-vue]: https://github.com/fabian-hiller/formisch/tree/main/frameworks/vue
|
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";
|
|
@@ -40,15 +45,17 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
40
45
|
} else for (let index = 0; index < schema.items; index++) {
|
|
41
46
|
internalFieldStore.children[index] = {};
|
|
42
47
|
path.push(index);
|
|
43
|
-
initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput
|
|
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"`);
|
|
@@ -58,9 +65,13 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
58
65
|
for (const key in schema.entries) {
|
|
59
66
|
internalFieldStore.children[key] = {};
|
|
60
67
|
path.push(key);
|
|
61
|
-
initializeFieldStore(internalFieldStore.children[key], schema.entries[key], initialInput
|
|
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,50 +253,69 @@ 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
|
-
|
|
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();
|
|
258
270
|
}
|
|
259
|
-
internalFieldStore.items.value = [...items, ...input.slice(items.length).map(createUniqueId)];
|
|
260
271
|
}
|
|
261
|
-
|
|
262
|
-
internalFieldStore.isDirty.value = untrack(() => internalFieldStore.startItems.value).length !== items.length;
|
|
263
|
-
} else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setFieldInput(internalFieldStore.children[key], input[key]);
|
|
264
|
-
else {
|
|
265
|
-
internalFieldStore.input.value = input;
|
|
266
|
-
internalFieldStore.isTouched.value = true;
|
|
267
|
-
const startInput = untrack(() => internalFieldStore.startInput.value);
|
|
268
|
-
internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
|
|
272
|
+
internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createUniqueId)];
|
|
269
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
|
+
});
|
|
270
297
|
});
|
|
271
298
|
}
|
|
272
299
|
function setInitialFieldInput(internalFieldStore, initialInput) {
|
|
273
300
|
batch(() => {
|
|
274
301
|
if (internalFieldStore.kind === "array") {
|
|
275
|
-
|
|
302
|
+
internalFieldStore.input.value = initialInput == null ? initialInput : true;
|
|
303
|
+
const initialArrayInput = initialInput ?? [];
|
|
304
|
+
if (initialArrayInput.length > internalFieldStore.children.length) {
|
|
276
305
|
const path = JSON.parse(internalFieldStore.name);
|
|
277
|
-
for (let index = internalFieldStore.children.length; index <
|
|
306
|
+
for (let index = internalFieldStore.children.length; index < initialArrayInput.length; index++) {
|
|
278
307
|
internalFieldStore.children[index] = {};
|
|
279
308
|
path.push(index);
|
|
280
|
-
initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item,
|
|
309
|
+
initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, initialArrayInput[index], path);
|
|
281
310
|
path.pop();
|
|
282
311
|
}
|
|
283
312
|
}
|
|
284
|
-
internalFieldStore.initialItems.value =
|
|
285
|
-
for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index],
|
|
286
|
-
} else if (internalFieldStore.kind === "object")
|
|
287
|
-
|
|
313
|
+
internalFieldStore.initialItems.value = initialArrayInput.map(createUniqueId);
|
|
314
|
+
for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index], initialArrayInput[index]);
|
|
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;
|
|
288
319
|
});
|
|
289
320
|
}
|
|
290
321
|
function walkFieldStore(internalFieldStore, callback) {
|
|
@@ -345,8 +376,8 @@ async function validateFormInput(internalFormStore, config) {
|
|
|
345
376
|
});
|
|
346
377
|
return result;
|
|
347
378
|
}
|
|
348
|
-
function validateIfRequired(internalFormStore, internalFieldStore,
|
|
349
|
-
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);
|
|
350
381
|
}
|
|
351
382
|
var INTERNAL = "~internal";
|
|
352
383
|
|
|
@@ -387,7 +418,12 @@ function handleSubmit(form, handler) {
|
|
|
387
418
|
}
|
|
388
419
|
function insert(form, config) {
|
|
389
420
|
const internalFormStore = form[INTERNAL];
|
|
390
|
-
|
|
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;
|
|
391
427
|
const items = untrack(() => internalArrayStore.items.value);
|
|
392
428
|
const insertIndex = config.at === void 0 ? items.length : config.at;
|
|
393
429
|
if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
|
|
@@ -401,6 +437,7 @@ function insert(form, config) {
|
|
|
401
437
|
path.push(insertIndex);
|
|
402
438
|
initializeFieldStore(internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, path);
|
|
403
439
|
} else resetItemState(internalArrayStore.children[insertIndex], config.initialInput);
|
|
440
|
+
internalArrayStore.input.value = true;
|
|
404
441
|
internalArrayStore.isTouched.value = true;
|
|
405
442
|
internalArrayStore.isDirty.value = true;
|
|
406
443
|
validateIfRequired(internalFormStore, internalArrayStore, "input");
|
|
@@ -460,17 +497,20 @@ function reset(form, config) {
|
|
|
460
497
|
const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
|
|
461
498
|
if (config?.initialInput) setInitialFieldInput(internalFieldStore, config.initialInput);
|
|
462
499
|
walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
|
|
500
|
+
internalFieldStore$1.elements = internalFieldStore$1.initialElements;
|
|
463
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;
|
|
464
505
|
if (internalFieldStore$1.kind === "array") {
|
|
465
506
|
internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
|
|
466
507
|
if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
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));
|
|
474
514
|
for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
|
|
475
515
|
}
|
|
476
516
|
});
|
|
@@ -487,9 +527,8 @@ function setErrors(form, config) {
|
|
|
487
527
|
function setInput(form, config) {
|
|
488
528
|
batch(() => {
|
|
489
529
|
const internalFormStore = form[INTERNAL];
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
validateIfRequired(internalFormStore, internalFieldStore, "input");
|
|
530
|
+
setFieldInput(internalFormStore, config.path ?? [], config.input);
|
|
531
|
+
validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
|
|
493
532
|
});
|
|
494
533
|
}
|
|
495
534
|
function submit(form) {
|
|
@@ -517,7 +556,7 @@ function validate(form, config) {
|
|
|
517
556
|
function createForm(config) {
|
|
518
557
|
const internalFormStore = createFormStore(
|
|
519
558
|
config,
|
|
520
|
-
|
|
559
|
+
(input) => v.safeParseAsync(config.schema, input)
|
|
521
560
|
);
|
|
522
561
|
const getIsTouched = createMemo(
|
|
523
562
|
() => getFieldBool(internalFormStore, "isTouched")
|
|
@@ -568,9 +607,10 @@ function unwrap(value) {
|
|
|
568
607
|
|
|
569
608
|
// src/primitives/useField/useField.ts
|
|
570
609
|
function useField(form, config) {
|
|
610
|
+
const getPath = createMemo(() => unwrap(config).path);
|
|
571
611
|
const getInternalFormStore = createMemo(() => unwrap(form)[INTERNAL]);
|
|
572
612
|
const getInternalFieldStore = createMemo(
|
|
573
|
-
() => getFieldStore(getInternalFormStore(),
|
|
613
|
+
() => getFieldStore(getInternalFormStore(), getPath())
|
|
574
614
|
);
|
|
575
615
|
const getInput2 = createMemo(() => getFieldInput(getInternalFieldStore()));
|
|
576
616
|
const getIsTouched = createMemo(
|
|
@@ -584,7 +624,7 @@ function useField(form, config) {
|
|
|
584
624
|
);
|
|
585
625
|
return {
|
|
586
626
|
get path() {
|
|
587
|
-
return
|
|
627
|
+
return getPath();
|
|
588
628
|
},
|
|
589
629
|
get input() {
|
|
590
630
|
return getInput2();
|
|
@@ -627,7 +667,8 @@ function useField(form, config) {
|
|
|
627
667
|
onInput(event) {
|
|
628
668
|
const internalFieldStore = getInternalFieldStore();
|
|
629
669
|
setFieldInput(
|
|
630
|
-
|
|
670
|
+
getInternalFormStore(),
|
|
671
|
+
getPath(),
|
|
631
672
|
getElementInput(event.currentTarget, internalFieldStore)
|
|
632
673
|
);
|
|
633
674
|
validateIfRequired(getInternalFormStore(), internalFieldStore, "input");
|