@formisch/qwik 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/index.d.ts CHANGED
@@ -24,19 +24,25 @@ interface InternalBaseStore {
24
24
  schema: NoSerialize<Schema>;
25
25
  elements: FieldElement[];
26
26
  errors: Signal<[string, ...string[]] | null>;
27
+ isTouched: Signal<boolean>;
28
+ isDirty: Signal<boolean>;
27
29
  }
28
30
  interface InternalArrayStore extends InternalBaseStore {
29
31
  kind: "array";
30
32
  children: InternalFieldStore[];
33
+ initialInput: Signal<true | null | undefined>;
34
+ startInput: Signal<true | null | undefined>;
35
+ input: Signal<true | null | undefined>;
31
36
  initialItems: Signal<string[]>;
32
37
  startItems: Signal<string[]>;
33
38
  items: Signal<string[]>;
34
- isTouched: Signal<boolean>;
35
- isDirty: Signal<boolean>;
36
39
  }
37
40
  interface InternalObjectStore extends InternalBaseStore {
38
41
  kind: "object";
39
42
  children: Record<string, InternalFieldStore>;
43
+ initialInput: Signal<true | null | undefined>;
44
+ startInput: Signal<true | null | undefined>;
45
+ input: Signal<true | null | undefined>;
40
46
  }
41
47
  interface InternalValueStore extends InternalBaseStore {
42
48
  kind: "value";
@@ -169,21 +175,129 @@ type ValidArrayPath<TValue, TPath extends RequiredPath> = TPath extends LazyArra
169
175
  */
170
176
  //#endregion
171
177
  //#region ../../packages/methods/dist/index.qwik.d.ts
172
- declare function focus(config: any): void;
173
- declare function getAllErrors(form: any): null;
174
- declare function getErrors(form: any, config: any): any;
175
- declare function getInput(form: any, config: any): unknown;
176
- declare function handleSubmit(form: any, handler: any): (event: any) => Promise<void>;
177
- declare function insert(form: any, config: any): void;
178
- declare function move(form: any, config: any): void;
179
- declare function remove(form: any, config: any): void;
180
- declare function replace(form: any, config: any): void;
181
- declare function reset(form: any, config: any): void;
182
- declare function setErrors(form: any, config: any): void;
183
- declare function setInput(form: any, config: any): void;
184
- declare function submit(form: any): void;
185
- declare function swap(form: any, config: any): void;
186
- declare function validate(form: any, config: any): Promise<v.SafeParseResult<Schema>>;
178
+ //#region src/focus/focus.d.ts
179
+ interface FocusFieldConfig<TSchema extends Schema, TFieldPath extends RequiredPath> {
180
+ readonly form: BaseFormStore<TSchema>;
181
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
182
+ }
183
+ declare function focus<TSchema extends Schema, TFieldPath extends RequiredPath>(config: FocusFieldConfig<TSchema, TFieldPath>): void;
184
+ //#endregion
185
+ //#region src/getAllErrors/getAllErrors.d.ts
186
+ declare function getAllErrors(form: BaseFormStore): [string, ...string[]] | null;
187
+ //#endregion
188
+ //#region src/getErrors/getErrors.d.ts
189
+ interface GetFormErrorsConfig {
190
+ readonly path?: undefined;
191
+ }
192
+ interface GetFieldErrorsConfig<TSchema extends Schema, TFieldPath extends RequiredPath> {
193
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
194
+ }
195
+ declare function getErrors<TSchema extends Schema>(form: BaseFormStore<TSchema>): [string, ...string[]] | null;
196
+ declare function getErrors<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? GetFieldErrorsConfig<TSchema, TFieldPath> : GetFormErrorsConfig): [string, ...string[]] | null;
197
+ //#endregion
198
+ //#region src/getInput/getInput.d.ts
199
+ interface GetFormInputConfig {
200
+ readonly path?: undefined;
201
+ }
202
+ interface GetFieldInputConfig<TSchema extends Schema, TFieldPath extends RequiredPath> {
203
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
204
+ }
205
+ declare function getInput<TSchema extends Schema>(form: BaseFormStore<TSchema>): PartialValues<v.InferInput<TSchema>>;
206
+ declare function getInput<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? GetFieldInputConfig<TSchema, TFieldPath> : GetFormInputConfig): PartialValues<TFieldPath extends RequiredPath ? PathValue<v.InferInput<TSchema>, TFieldPath> : v.InferInput<TSchema>>;
207
+ //#endregion
208
+ //#region src/handleSubmit/handleSubmit.d.ts
209
+ declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitHandler<TSchema>): (event: SubmitEvent) => void;
210
+ //#endregion
211
+ //#region src/insert/insert.d.ts
212
+ interface InsertConfig<TSchema extends Schema, TFieldArrayPath extends RequiredPath> {
213
+ readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
214
+ readonly at?: number | undefined;
215
+ readonly initialInput?: DeepPartial<PathValue<v.InferInput<TSchema>, [...TFieldArrayPath, number]>> | undefined;
216
+ }
217
+ declare function insert<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: BaseFormStore<TSchema>, config: InsertConfig<TSchema, TFieldArrayPath>): void;
218
+ //#endregion
219
+ //#region src/move/move.d.ts
220
+ interface MoveConfig<TSchema extends Schema, TFieldArrayPath extends RequiredPath> {
221
+ readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
222
+ readonly from: number;
223
+ readonly to: number;
224
+ }
225
+ declare function move<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: BaseFormStore<TSchema>, config: MoveConfig<TSchema, TFieldArrayPath>): void;
226
+ //#endregion
227
+ //#region src/remove/remove.d.ts
228
+ interface RemoveConfig<TSchema extends Schema, TFieldArrayPath extends RequiredPath> {
229
+ readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
230
+ readonly at: number;
231
+ }
232
+ declare function remove<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: BaseFormStore<TSchema>, config: RemoveConfig<TSchema, TFieldArrayPath>): void;
233
+ //#endregion
234
+ //#region src/replace/replace.d.ts
235
+ interface ReplaceConfig<TSchema extends Schema, TFieldArrayPath extends RequiredPath> {
236
+ readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
237
+ readonly at: number;
238
+ readonly initialInput?: DeepPartial<PathValue<v.InferInput<TSchema>, [...TFieldArrayPath, number]>> | undefined;
239
+ }
240
+ declare function replace<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: BaseFormStore<TSchema>, config: ReplaceConfig<TSchema, TFieldArrayPath>): void;
241
+ //#endregion
242
+ //#region src/reset/reset.d.ts
243
+ interface ResetBaseConfig {
244
+ readonly keepInput?: boolean | undefined;
245
+ readonly keepTouched?: boolean | undefined;
246
+ readonly keepErrors?: boolean | undefined;
247
+ }
248
+ interface ResetFormConfig<TSchema extends Schema> extends ResetBaseConfig {
249
+ readonly path?: undefined;
250
+ readonly initialInput?: DeepPartial<v.InferInput<TSchema>> | undefined;
251
+ readonly keepSubmitCount?: boolean | undefined;
252
+ readonly keepSubmitted?: boolean | undefined;
253
+ }
254
+ interface ResetFieldConfig<TSchema extends Schema, TFieldPath extends RequiredPath> extends ResetBaseConfig {
255
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
256
+ readonly initialInput?: DeepPartial<PathValue<v.InferInput<TSchema>, TFieldPath>>;
257
+ }
258
+ declare function reset(form: BaseFormStore): void;
259
+ declare function reset<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? ResetFieldConfig<TSchema, TFieldPath> : ResetFormConfig<TSchema>): void;
260
+ //#endregion
261
+ //#region src/setErrors/setErrors.d.ts
262
+ interface SetFormErrorsConfig {
263
+ readonly path?: undefined;
264
+ readonly errors: [string, ...string[]] | null;
265
+ }
266
+ interface SetFieldErrorsConfig<TSchema extends Schema, TFieldPath extends RequiredPath> {
267
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
268
+ readonly errors: [string, ...string[]] | null;
269
+ }
270
+ declare function setErrors<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? SetFieldErrorsConfig<TSchema, TFieldPath> : SetFormErrorsConfig): void;
271
+ //#endregion
272
+ //#region src/setInput/setInput.d.ts
273
+ interface SetFormInputConfig<TSchema extends Schema> {
274
+ readonly path?: undefined;
275
+ readonly input: v.InferInput<TSchema>;
276
+ }
277
+ interface SetFieldInputConfig<TSchema extends Schema, TFieldPath extends RequiredPath> {
278
+ readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
279
+ readonly input: PathValue<v.InferInput<TSchema>, TFieldPath>;
280
+ }
281
+ declare function setInput<TSchema extends Schema>(form: BaseFormStore<TSchema>, config: SetFormInputConfig<TSchema>): void;
282
+ declare function setInput<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? SetFieldInputConfig<TSchema, TFieldPath> : SetFormInputConfig<TSchema>): void;
283
+ //#endregion
284
+ //#region src/submit/submit.d.ts
285
+ declare function submit(form: BaseFormStore): void;
286
+ //#endregion
287
+ //#region src/swap/swap.d.ts
288
+ interface SwapConfig<TSchema extends Schema, TFieldArrayPath extends RequiredPath> {
289
+ readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
290
+ readonly at: number;
291
+ readonly and: number;
292
+ }
293
+ declare function swap<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: BaseFormStore<TSchema>, config: SwapConfig<TSchema, TFieldArrayPath>): void;
294
+ //#endregion
295
+ //#region src/validate/validate.d.ts
296
+ interface ValidateFormConfig {
297
+ readonly shouldFocus?: boolean | undefined;
298
+ }
299
+ declare function validate<TSchema extends Schema>(form: BaseFormStore<TSchema>, config?: ValidateFormConfig): Promise<v.SafeParseResult<TSchema>>;
300
+ //#endregion
187
301
  //#endregion
188
302
  //#region src/types/field.d.ts
189
303
  /**
@@ -284,4 +398,4 @@ declare function useFieldArray<TSchema extends Schema, TFieldArrayPath extends R
284
398
  declare function useFormQrl<TSchema extends Schema>(configQrl: QRL<FormConfig<TSchema>>): FormStore<TSchema>;
285
399
  declare const useForm$: <TSchema extends Schema>(qrl: FormConfig<TSchema>) => FormStore<TSchema>;
286
400
  //#endregion
287
- export { Field, FieldArray, FieldArrayProps, FieldArrayStore, FieldElementProps, FieldProps, FieldStore, Form, FormProps, FormStore, type SubmitHandler, UseFieldArrayConfig, UseFieldConfig, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, useForm$, useFormQrl, validate };
401
+ export { Field, FieldArray, FieldArrayProps, FieldArrayStore, FieldElementProps, FieldProps, FieldStore, FocusFieldConfig, Form, FormProps, FormStore, GetFieldErrorsConfig, GetFieldInputConfig, GetFormErrorsConfig, GetFormInputConfig, InsertConfig, MoveConfig, RemoveConfig, ReplaceConfig, ResetFieldConfig, ResetFormConfig, SetFieldErrorsConfig, SetFieldInputConfig, SetFormErrorsConfig, SetFormInputConfig, type SubmitHandler, SwapConfig, UseFieldArrayConfig, UseFieldConfig, ValidateFormConfig, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, useForm$, useFormQrl, validate };
@@ -14,16 +14,21 @@ function batch(fn) {
14
14
  * TODO: Add comment
15
15
  * TODO: Should this stay in /primitives or move to /utils?
16
16
  */
17
- function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
17
+ function initializeFieldStore(internalFieldStore, schema, initialInput, path, nullish = false) {
18
18
  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`);
19
19
  else if (schema.type === "lazy") initializeFieldStore(internalFieldStore, schema.getter(void 0), initialInput, path);
20
- 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);
20
+ 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);
21
+ else if (schema.type === "non_nullable" || schema.type === "non_nullish" || schema.type === "non_optional") initializeFieldStore(internalFieldStore, schema.wrapped, initialInput, path);
21
22
  else if (schema.type === "intersect" || schema.type === "union" || schema.type === "variant") for (const schemaOption of schema.options) initializeFieldStore(internalFieldStore, schemaOption, initialInput, path);
22
23
  else {
23
24
  internalFieldStore.schema = schema;
24
25
  internalFieldStore.name = JSON.stringify(path);
25
- internalFieldStore.elements = [];
26
+ const initialElements = [];
27
+ internalFieldStore.initialElements = initialElements;
28
+ internalFieldStore.elements = initialElements;
26
29
  internalFieldStore.errors = createSignal(null);
30
+ internalFieldStore.isTouched = createSignal(false);
31
+ internalFieldStore.isDirty = createSignal(false);
27
32
  if (schema.type === "array" || schema.type === "loose_tuple" || schema.type === "strict_tuple" || schema.type === "tuple") {
28
33
  if (internalFieldStore.kind && internalFieldStore.kind !== "array") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "array"`);
29
34
  internalFieldStore.kind = "array";
@@ -42,12 +47,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
42
47
  initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput?.[index], path);
43
48
  path.pop();
44
49
  }
50
+ const arrayInput = nullish && initialInput == null ? initialInput : true;
51
+ internalFieldStore.initialInput = createSignal(arrayInput);
52
+ internalFieldStore.startInput = createSignal(arrayInput);
53
+ internalFieldStore.input = createSignal(arrayInput);
45
54
  const initialItems = internalFieldStore.children.map(createId);
46
55
  internalFieldStore.initialItems = createSignal(initialItems);
47
56
  internalFieldStore.startItems = createSignal(initialItems);
48
57
  internalFieldStore.items = createSignal(initialItems);
49
- internalFieldStore.isTouched = createSignal(false);
50
- internalFieldStore.isDirty = createSignal(false);
51
58
  }
52
59
  } else if (schema.type === "loose_object" || schema.type === "object" || schema.type === "strict_object") {
53
60
  if (internalFieldStore.kind && internalFieldStore.kind !== "object") throw new Error(`Store initialized as "${internalFieldStore.kind}" cannot be reinitialized as "object"`);
@@ -60,6 +67,10 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
60
67
  initializeFieldStore(internalFieldStore.children[key], schema.entries[key], initialInput?.[key], path);
61
68
  path.pop();
62
69
  }
70
+ const objectInput = nullish && initialInput == null ? initialInput : true;
71
+ internalFieldStore.initialInput = createSignal(objectInput);
72
+ internalFieldStore.startInput = createSignal(objectInput);
73
+ internalFieldStore.input = createSignal(objectInput);
63
74
  }
64
75
  } else {
65
76
  internalFieldStore.kind = "value";
@@ -67,8 +78,6 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
67
78
  internalFieldStore.initialInput = createSignal(initialInput);
68
79
  internalFieldStore.startInput = createSignal(initialInput);
69
80
  internalFieldStore.input = createSignal(initialInput);
70
- internalFieldStore.isTouched = createSignal(false);
71
- internalFieldStore.isDirty = createSignal(false);
72
81
  }
73
82
  }
74
83
  }
@@ -85,10 +94,14 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path) {
85
94
  function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
86
95
  batch(() => {
87
96
  untrack(() => {
97
+ toInternalFieldStore.elements = fromInternalFieldStore.elements;
98
+ toInternalFieldStore.errors.value = fromInternalFieldStore.errors.value;
99
+ toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
100
+ toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
101
+ toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
102
+ toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
88
103
  if (fromInternalFieldStore.kind === "array" && toInternalFieldStore.kind === "array") {
89
104
  const fromItems = fromInternalFieldStore.items.value;
90
- toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
91
- toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
92
105
  toInternalFieldStore.startItems.value = fromInternalFieldStore.startItems.value;
93
106
  toInternalFieldStore.items.value = fromItems;
94
107
  let path;
@@ -103,12 +116,6 @@ function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
103
116
  copyItemState(fromInternalFieldStore.children[index], toInternalFieldStore.children[index]);
104
117
  }
105
118
  } else if (fromInternalFieldStore.kind === "object" && toInternalFieldStore.kind === "object") for (const key in fromInternalFieldStore.children) copyItemState(fromInternalFieldStore.children[key], toInternalFieldStore.children[key]);
106
- else if (fromInternalFieldStore.kind === "value" && toInternalFieldStore.kind === "value") {
107
- toInternalFieldStore.isTouched.value = fromInternalFieldStore.isTouched.value;
108
- toInternalFieldStore.isDirty.value = fromInternalFieldStore.isDirty.value;
109
- toInternalFieldStore.startInput.value = fromInternalFieldStore.startInput.value;
110
- toInternalFieldStore.input.value = fromInternalFieldStore.input.value;
111
- }
112
119
  });
113
120
  });
114
121
  }
@@ -123,11 +130,15 @@ function copyItemState(fromInternalFieldStore, toInternalFieldStore) {
123
130
  */
124
131
  function resetItemState(internalFieldStore, initialInput) {
125
132
  batch(() => {
133
+ internalFieldStore.elements = [];
126
134
  internalFieldStore.errors.value = null;
127
- if (internalFieldStore.kind === "array") {
128
- internalFieldStore.isTouched.value = false;
129
- internalFieldStore.isDirty.value = false;
130
- if (initialInput) {
135
+ internalFieldStore.isTouched.value = false;
136
+ internalFieldStore.isDirty.value = false;
137
+ if (internalFieldStore.kind === "array" || internalFieldStore.kind === "object") {
138
+ const objectInput = initialInput == null ? initialInput : true;
139
+ internalFieldStore.startInput.value = objectInput;
140
+ internalFieldStore.input.value = objectInput;
141
+ if (internalFieldStore.kind === "array") if (initialInput) {
131
142
  const newItems = initialInput.map(createId);
132
143
  internalFieldStore.startItems.value = newItems;
133
144
  internalFieldStore.items.value = newItems;
@@ -136,10 +147,8 @@ function resetItemState(internalFieldStore, initialInput) {
136
147
  internalFieldStore.startItems.value = [];
137
148
  internalFieldStore.items.value = [];
138
149
  }
139
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) resetItemState(internalFieldStore.children[key], initialInput?.[key]);
140
- else {
141
- internalFieldStore.isTouched.value = false;
142
- internalFieldStore.isDirty.value = false;
150
+ else for (const key in internalFieldStore.children) resetItemState(internalFieldStore.children[key], initialInput?.[key]);
151
+ } else {
143
152
  internalFieldStore.startInput.value = initialInput;
144
153
  internalFieldStore.input.value = initialInput;
145
154
  }
@@ -156,15 +165,27 @@ function resetItemState(internalFieldStore, initialInput) {
156
165
  function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
157
166
  batch(() => {
158
167
  untrack(() => {
168
+ const tempElements = firstInternalFieldStore.elements;
169
+ firstInternalFieldStore.elements = secondInternalFieldStore.elements;
170
+ secondInternalFieldStore.elements = tempElements;
171
+ const tempErrors = firstInternalFieldStore.errors.value;
172
+ firstInternalFieldStore.errors.value = secondInternalFieldStore.errors.value;
173
+ secondInternalFieldStore.errors.value = tempErrors;
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
+ const tempIsTouched = firstInternalFieldStore.isTouched.value;
181
+ firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
182
+ secondInternalFieldStore.isTouched.value = tempIsTouched;
183
+ const tempIsDirty = firstInternalFieldStore.isDirty.value;
184
+ firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
185
+ secondInternalFieldStore.isDirty.value = tempIsDirty;
159
186
  if (firstInternalFieldStore.kind === "array" && secondInternalFieldStore.kind === "array") {
160
187
  const firstItems = firstInternalFieldStore.items.value;
161
188
  const secondItems = secondInternalFieldStore.items.value;
162
- const tempIsTouched = firstInternalFieldStore.isTouched.value;
163
- firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
164
- secondInternalFieldStore.isTouched.value = tempIsTouched;
165
- const tempIsDirty = firstInternalFieldStore.isDirty.value;
166
- firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
167
- secondInternalFieldStore.isDirty.value = tempIsDirty;
168
189
  const tempStartItems = firstInternalFieldStore.startItems.value;
169
190
  firstInternalFieldStore.startItems.value = secondInternalFieldStore.startItems.value;
170
191
  secondInternalFieldStore.startItems.value = tempStartItems;
@@ -191,33 +212,25 @@ function swapItemState(firstInternalFieldStore, secondInternalFieldStore) {
191
212
  swapItemState(firstInternalFieldStore.children[index], secondInternalFieldStore.children[index]);
192
213
  }
193
214
  } else if (firstInternalFieldStore.kind === "object" && secondInternalFieldStore.kind === "object") for (const key in firstInternalFieldStore.children) swapItemState(firstInternalFieldStore.children[key], secondInternalFieldStore.children[key]);
194
- else if (firstInternalFieldStore.kind === "value" && secondInternalFieldStore.kind === "value") {
195
- const tempIsTouched = firstInternalFieldStore.isTouched.value;
196
- firstInternalFieldStore.isTouched.value = secondInternalFieldStore.isTouched.value;
197
- secondInternalFieldStore.isTouched.value = tempIsTouched;
198
- const tempIsDirty = firstInternalFieldStore.isDirty.value;
199
- firstInternalFieldStore.isDirty.value = secondInternalFieldStore.isDirty.value;
200
- secondInternalFieldStore.isDirty.value = tempIsDirty;
201
- const tempStartInput = firstInternalFieldStore.startInput.value;
202
- firstInternalFieldStore.startInput.value = secondInternalFieldStore.startInput.value;
203
- secondInternalFieldStore.startInput.value = tempStartInput;
204
- const tempInput = firstInternalFieldStore.input.value;
205
- firstInternalFieldStore.input.value = secondInternalFieldStore.input.value;
206
- secondInternalFieldStore.input.value = tempInput;
207
- }
208
215
  });
209
216
  });
210
217
  }
211
218
  function getFieldInput(internalFieldStore) {
212
219
  if (internalFieldStore.kind === "array") {
213
- const value = [];
214
- for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = getFieldInput(internalFieldStore.children[index]);
215
- return value;
220
+ if (internalFieldStore.input.value) {
221
+ const value = [];
222
+ for (let index = 0; index < internalFieldStore.items.value.length; index++) value[index] = getFieldInput(internalFieldStore.children[index]);
223
+ return value;
224
+ }
225
+ return internalFieldStore.input.value;
216
226
  }
217
227
  if (internalFieldStore.kind === "object") {
218
- const value = {};
219
- for (const key in internalFieldStore.children) value[key] = getFieldInput(internalFieldStore.children[key]);
220
- return value;
228
+ if (internalFieldStore.input.value) {
229
+ const value = {};
230
+ for (const key in internalFieldStore.children) value[key] = getFieldInput(internalFieldStore.children[key]);
231
+ return value;
232
+ }
233
+ return internalFieldStore.input.value;
221
234
  }
222
235
  return internalFieldStore.input.value;
223
236
  }
@@ -248,17 +261,16 @@ function getElementInput(element, internalFieldStore) {
248
261
  return element.value;
249
262
  }
250
263
  function getFieldBool(internalFieldStore, type) {
264
+ if (internalFieldStore[type].value) return true;
251
265
  if (internalFieldStore.kind === "array") {
252
- if (internalFieldStore[type].value) return true;
253
266
  for (let index = 0; index < internalFieldStore.items.value.length; index++) if (getFieldBool(internalFieldStore.children[index], type)) return true;
254
267
  return false;
255
268
  }
256
269
  if (internalFieldStore.kind == "object") {
257
- if (type === "errors" && internalFieldStore[type].value) return true;
258
270
  for (const key in internalFieldStore.children) if (getFieldBool(internalFieldStore.children[key], type)) return true;
259
271
  return false;
260
272
  }
261
- return !!internalFieldStore[type].value;
273
+ return false;
262
274
  }
263
275
  function getFieldStore(internalFormStore, path) {
264
276
  let internalFieldStore = internalFormStore;
@@ -274,38 +286,53 @@ function setFieldBool(internalFieldStore, type, bool) {
274
286
  else internalFieldStore[type].value = bool;
275
287
  });
276
288
  }
277
- function setFieldInput(internalFieldStore, input) {
278
- batch(() => {
279
- if (internalFieldStore.kind === "array") {
280
- const arrayInput = input ?? [];
281
- const items = untrack(() => internalFieldStore.items.value);
282
- if (arrayInput.length < items.length) internalFieldStore.items.value = items.slice(0, arrayInput.length);
283
- else if (arrayInput.length > items.length) {
284
- if (arrayInput.length > internalFieldStore.children.length) {
285
- const path = JSON.parse(internalFieldStore.name);
286
- for (let index = internalFieldStore.children.length; index < arrayInput.length; index++) {
287
- internalFieldStore.children[index] = {};
288
- path.push(index);
289
- initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], path);
290
- path.pop();
291
- }
289
+ function setNestedInput(internalFieldStore, input) {
290
+ internalFieldStore.isTouched.value = true;
291
+ if (internalFieldStore.kind === "array") {
292
+ const arrayInput = input ?? [];
293
+ const items = internalFieldStore.items.value;
294
+ if (arrayInput.length < items.length) internalFieldStore.items.value = items.slice(0, arrayInput.length);
295
+ else if (arrayInput.length > items.length) {
296
+ if (arrayInput.length > internalFieldStore.children.length) {
297
+ const path = JSON.parse(internalFieldStore.name);
298
+ for (let index = internalFieldStore.children.length; index < arrayInput.length; index++) {
299
+ internalFieldStore.children[index] = {};
300
+ path.push(index);
301
+ initializeFieldStore(internalFieldStore.children[index], internalFieldStore.schema.item, arrayInput[index], path);
302
+ path.pop();
292
303
  }
293
- internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createId)];
294
304
  }
295
- for (let index = 0; index < items.length; index++) setFieldInput(internalFieldStore.children[index], arrayInput[index]);
296
- internalFieldStore.isDirty.value = untrack(() => internalFieldStore.startItems.value).length !== items.length;
297
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setFieldInput(internalFieldStore.children[key], input?.[key]);
298
- else {
299
- internalFieldStore.input.value = input;
300
- internalFieldStore.isTouched.value = true;
301
- const startInput = untrack(() => internalFieldStore.startInput.value);
302
- internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
305
+ internalFieldStore.items.value = [...items, ...arrayInput.slice(items.length).map(createId)];
303
306
  }
307
+ for (let index = 0; index < items.length; index++) setNestedInput(internalFieldStore.children[index], arrayInput[index]);
308
+ internalFieldStore.input.value = input == null ? input : true;
309
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== items.length;
310
+ } else if (internalFieldStore.kind === "object") {
311
+ for (const key in internalFieldStore.children) setNestedInput(internalFieldStore.children[key], input?.[key]);
312
+ internalFieldStore.input.value = input == null ? input : true;
313
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value;
314
+ } else {
315
+ internalFieldStore.input.value = input;
316
+ const startInput = internalFieldStore.startInput.value;
317
+ internalFieldStore.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
318
+ }
319
+ }
320
+ function setFieldInput(internalFormStore, path, input) {
321
+ batch(() => {
322
+ untrack(() => {
323
+ let internalFieldStore = internalFormStore;
324
+ for (let index = 0; index < path.length; index++) {
325
+ internalFieldStore = internalFieldStore.children[path[index]];
326
+ if (index < path.length - 1) internalFieldStore.input.value = true;
327
+ else setNestedInput(internalFieldStore, input);
328
+ }
329
+ });
304
330
  });
305
331
  }
306
332
  function setInitialFieldInput(internalFieldStore, initialInput) {
307
333
  batch(() => {
308
334
  if (internalFieldStore.kind === "array") {
335
+ internalFieldStore.input.value = initialInput == null ? initialInput : true;
309
336
  const initialArrayInput = initialInput ?? [];
310
337
  if (initialArrayInput.length > internalFieldStore.children.length) {
311
338
  const path = JSON.parse(internalFieldStore.name);
@@ -318,8 +345,10 @@ function setInitialFieldInput(internalFieldStore, initialInput) {
318
345
  }
319
346
  internalFieldStore.initialItems.value = initialArrayInput.map(createId);
320
347
  for (let index = 0; index < internalFieldStore.children.length; index++) setInitialFieldInput(internalFieldStore.children[index], initialArrayInput[index]);
321
- } else if (internalFieldStore.kind === "object") for (const key in internalFieldStore.children) setInitialFieldInput(internalFieldStore.children[key], initialInput?.[key]);
322
- else internalFieldStore.initialInput.value = initialInput;
348
+ } else if (internalFieldStore.kind === "object") {
349
+ internalFieldStore.input.value = initialInput == null ? initialInput : true;
350
+ for (const key in internalFieldStore.children) setInitialFieldInput(internalFieldStore.children[key], initialInput?.[key]);
351
+ } else internalFieldStore.initialInput.value = initialInput;
323
352
  });
324
353
  }
325
354
  function walkFieldStore(internalFieldStore, callback) {
@@ -380,8 +409,8 @@ async function validateFormInput(internalFormStore, config) {
380
409
  });
381
410
  return result;
382
411
  }
383
- function validateIfRequired(internalFormStore, internalFieldStore, validationModes) {
384
- if (validationModes === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
412
+ function validateIfRequired(internalFormStore, internalFieldStore, validationMode) {
413
+ if (validationMode === (internalFormStore.validate === "initial" || (internalFormStore.validate === "submit" ? untrack(() => internalFormStore.isSubmitted.value) : untrack(() => getFieldBool(internalFieldStore, "errors"))) ? internalFormStore.revalidate : internalFormStore.validate)) validateFormInput(internalFormStore);
385
414
  }
386
415
  const INTERNAL = "~internal";
387
416
 
@@ -423,7 +452,12 @@ function handleSubmit(form, handler) {
423
452
  }
424
453
  function insert(form, config) {
425
454
  const internalFormStore = form[INTERNAL];
426
- const internalArrayStore = getFieldStore(internalFormStore, config.path);
455
+ let internalFieldStore = internalFormStore;
456
+ for (let index = 0; index < config.path.length; index++) {
457
+ internalFieldStore = internalFieldStore.children[config.path[index]];
458
+ if (index < config.path.length - 1) internalFieldStore.input.value = true;
459
+ }
460
+ const internalArrayStore = internalFieldStore;
427
461
  const items = untrack(() => internalArrayStore.items.value);
428
462
  const insertIndex = config.at === void 0 ? items.length : config.at;
429
463
  if (insertIndex >= 0 && insertIndex <= items.length) batch(() => {
@@ -437,6 +471,7 @@ function insert(form, config) {
437
471
  path.push(insertIndex);
438
472
  initializeFieldStore(internalArrayStore.children[insertIndex], internalArrayStore.schema.item, config.initialInput, path);
439
473
  } else resetItemState(internalArrayStore.children[insertIndex], config.initialInput);
474
+ internalArrayStore.input.value = true;
440
475
  internalArrayStore.isTouched.value = true;
441
476
  internalArrayStore.isDirty.value = true;
442
477
  validateIfRequired(internalFormStore, internalArrayStore, "input");
@@ -496,17 +531,20 @@ function reset(form, config) {
496
531
  const internalFieldStore = config?.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
497
532
  if (config?.initialInput) setInitialFieldInput(internalFieldStore, config.initialInput);
498
533
  walkFieldStore(internalFieldStore, (internalFieldStore$1) => {
534
+ internalFieldStore$1.elements = internalFieldStore$1.initialElements;
499
535
  if (!config?.keepErrors) internalFieldStore$1.errors.value = null;
536
+ if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
537
+ internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
538
+ if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
500
539
  if (internalFieldStore$1.kind === "array") {
501
540
  internalFieldStore$1.startItems.value = internalFieldStore$1.initialItems.value;
502
541
  if (!config?.keepInput || internalFieldStore$1.startItems.value.length === internalFieldStore$1.items.value.length) internalFieldStore$1.items.value = internalFieldStore$1.initialItems.value;
503
- if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
504
- internalFieldStore$1.isDirty.value = internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
505
- } else if (internalFieldStore$1.kind === "value") {
506
- internalFieldStore$1.startInput.value = internalFieldStore$1.initialInput.value;
507
- if (!config?.keepInput) internalFieldStore$1.input.value = internalFieldStore$1.initialInput.value;
508
- if (!config?.keepTouched) internalFieldStore$1.isTouched.value = false;
509
- internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
542
+ internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value || internalFieldStore$1.startItems.value !== internalFieldStore$1.items.value;
543
+ } else if (internalFieldStore$1.kind === "object") internalFieldStore$1.isDirty.value = internalFieldStore$1.startInput.value !== internalFieldStore$1.input.value;
544
+ else {
545
+ const startInput = internalFieldStore$1.startInput.value;
546
+ const input = internalFieldStore$1.input.value;
547
+ internalFieldStore$1.isDirty.value = startInput !== input && (startInput !== void 0 || input !== "" && !Number.isNaN(input));
510
548
  for (const element of internalFieldStore$1.elements) if (element.type === "file") element.value = "";
511
549
  }
512
550
  });
@@ -523,9 +561,8 @@ function setErrors(form, config) {
523
561
  function setInput(form, config) {
524
562
  batch(() => {
525
563
  const internalFormStore = form[INTERNAL];
526
- const internalFieldStore = config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
527
- setFieldInput(internalFieldStore, config.input);
528
- validateIfRequired(internalFormStore, internalFieldStore, "input");
564
+ setFieldInput(internalFormStore, config.path ?? [], config.input);
565
+ validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
529
566
  });
530
567
  }
531
568
  function submit(form) {
@@ -568,7 +605,8 @@ function usePathSignal(path) {
568
605
  //#region src/hooks/useField/useField.ts
569
606
  function useField(form, config) {
570
607
  const pathSignal = usePathSignal(config.path);
571
- const internalFieldStore = useComputed$(() => getFieldStore(form[INTERNAL], pathSignal.value));
608
+ const internalFormStore = form[INTERNAL];
609
+ const internalFieldStore = useComputed$(() => getFieldStore(internalFormStore, pathSignal.value));
572
610
  useTask$(({ track, cleanup }) => {
573
611
  track(internalFieldStore);
574
612
  cleanup(() => {
@@ -592,17 +630,17 @@ function useField(form, config) {
592
630
  }),
593
631
  onFocus$: $(() => {
594
632
  setFieldBool(internalFieldStore.value, "isTouched", true);
595
- validateIfRequired(form[INTERNAL], internalFieldStore.value, "touch");
633
+ validateIfRequired(internalFormStore, internalFieldStore.value, "touch");
596
634
  }),
597
635
  onInput$: $((_, element) => {
598
- setFieldInput(internalFieldStore.value, getElementInput(element, internalFieldStore.value));
599
- validateIfRequired(form[INTERNAL], internalFieldStore.value, "input");
636
+ setFieldInput(internalFormStore, pathSignal.value, getElementInput(element, internalFieldStore.value));
637
+ validateIfRequired(internalFormStore, internalFieldStore.value, "input");
600
638
  }),
601
639
  onChange$: $(() => {
602
- validateIfRequired(form[INTERNAL], internalFieldStore.value, "change");
640
+ validateIfRequired(internalFormStore, internalFieldStore.value, "change");
603
641
  }),
604
642
  onBlur$: $(() => {
605
- validateIfRequired(form[INTERNAL], internalFieldStore.value, "blur");
643
+ validateIfRequired(internalFormStore, internalFieldStore.value, "blur");
606
644
  })
607
645
  }
608
646
  }));
@@ -669,7 +707,8 @@ const useForm$ = implicit$FirstArg(useFormQrl);
669
707
  */
670
708
  const Field = component$(({ of, path, render$ }) => {
671
709
  const field = useField(of, { path });
672
- return useResolvedQrl(render$)(field);
710
+ const render = useResolvedQrl(render$);
711
+ return render(field);
673
712
  });
674
713
 
675
714
  //#endregion
@@ -679,7 +718,8 @@ const Field = component$(({ of, path, render$ }) => {
679
718
  */
680
719
  const FieldArray = component$(({ of, path, render$ }) => {
681
720
  const field = useFieldArray(of, { path });
682
- return useResolvedQrl(render$)(field);
721
+ const render = useResolvedQrl(render$);
722
+ return render(field);
683
723
  });
684
724
 
685
725
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formisch/qwik",
3
3
  "description": "The modular and type-safe form library for Qwik",
4
- "version": "0.3.1",
4
+ "version": "0.4.0",
5
5
  "license": "MIT",
6
6
  "author": "Fabian Hiller",
7
7
  "homepage": "https://formisch.dev",
@@ -49,8 +49,8 @@
49
49
  "valibot": "^1.1.0",
50
50
  "vite": "7.0.4",
51
51
  "vite-tsconfig-paths": "^5.1.4",
52
- "@formisch/core": "0.3.1",
53
- "@formisch/methods": "0.2.0"
52
+ "@formisch/core": "0.4.0",
53
+ "@formisch/methods": "0.3.0"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "@qwik.dev/core": ">=2",