@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/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 === "
|
|
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
|
-
|
|
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";
|
|
@@ -38,15 +43,17 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
38
43
|
} else for (let index = 0; index < schema.items; index++) {
|
|
39
44
|
internalFieldStore.children[index] = {};
|
|
40
45
|
path.push(index);
|
|
41
|
-
initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput
|
|
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"`);
|
|
@@ -56,9 +63,13 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
|
|
|
56
63
|
for (const key in schema.entries) {
|
|
57
64
|
internalFieldStore.children[key] = {};
|
|
58
65
|
path.push(key);
|
|
59
|
-
initializeFieldStore(internalFieldStore.children[key], schema.entries[key], initialInput
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
|
238
|
+
return false;
|
|
227
239
|
}
|
|
228
240
|
function getFieldStore(internalFormStore, path) {
|
|
229
241
|
let internalFieldStore = internalFormStore;
|
|
@@ -239,50 +251,69 @@ function setFieldBool(internalFieldStore, type, bool) {
|
|
|
239
251
|
else internalFieldStore[type].value = bool;
|
|
240
252
|
});
|
|
241
253
|
}
|
|
242
|
-
function
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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();
|
|
256
268
|
}
|
|
257
|
-
internalFieldStore.items.value = [...items, ...input.slice(items.length).map(createId)];
|
|
258
269
|
}
|
|
259
|
-
|
|
260
|
-
internalFieldStore.isDirty.value = untrack(() => internalFieldStore.startItems.value).length !== items.length;
|
|
261
|
-
} else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setFieldInput(internalFieldStore.children[key], input[key]);
|
|
262
|
-
else {
|
|
263
|
-
internalFieldStore.input.value = input;
|
|
264
|
-
internalFieldStore.isTouched.value = true;
|
|
265
|
-
const startInput = untrack(() => internalFieldStore.startInput.value);
|
|
266
|
-
internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
|
|
270
|
+
internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createId)];
|
|
267
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
|
+
});
|
|
268
295
|
});
|
|
269
296
|
}
|
|
270
297
|
function setInitialFieldInput(internalFieldStore, initialInput) {
|
|
271
298
|
batch(() => {
|
|
272
299
|
if (internalFieldStore.kind === "array") {
|
|
273
|
-
|
|
300
|
+
internalFieldStore.input.value = initialInput == null ? initialInput : true;
|
|
301
|
+
const initialArrayInput = initialInput ?? [];
|
|
302
|
+
if (initialArrayInput.length > internalFieldStore.children.length) {
|
|
274
303
|
const path = JSON.parse(internalFieldStore.name);
|
|
275
|
-
for (let index = internalFieldStore.children.length; index <
|
|
304
|
+
for (let index = internalFieldStore.children.length; index < initialArrayInput.length; index++) {
|
|
276
305
|
internalFieldStore.children[index] = {};
|
|
277
306
|
path.push(index);
|
|
278
|
-
initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item,
|
|
307
|
+
initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, initialArrayInput[index], path);
|
|
279
308
|
path.pop();
|
|
280
309
|
}
|
|
281
310
|
}
|
|
282
|
-
internalFieldStore.initialItems.value =
|
|
283
|
-
for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index],
|
|
284
|
-
} else if (internalFieldStore.kind === "object")
|
|
285
|
-
|
|
311
|
+
internalFieldStore.initialItems.value = initialArrayInput.map(createId);
|
|
312
|
+
for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index], initialArrayInput[index]);
|
|
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;
|
|
286
317
|
});
|
|
287
318
|
}
|
|
288
319
|
function walkFieldStore(internalFieldStore, callback) {
|
|
@@ -343,8 +374,8 @@ async function validateFormInput(internalFormStore, config) {
|
|
|
343
374
|
});
|
|
344
375
|
return result;
|
|
345
376
|
}
|
|
346
|
-
function validateIfRequired(internalFormStore, internalFieldStore,
|
|
347
|
-
if (
|
|
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);
|
|
348
379
|
}
|
|
349
380
|
var INTERNAL = "~internal";
|
|
350
381
|
|
|
@@ -385,7 +416,12 @@ function handleSubmit(form, handler) {
|
|
|
385
416
|
}
|
|
386
417
|
function insert(form, config) {
|
|
387
418
|
const internalFormStore = form[INTERNAL];
|
|
388
|
-
|
|
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;
|
|
389
425
|
const items = untrack(() => internalArrayStore.items.value);
|
|
390
426
|
const insertIndex = config.at === void 0 ? items.length : config.at;
|
|
391
427
|
if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
|
|
@@ -399,6 +435,7 @@ function insert(form, config) {
|
|
|
399
435
|
path.push(insertIndex);
|
|
400
436
|
initializeFieldStore(internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, path);
|
|
401
437
|
} else resetItemState(internalArrayStore.children[insertIndex], config.initialInput);
|
|
438
|
+
internalArrayStore.input.value = true;
|
|
402
439
|
internalArrayStore.isTouched.value = true;
|
|
403
440
|
internalArrayStore.isDirty.value = true;
|
|
404
441
|
validateIfRequired(internalFormStore, internalArrayStore, "input");
|
|
@@ -458,17 +495,20 @@ function reset(form, config) {
|
|
|
458
495
|
const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
|
|
459
496
|
if (config?.initialInput) setInitialFieldInput(internalFieldStore, config.initialInput);
|
|
460
497
|
walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
|
|
498
|
+
internalFieldStore$1.elements = internalFieldStore$1.initialElements;
|
|
461
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;
|
|
462
503
|
if (internalFieldStore$1.kind === "array") {
|
|
463
504
|
internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
|
|
464
505
|
if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
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));
|
|
472
512
|
for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
|
|
473
513
|
}
|
|
474
514
|
});
|
|
@@ -485,9 +525,8 @@ function setErrors(form, config) {
|
|
|
485
525
|
function setInput(form, config) {
|
|
486
526
|
batch(() => {
|
|
487
527
|
const internalFormStore = form[INTERNAL];
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
validateIfRequired(internalFormStore, internalFieldStore, "input");
|
|
528
|
+
setFieldInput(internalFormStore, config.path ?? [], config.input);
|
|
529
|
+
validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
|
|
491
530
|
});
|
|
492
531
|
}
|
|
493
532
|
function submit(form) {
|
|
@@ -519,7 +558,7 @@ import * as v2 from "valibot";
|
|
|
519
558
|
function createForm(config) {
|
|
520
559
|
const internalFormStore = createFormStore(
|
|
521
560
|
config,
|
|
522
|
-
|
|
561
|
+
(input) => v2.safeParseAsync(config.schema, input)
|
|
523
562
|
);
|
|
524
563
|
const getIsTouched = createMemo(
|
|
525
564
|
() => getFieldBool(internalFormStore, "isTouched")
|
|
@@ -573,9 +612,10 @@ function unwrap(value) {
|
|
|
573
612
|
|
|
574
613
|
// src/primitives/useField/useField.ts
|
|
575
614
|
function useField(form, config) {
|
|
615
|
+
const getPath = createMemo2(() => unwrap(config).path);
|
|
576
616
|
const getInternalFormStore = createMemo2(() => unwrap(form)[INTERNAL]);
|
|
577
617
|
const getInternalFieldStore = createMemo2(
|
|
578
|
-
() => getFieldStore(getInternalFormStore(),
|
|
618
|
+
() => getFieldStore(getInternalFormStore(), getPath())
|
|
579
619
|
);
|
|
580
620
|
const getInput2 = createMemo2(() => getFieldInput(getInternalFieldStore()));
|
|
581
621
|
const getIsTouched = createMemo2(
|
|
@@ -589,7 +629,7 @@ function useField(form, config) {
|
|
|
589
629
|
);
|
|
590
630
|
return {
|
|
591
631
|
get path() {
|
|
592
|
-
return
|
|
632
|
+
return getPath();
|
|
593
633
|
},
|
|
594
634
|
get input() {
|
|
595
635
|
return getInput2();
|
|
@@ -632,7 +672,8 @@ function useField(form, config) {
|
|
|
632
672
|
onInput(event) {
|
|
633
673
|
const internalFieldStore = getInternalFieldStore();
|
|
634
674
|
setFieldInput(
|
|
635
|
-
|
|
675
|
+
getInternalFormStore(),
|
|
676
|
+
getPath(),
|
|
636
677
|
getElementInput(event.currentTarget, internalFieldStore)
|
|
637
678
|
);
|
|
638
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
|
|
@@ -371,7 +376,7 @@ interface FieldArrayProps<TSchema extends Schema = Schema, TFieldArrayPath exten
|
|
|
371
376
|
declare function FieldArray<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(props: FieldArrayProps<TSchema, TFieldArrayPath>): JSX.Element;
|
|
372
377
|
//#endregion
|
|
373
378
|
//#region src/components/Form/Form.d.ts
|
|
374
|
-
type FormProps<TSchema extends Schema = Schema> = Omit<JSX.FormHTMLAttributes<HTMLFormElement>, "onSubmit"> & {
|
|
379
|
+
type FormProps<TSchema extends Schema = Schema> = Omit<JSX.FormHTMLAttributes<HTMLFormElement>, "onSubmit" | "novalidate" | "noValidate"> & {
|
|
375
380
|
of: FormStore<TSchema>;
|
|
376
381
|
children: JSX.Element;
|
|
377
382
|
onSubmit: SubmitHandler<TSchema>;
|