@vizejs/composable 0.808.0 → 0.810.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.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/{use-form-C2JXZ0i7.mjs → use-form-A4j_h14D.mjs} +168 -27
- package/dist/use-form.d.mts +123 -18
- package/dist/use-form.mjs +1 -1
- package/package.json +2 -2
- package/registry/files/form-control-types.ts +260 -0
- package/registry/files/form-dirty.ts +131 -0
- package/registry/files/form-options.ts +156 -0
- package/registry/files/form-path-types.ts +135 -0
- package/registry/files/use-form.ts +102 -449
- package/registry/registry.json +28 -4
package/dist/use-form.d.mts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { d as StandardSchemaV1 } from "./standard-schema-DmNgFVK5.mjs";
|
|
2
|
-
import { ComputedRef, Ref, ShallowRef, WritableComputedRef } from "vue";
|
|
2
|
+
import { ComputedRef, MaybeRefOrGetter, Ref, ShallowRef, WritableComputedRef } from "vue";
|
|
3
3
|
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/form-path-types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Path and error types for {@link useForm}.
|
|
7
|
+
*
|
|
8
|
+
* Paths are template-literal keys, bounded at eight levels so type checking
|
|
9
|
+
* stays fast. Leaves stop the walk.
|
|
10
|
+
*/
|
|
5
11
|
/** Values treated as leaves: paths never descend into them. */
|
|
6
12
|
type FormLeaf = string | number | bigint | boolean | symbol | null | undefined | Date | RegExp | Blob | Map<unknown, unknown> | Set<unknown> | ((...arguments_: never[]) => unknown);
|
|
7
13
|
type Decrement = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
@@ -15,7 +21,7 @@ type Decrement = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
15
21
|
type FormPath<Values, Depth extends number = 8> = [Depth] extends [never] ? never : Values extends FormLeaf ? never : Values extends readonly (infer Item)[] ? `${number}` | `${number}.${FormPath<Item, Decrement[Depth]>}` : { [Key in keyof Values & string]: Key | (FormPath<Values[Key], Decrement[Depth]> extends infer Rest extends string ? [Rest] extends [never] ? never : `${Key}.${Rest}` : never) }[keyof Values & string];
|
|
16
22
|
/** Value type found at `Path` inside `Values`. */
|
|
17
23
|
type FormPathValue<Values, Path extends string> = Path extends `${infer Head}.${infer Rest}` ? Values extends readonly (infer Item)[] ? Head extends `${number}` ? FormPathValue<Item, Rest> : never : Head extends keyof Values ? FormPathValue<Values[Head], Rest> : never : Values extends readonly (infer Item)[] ? Path extends `${number}` ? Item : never : Path extends keyof Values ? Values[Path] : never;
|
|
18
|
-
/** Paths whose value is an array (usable with
|
|
24
|
+
/** Paths whose value is an array (usable with field arrays). */
|
|
19
25
|
type FormArrayPath<Values> = { [Path in FormPath<Values>]: FormPathValue<Values, Path> extends readonly unknown[] ? Path : never }[FormPath<Values>];
|
|
20
26
|
/** Item type of the array at `Path`. */
|
|
21
27
|
type FormArrayItem<Values, Path extends string> = FormPathValue<Values, Path> extends readonly (infer Item)[] ? Item : never;
|
|
@@ -49,19 +55,32 @@ type FormValidator<Values> = (values: Values, context: {
|
|
|
49
55
|
}) => FormErrors<Values> | undefined | void | Promise<FormErrors<Values> | undefined | void>;
|
|
50
56
|
/** Per-field validators keyed by path. */
|
|
51
57
|
type FormFieldValidators<Values> = { readonly [Path in FormPath<Values>]?: FieldValidator<FormPathValue<Values, Path>, Values> };
|
|
58
|
+
/** One dirty branch: `true` for a leaf, or a nested map when children differ. */
|
|
59
|
+
type DirtyNode<Values> = true | DirtyFields<Values>;
|
|
60
|
+
/**
|
|
61
|
+
* Nested dirty flags against the initial values.
|
|
62
|
+
*
|
|
63
|
+
* A dirty leaf is `true`. Clean branches are omitted. Array indexes stay
|
|
64
|
+
* aligned with the value, and a clean item is `undefined`.
|
|
65
|
+
*/
|
|
66
|
+
type DirtyFields<Values> = Values extends FormLeaf ? true : Values extends readonly (infer Item)[] ? readonly (DirtyNode<Item> | undefined)[] : Values extends object ? { readonly [Key in keyof Values]?: DirtyNode<Values[Key]> } : true;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/form-options.d.ts
|
|
52
69
|
/** When validation runs automatically. */
|
|
53
70
|
type FormValidationTrigger = "change" | "blur" | "submit";
|
|
54
|
-
/**
|
|
71
|
+
/** How many messages each path keeps after validation. */
|
|
72
|
+
type FormCriteriaMode = "all" | "first";
|
|
73
|
+
/** Discriminated outcome of validation and submission. */
|
|
55
74
|
type FormResult<Output> = {
|
|
56
75
|
/** Every validator passed. */readonly status: "valid"; /** Schema output, or the values when no schema is configured. */
|
|
57
76
|
readonly value: Output;
|
|
58
77
|
} | {
|
|
59
78
|
/** At least one validator failed. */readonly status: "invalid"; /** Errors keyed by path. */
|
|
60
|
-
readonly errors:
|
|
79
|
+
readonly errors: Readonly<Record<string, readonly string[]>>;
|
|
61
80
|
} | {
|
|
62
81
|
/** A newer validation started before this one finished. */readonly status: "superseded";
|
|
63
82
|
};
|
|
64
|
-
/** Options for
|
|
83
|
+
/** Options for `useForm`. */
|
|
65
84
|
interface UseFormOptions<Values extends object, Output> {
|
|
66
85
|
/** Initial values, or a factory producing fresh initial values. */
|
|
67
86
|
readonly initialValues: Values | (() => Values);
|
|
@@ -97,7 +116,21 @@ interface UseFormOptions<Values extends object, Output> {
|
|
|
97
116
|
*/
|
|
98
117
|
readonly revalidateOn?: FormValidationTrigger;
|
|
99
118
|
/**
|
|
100
|
-
*
|
|
119
|
+
* `"all"` records every message. `"first"` keeps the earliest message in
|
|
120
|
+
* source order: schema, then the form validator, then the field validator.
|
|
121
|
+
*
|
|
122
|
+
* @default "all"
|
|
123
|
+
*/
|
|
124
|
+
readonly criteriaMode?: FormCriteriaMode;
|
|
125
|
+
/**
|
|
126
|
+
* When true, value writes, field-array edits, and submission do nothing.
|
|
127
|
+
* `setError`, `setErrors`, and `reset` still run. A ref or getter tracks.
|
|
128
|
+
*
|
|
129
|
+
* @default false
|
|
130
|
+
*/
|
|
131
|
+
readonly disabled?: MaybeRefOrGetter<boolean>;
|
|
132
|
+
/**
|
|
133
|
+
* Default submit handler for `submit`.
|
|
101
134
|
*
|
|
102
135
|
* @default undefined
|
|
103
136
|
*/
|
|
@@ -105,7 +138,7 @@ interface UseFormOptions<Values extends object, Output> {
|
|
|
105
138
|
readonly signal: AbortSignal;
|
|
106
139
|
}) => unknown;
|
|
107
140
|
}
|
|
108
|
-
/** Options for
|
|
141
|
+
/** Options for `setValue`. */
|
|
109
142
|
interface SetFieldValueOptions {
|
|
110
143
|
/**
|
|
111
144
|
* Mark the field touched.
|
|
@@ -120,7 +153,45 @@ interface SetFieldValueOptions {
|
|
|
120
153
|
*/
|
|
121
154
|
readonly validate?: boolean;
|
|
122
155
|
}
|
|
123
|
-
/**
|
|
156
|
+
/**
|
|
157
|
+
* What {@link FormControls.reset} retains. Every omitted flag clears that state.
|
|
158
|
+
*/
|
|
159
|
+
interface FormResetOptions {
|
|
160
|
+
/**
|
|
161
|
+
* Keep values that differ from the previous initial values. Clean fields
|
|
162
|
+
* take the reset values. Added keys stay, and removed keys stay removed.
|
|
163
|
+
*
|
|
164
|
+
* @default false
|
|
165
|
+
*/
|
|
166
|
+
readonly keepDirtyValues?: boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Keep the current errors.
|
|
169
|
+
*
|
|
170
|
+
* @default false
|
|
171
|
+
*/
|
|
172
|
+
readonly keepErrors?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Keep touched fields.
|
|
175
|
+
*
|
|
176
|
+
* @default false
|
|
177
|
+
*/
|
|
178
|
+
readonly keepTouched?: boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Keep the submit count.
|
|
181
|
+
*
|
|
182
|
+
* @default false
|
|
183
|
+
*/
|
|
184
|
+
readonly keepSubmitCount?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Keep whether the latest finished submit succeeded.
|
|
187
|
+
*
|
|
188
|
+
* @default false
|
|
189
|
+
*/
|
|
190
|
+
readonly keepIsSubmitSuccessful?: boolean;
|
|
191
|
+
}
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/form-control-types.d.ts
|
|
194
|
+
/** Reactive controls for one field, returned by `field`. */
|
|
124
195
|
interface FormFieldControls<Value, Path extends string = string> {
|
|
125
196
|
/** Dotted path of the field (also a suitable `name` attribute). */
|
|
126
197
|
readonly path: Path;
|
|
@@ -158,7 +229,7 @@ interface FormArrayEntry<Item, Path extends string = string> {
|
|
|
158
229
|
/** Current item value. */
|
|
159
230
|
readonly value: Item;
|
|
160
231
|
}
|
|
161
|
-
/** Typed operations on an array field, returned by
|
|
232
|
+
/** Typed operations on an array field, returned by `fieldArray`. */
|
|
162
233
|
interface FormFieldArrayControls<Item, Path extends string = string> {
|
|
163
234
|
/** Entries with stable keys. */
|
|
164
235
|
readonly entries: ComputedRef<readonly FormArrayEntry<Item, Path>[]>;
|
|
@@ -177,7 +248,7 @@ interface FormFieldArrayControls<Item, Path extends string = string> {
|
|
|
177
248
|
/** Replace all items and reset their state. */
|
|
178
249
|
readonly replace: (items: readonly Item[]) => void;
|
|
179
250
|
}
|
|
180
|
-
/** Reactive state and actions returned by
|
|
251
|
+
/** Reactive state and actions returned by `useForm`. */
|
|
181
252
|
interface FormControls<Values extends object, Output> {
|
|
182
253
|
/** Current values. Mutations are tracked (dirty state derives from them). */
|
|
183
254
|
readonly values: Ref<Values>;
|
|
@@ -187,12 +258,21 @@ interface FormControls<Values extends object, Output> {
|
|
|
187
258
|
readonly valid: ComputedRef<boolean>;
|
|
188
259
|
/** Whether any value differs from its initial value. */
|
|
189
260
|
readonly dirty: ComputedRef<boolean>;
|
|
261
|
+
/** Nested flags for values that differ from the initial values. */
|
|
262
|
+
readonly dirtyFields: ComputedRef<DirtyFields<Values>>;
|
|
190
263
|
/** Whether any validation is pending. */
|
|
191
264
|
readonly validating: ComputedRef<boolean>;
|
|
192
265
|
/** Whether a submission is in progress. */
|
|
193
266
|
readonly submitting: Readonly<Ref<boolean>>;
|
|
194
|
-
/** Number of submit attempts. */
|
|
267
|
+
/** Number of submit attempts. Disabled submits are not counted. */
|
|
195
268
|
readonly submitCount: Readonly<Ref<number>>;
|
|
269
|
+
/**
|
|
270
|
+
* Whether the latest finished submit was valid and its handler settled.
|
|
271
|
+
* A superseded submit leaves this flag unchanged.
|
|
272
|
+
*/
|
|
273
|
+
readonly isSubmitSuccessful: Readonly<Ref<boolean>>;
|
|
274
|
+
/** Whether value writes, field-array edits, and submission are ignored. */
|
|
275
|
+
readonly disabled: ComputedRef<boolean>;
|
|
196
276
|
/**
|
|
197
277
|
* Typed controls for one field.
|
|
198
278
|
*
|
|
@@ -215,7 +295,14 @@ interface FormControls<Values extends object, Output> {
|
|
|
215
295
|
*/
|
|
216
296
|
readonly getValue: <Path extends FormPath<Values>>(path: Path) => FormPathValue<Values, Path>;
|
|
217
297
|
/**
|
|
218
|
-
*
|
|
298
|
+
* Read a path as a computed ref that tracks later edits.
|
|
299
|
+
*
|
|
300
|
+
* @param path Dotted path.
|
|
301
|
+
* @returns The value at that path.
|
|
302
|
+
*/
|
|
303
|
+
readonly watch: <Path extends FormPath<Values>>(path: Path) => ComputedRef<FormPathValue<Values, Path>>;
|
|
304
|
+
/**
|
|
305
|
+
* Write the value at a path. Does nothing while `disabled` is true.
|
|
219
306
|
*
|
|
220
307
|
* @param path Dotted path.
|
|
221
308
|
* @param value New value.
|
|
@@ -224,6 +311,7 @@ interface FormControls<Values extends object, Output> {
|
|
|
224
311
|
readonly setValue: <Path extends FormPath<Values>>(path: Path, value: FormPathValue<Values, Path>, options?: SetFieldValueOptions) => void;
|
|
225
312
|
/**
|
|
226
313
|
* Replace (or shallowly merge) the values without touching initial values.
|
|
314
|
+
* Does nothing while `disabled` is true.
|
|
227
315
|
*
|
|
228
316
|
* @param values New values.
|
|
229
317
|
* @param options `merge: true` shallowly merges top-level keys.
|
|
@@ -244,6 +332,13 @@ interface FormControls<Values extends object, Output> {
|
|
|
244
332
|
* @param errors Errors keyed by path.
|
|
245
333
|
*/
|
|
246
334
|
readonly setErrors: (errors: FormErrors<Values>) => void;
|
|
335
|
+
/**
|
|
336
|
+
* Set one path's errors without replacing the others.
|
|
337
|
+
*
|
|
338
|
+
* @param path Dotted path. `""` is the form-level error.
|
|
339
|
+
* @param message Messages for that path. An empty result clears it.
|
|
340
|
+
*/
|
|
341
|
+
readonly setError: (path: FormPath<Values> | "", message: ValidatorResult) => void;
|
|
247
342
|
/**
|
|
248
343
|
* Clear errors of one path, or all errors.
|
|
249
344
|
*
|
|
@@ -258,7 +353,8 @@ interface FormControls<Values extends object, Output> {
|
|
|
258
353
|
readonly validate: () => Promise<FormResult<Output>>;
|
|
259
354
|
/**
|
|
260
355
|
* Validate and, when valid, call `handler` (or the `onSubmit` option).
|
|
261
|
-
* A newer submit aborts an older one's pending validation.
|
|
356
|
+
* A newer submit aborts an older one's pending validation. Does nothing
|
|
357
|
+
* while `disabled` is true.
|
|
262
358
|
*
|
|
263
359
|
* @param handler Submit handler overriding `onSubmit`.
|
|
264
360
|
* @returns The validation result.
|
|
@@ -276,12 +372,16 @@ interface FormControls<Values extends object, Output> {
|
|
|
276
372
|
readonly signal: AbortSignal;
|
|
277
373
|
}) => unknown) => (event?: Event) => Promise<FormResult<Output>>;
|
|
278
374
|
/**
|
|
279
|
-
* Restore initial values (or install new ones)
|
|
375
|
+
* Restore initial values (or install new ones). Omitted keep-flags clear
|
|
376
|
+
* that piece of state. Still runs while `disabled` is true.
|
|
280
377
|
*
|
|
281
378
|
* @param values New initial values.
|
|
379
|
+
* @param options State to retain.
|
|
282
380
|
*/
|
|
283
|
-
readonly reset: (values?: Values) => void;
|
|
381
|
+
readonly reset: (values?: Values, options?: FormResetOptions) => void;
|
|
284
382
|
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/use-form.d.ts
|
|
285
385
|
/**
|
|
286
386
|
* Fully typed form state with validation, field arrays, and submission.
|
|
287
387
|
*
|
|
@@ -292,7 +392,12 @@ interface FormControls<Values extends object, Output> {
|
|
|
292
392
|
* … via the `~standard` protocol), a whole-form validator, and per-field
|
|
293
393
|
* validators; all may be asynchronous. A newer validation aborts older ones
|
|
294
394
|
* through their `signal` and their results are discarded, so a slow check
|
|
295
|
-
* can never overwrite fresher errors.
|
|
395
|
+
* can never overwrite fresher errors. `criteriaMode: "first"` keeps one
|
|
396
|
+
* message per path, in source order: schema, form validator, field validator.
|
|
397
|
+
* `dirtyFields` mirrors edited branches, `watch` reads one path, and
|
|
398
|
+
* `setError` updates a single path. `disabled` ignores writes and submission.
|
|
399
|
+
* `reset` can keep dirty values, errors, touched fields, the submit count,
|
|
400
|
+
* or the success flag.
|
|
296
401
|
*
|
|
297
402
|
* This composable owns the *values*. `@vizejs/ui/form` owns accessible
|
|
298
403
|
* error presentation (error summaries, focus management, native constraint
|
|
@@ -321,4 +426,4 @@ interface FormControls<Values extends object, Output> {
|
|
|
321
426
|
*/
|
|
322
427
|
declare function useForm<Values extends object, Output = Values>(options: UseFormOptions<Values, Output>): FormControls<Values, Output>;
|
|
323
428
|
//#endregion
|
|
324
|
-
export { FieldValidator, FieldValidatorContext, FormArrayEntry, FormArrayItem, FormArrayPath, FormControls, FormErrorRecord, FormErrors, FormFieldArrayControls, FormFieldControls, FormFieldValidators, FormLeaf, FormPath, FormPathValue, FormResult, FormValidationTrigger, FormValidator, SetFieldValueOptions, UseFormOptions, ValidatorResult, useForm };
|
|
429
|
+
export { type DirtyFields, type DirtyNode, type FieldValidator, type FieldValidatorContext, type FormArrayEntry, type FormArrayItem, type FormArrayPath, type FormControls, type FormCriteriaMode, type FormErrorRecord, type FormErrors, type FormFieldArrayControls, type FormFieldControls, type FormFieldValidators, type FormLeaf, type FormPath, type FormPathValue, type FormResetOptions, type FormResult, type FormValidationTrigger, type FormValidator, type SetFieldValueOptions, type UseFormOptions, type ValidatorResult, useForm };
|
package/dist/use-form.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizejs/composable",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.810.0",
|
|
4
4
|
"description": "Lifecycle-safe composable foundations for Vize applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"composables",
|
|
@@ -978,7 +978,7 @@
|
|
|
978
978
|
},
|
|
979
979
|
"devDependencies": {
|
|
980
980
|
"@types/node": "25.9.2",
|
|
981
|
-
"@vizejs/native": "0.
|
|
981
|
+
"@vizejs/native": "0.810.0",
|
|
982
982
|
"happy-dom": "21.0.4",
|
|
983
983
|
"typescript": "6.0.3",
|
|
984
984
|
"vite-plus": "0.1.24",
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public control surface returned by {@link useForm}.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ComputedRef, Ref, ShallowRef, WritableComputedRef } from "vue";
|
|
6
|
+
|
|
7
|
+
import type { FormResetOptions, FormResult, SetFieldValueOptions } from "./form-options.ts";
|
|
8
|
+
import type {
|
|
9
|
+
DirtyFields,
|
|
10
|
+
FormArrayItem,
|
|
11
|
+
FormArrayPath,
|
|
12
|
+
FormErrorRecord,
|
|
13
|
+
FormErrors,
|
|
14
|
+
FormPath,
|
|
15
|
+
FormPathValue,
|
|
16
|
+
ValidatorResult,
|
|
17
|
+
} from "./form-path-types.ts";
|
|
18
|
+
|
|
19
|
+
/** Reactive controls for one field, returned by `field`. */
|
|
20
|
+
export interface FormFieldControls<Value, Path extends string = string> {
|
|
21
|
+
/** Dotted path of the field (also a suitable `name` attribute). */
|
|
22
|
+
readonly path: Path;
|
|
23
|
+
|
|
24
|
+
/** Writable value bound to the form state (use with `v-model`). */
|
|
25
|
+
readonly value: WritableComputedRef<Value>;
|
|
26
|
+
|
|
27
|
+
/** Current error messages. */
|
|
28
|
+
readonly errors: ComputedRef<readonly string[]>;
|
|
29
|
+
|
|
30
|
+
/** First error message. */
|
|
31
|
+
readonly error: ComputedRef<string | undefined>;
|
|
32
|
+
|
|
33
|
+
/** Whether the value differs from its initial value. */
|
|
34
|
+
readonly dirty: ComputedRef<boolean>;
|
|
35
|
+
|
|
36
|
+
/** Whether the field was blurred or touched programmatically. */
|
|
37
|
+
readonly touched: ComputedRef<boolean>;
|
|
38
|
+
|
|
39
|
+
/** Whether a validation of this field is pending. */
|
|
40
|
+
readonly validating: ComputedRef<boolean>;
|
|
41
|
+
|
|
42
|
+
/** Mark the field touched (wire to `blur`); validates when configured. */
|
|
43
|
+
readonly onBlur: () => void;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Validate this field.
|
|
47
|
+
*
|
|
48
|
+
* @returns Whether the field is valid (`false` also when superseded).
|
|
49
|
+
*/
|
|
50
|
+
readonly validate: () => Promise<boolean>;
|
|
51
|
+
|
|
52
|
+
/** Restore the initial value and clear errors and touched state. */
|
|
53
|
+
readonly reset: () => void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** One entry of a field array, with a stable key for `v-for`. */
|
|
57
|
+
export interface FormArrayEntry<Item, Path extends string = string> {
|
|
58
|
+
/** Stable key that follows the item through moves. */
|
|
59
|
+
readonly key: number;
|
|
60
|
+
|
|
61
|
+
/** Current index. */
|
|
62
|
+
readonly index: number;
|
|
63
|
+
|
|
64
|
+
/** Dotted path of the item, e.g. `items.2`. */
|
|
65
|
+
readonly path: `${Path}.${number}`;
|
|
66
|
+
|
|
67
|
+
/** Current item value. */
|
|
68
|
+
readonly value: Item;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Typed operations on an array field, returned by `fieldArray`. */
|
|
72
|
+
export interface FormFieldArrayControls<Item, Path extends string = string> {
|
|
73
|
+
/** Entries with stable keys. */
|
|
74
|
+
readonly entries: ComputedRef<readonly FormArrayEntry<Item, Path>[]>;
|
|
75
|
+
|
|
76
|
+
/** Append items. */
|
|
77
|
+
readonly append: (...items: Item[]) => void;
|
|
78
|
+
|
|
79
|
+
/** Prepend items. */
|
|
80
|
+
readonly prepend: (...items: Item[]) => void;
|
|
81
|
+
|
|
82
|
+
/** Insert an item at `index`. */
|
|
83
|
+
readonly insert: (index: number, item: Item) => void;
|
|
84
|
+
|
|
85
|
+
/** Remove the item at `index`, shifting errors and touched state. */
|
|
86
|
+
readonly remove: (index: number) => void;
|
|
87
|
+
|
|
88
|
+
/** Move an item from one index to another, carrying its state along. */
|
|
89
|
+
readonly move: (from: number, to: number) => void;
|
|
90
|
+
|
|
91
|
+
/** Swap two items. */
|
|
92
|
+
readonly swap: (first: number, second: number) => void;
|
|
93
|
+
|
|
94
|
+
/** Replace all items and reset their state. */
|
|
95
|
+
readonly replace: (items: readonly Item[]) => void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Reactive state and actions returned by `useForm`. */
|
|
99
|
+
export interface FormControls<Values extends object, Output> {
|
|
100
|
+
/** Current values. Mutations are tracked (dirty state derives from them). */
|
|
101
|
+
readonly values: Ref<Values>;
|
|
102
|
+
|
|
103
|
+
/** Current errors keyed by path; `""` holds form-level errors. */
|
|
104
|
+
readonly errors: Readonly<ShallowRef<FormErrorRecord>>;
|
|
105
|
+
|
|
106
|
+
/** Whether no errors are currently recorded. */
|
|
107
|
+
readonly valid: ComputedRef<boolean>;
|
|
108
|
+
|
|
109
|
+
/** Whether any value differs from its initial value. */
|
|
110
|
+
readonly dirty: ComputedRef<boolean>;
|
|
111
|
+
|
|
112
|
+
/** Nested flags for values that differ from the initial values. */
|
|
113
|
+
readonly dirtyFields: ComputedRef<DirtyFields<Values>>;
|
|
114
|
+
|
|
115
|
+
/** Whether any validation is pending. */
|
|
116
|
+
readonly validating: ComputedRef<boolean>;
|
|
117
|
+
|
|
118
|
+
/** Whether a submission is in progress. */
|
|
119
|
+
readonly submitting: Readonly<Ref<boolean>>;
|
|
120
|
+
|
|
121
|
+
/** Number of submit attempts. Disabled submits are not counted. */
|
|
122
|
+
readonly submitCount: Readonly<Ref<number>>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether the latest finished submit was valid and its handler settled.
|
|
126
|
+
* A superseded submit leaves this flag unchanged.
|
|
127
|
+
*/
|
|
128
|
+
readonly isSubmitSuccessful: Readonly<Ref<boolean>>;
|
|
129
|
+
|
|
130
|
+
/** Whether value writes, field-array edits, and submission are ignored. */
|
|
131
|
+
readonly disabled: ComputedRef<boolean>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Typed controls for one field.
|
|
135
|
+
*
|
|
136
|
+
* @param path Dotted path.
|
|
137
|
+
* @returns Field controls whose value type follows the path.
|
|
138
|
+
*/
|
|
139
|
+
readonly field: <Path extends FormPath<Values>>(
|
|
140
|
+
path: Path,
|
|
141
|
+
) => FormFieldControls<FormPathValue<Values, Path>, Path>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Typed operations for an array field.
|
|
145
|
+
*
|
|
146
|
+
* @param path Dotted path of an array.
|
|
147
|
+
* @returns Field array controls.
|
|
148
|
+
*/
|
|
149
|
+
readonly fieldArray: <Path extends FormArrayPath<Values>>(
|
|
150
|
+
path: Path,
|
|
151
|
+
) => FormFieldArrayControls<FormArrayItem<Values, Path>, Path>;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Read the value at a path.
|
|
155
|
+
*
|
|
156
|
+
* @param path Dotted path.
|
|
157
|
+
* @returns The value.
|
|
158
|
+
*/
|
|
159
|
+
readonly getValue: <Path extends FormPath<Values>>(path: Path) => FormPathValue<Values, Path>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Read a path as a computed ref that tracks later edits.
|
|
163
|
+
*
|
|
164
|
+
* @param path Dotted path.
|
|
165
|
+
* @returns The value at that path.
|
|
166
|
+
*/
|
|
167
|
+
readonly watch: <Path extends FormPath<Values>>(
|
|
168
|
+
path: Path,
|
|
169
|
+
) => ComputedRef<FormPathValue<Values, Path>>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Write the value at a path. Does nothing while `disabled` is true.
|
|
173
|
+
*
|
|
174
|
+
* @param path Dotted path.
|
|
175
|
+
* @param value New value.
|
|
176
|
+
* @param options Touch and validation behavior.
|
|
177
|
+
*/
|
|
178
|
+
readonly setValue: <Path extends FormPath<Values>>(
|
|
179
|
+
path: Path,
|
|
180
|
+
value: FormPathValue<Values, Path>,
|
|
181
|
+
options?: SetFieldValueOptions,
|
|
182
|
+
) => void;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Replace (or shallowly merge) the values without touching initial values.
|
|
186
|
+
* Does nothing while `disabled` is true.
|
|
187
|
+
*
|
|
188
|
+
* @param values New values.
|
|
189
|
+
* @param options `merge: true` shallowly merges top-level keys.
|
|
190
|
+
*/
|
|
191
|
+
readonly setValues: (values: Partial<Values>, options?: { readonly merge?: boolean }) => void;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Whether a field was touched.
|
|
195
|
+
*
|
|
196
|
+
* @param path Dotted path.
|
|
197
|
+
* @returns Touched state.
|
|
198
|
+
*/
|
|
199
|
+
readonly isTouched: (path: FormPath<Values>) => boolean;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Replace errors (for example with server-side errors).
|
|
203
|
+
*
|
|
204
|
+
* @param errors Errors keyed by path.
|
|
205
|
+
*/
|
|
206
|
+
readonly setErrors: (errors: FormErrors<Values>) => void;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Set one path's errors without replacing the others.
|
|
210
|
+
*
|
|
211
|
+
* @param path Dotted path. `""` is the form-level error.
|
|
212
|
+
* @param message Messages for that path. An empty result clears it.
|
|
213
|
+
*/
|
|
214
|
+
readonly setError: (path: FormPath<Values> | "", message: ValidatorResult) => void;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Clear errors of one path, or all errors.
|
|
218
|
+
*
|
|
219
|
+
* @param path Dotted path; omit to clear everything.
|
|
220
|
+
*/
|
|
221
|
+
readonly clearErrors: (path?: FormPath<Values> | "") => void;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Run every validator, aborting any older validation still in flight.
|
|
225
|
+
*
|
|
226
|
+
* @returns The discriminated result; never rejects on validation failure.
|
|
227
|
+
*/
|
|
228
|
+
readonly validate: () => Promise<FormResult<Output>>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Validate and, when valid, call `handler` (or the `onSubmit` option).
|
|
232
|
+
* A newer submit aborts an older one's pending validation. Does nothing
|
|
233
|
+
* while `disabled` is true.
|
|
234
|
+
*
|
|
235
|
+
* @param handler Submit handler overriding `onSubmit`.
|
|
236
|
+
* @returns The validation result.
|
|
237
|
+
*/
|
|
238
|
+
readonly submit: (
|
|
239
|
+
handler?: (value: Output, context: { readonly signal: AbortSignal }) => unknown,
|
|
240
|
+
) => Promise<FormResult<Output>>;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Create a `submit` event listener that prevents the native submission.
|
|
244
|
+
*
|
|
245
|
+
* @param handler Submit handler overriding `onSubmit`.
|
|
246
|
+
* @returns Event listener for `<form @submit>`.
|
|
247
|
+
*/
|
|
248
|
+
readonly handleSubmit: (
|
|
249
|
+
handler?: (value: Output, context: { readonly signal: AbortSignal }) => unknown,
|
|
250
|
+
) => (event?: Event) => Promise<FormResult<Output>>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Restore initial values (or install new ones). Omitted keep-flags clear
|
|
254
|
+
* that piece of state. Still runs while `disabled` is true.
|
|
255
|
+
*
|
|
256
|
+
* @param values New initial values.
|
|
257
|
+
* @param options State to retain.
|
|
258
|
+
*/
|
|
259
|
+
readonly reset: (values?: Values, options?: FormResetOptions) => void;
|
|
260
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dirty-field comparison for `useForm`.
|
|
3
|
+
*
|
|
4
|
+
* Equality matches the form's own deep compare (dates by instant, arrays by
|
|
5
|
+
* item, plain objects by own keys). Prototype-bearing values are leaves.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { isContainer } from "./form-value-paths.ts";
|
|
9
|
+
|
|
10
|
+
/** Whether two form values are the same, ignoring object identity. */
|
|
11
|
+
export function valuesEqual(left: unknown, right: unknown): boolean {
|
|
12
|
+
if (Object.is(left, right)) return true;
|
|
13
|
+
if (left instanceof Date && right instanceof Date) return left.getTime() === right.getTime();
|
|
14
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
15
|
+
return (
|
|
16
|
+
left.length === right.length && left.every((item, index) => valuesEqual(item, right[index]))
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (isContainer(left) && isContainer(right) && !Array.isArray(left) && !Array.isArray(right)) {
|
|
20
|
+
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
21
|
+
for (const key of keys) if (!valuesEqual(left[key], right[key])) return false;
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function blankDirty(value: unknown): boolean {
|
|
28
|
+
if (value === undefined) return true;
|
|
29
|
+
if (Array.isArray(value)) return value.every((item) => blankDirty(item));
|
|
30
|
+
if (isContainer(value)) return Object.keys(value).length === 0;
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Nested dirty flags of `current` against `previous`.
|
|
36
|
+
*
|
|
37
|
+
* Equal objects become `{}`, equal arrays become `[]`, and equal leaves
|
|
38
|
+
* become `undefined` so callers can omit them.
|
|
39
|
+
*/
|
|
40
|
+
export function dirtyFieldsOf(current: unknown, previous: unknown): unknown {
|
|
41
|
+
if (valuesEqual(current, previous)) {
|
|
42
|
+
if (Array.isArray(current)) return [];
|
|
43
|
+
if (isContainer(current)) return {};
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(current) || Array.isArray(previous)) {
|
|
47
|
+
const left = Array.isArray(current) ? current : [];
|
|
48
|
+
const right = Array.isArray(previous) ? previous : [];
|
|
49
|
+
const length = Math.max(left.length, right.length);
|
|
50
|
+
return Array.from({ length }, (_, index) => {
|
|
51
|
+
if (index >= left.length || index >= right.length) return true;
|
|
52
|
+
const child = dirtyFieldsOf(left[index], right[index]);
|
|
53
|
+
return blankDirty(child) ? undefined : child;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (
|
|
57
|
+
isContainer(current) &&
|
|
58
|
+
isContainer(previous) &&
|
|
59
|
+
!Array.isArray(current) &&
|
|
60
|
+
!Array.isArray(previous)
|
|
61
|
+
) {
|
|
62
|
+
const result: Record<string, unknown> = {};
|
|
63
|
+
const keys = new Set([...Object.keys(current), ...Object.keys(previous)]);
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
const child = dirtyFieldsOf(
|
|
66
|
+
Object.hasOwn(current, key) ? current[key] : undefined,
|
|
67
|
+
Object.hasOwn(previous, key) ? previous[key] : undefined,
|
|
68
|
+
);
|
|
69
|
+
if (blankDirty(child)) continue;
|
|
70
|
+
result[key] = child;
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function own(record: unknown, key: string): unknown {
|
|
78
|
+
if (!isContainer(record) || Array.isArray(record)) return undefined;
|
|
79
|
+
return Object.hasOwn(record, key) ? record[key] : undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function mergeKept(base: unknown, current: unknown, previous: unknown): unknown {
|
|
83
|
+
if (valuesEqual(current, previous)) return base;
|
|
84
|
+
if (Array.isArray(current) && Array.isArray(previous)) {
|
|
85
|
+
return current.map((item, index) => {
|
|
86
|
+
if (index >= previous.length) return item;
|
|
87
|
+
const baseItem = Array.isArray(base) ? base[index] : undefined;
|
|
88
|
+
return mergeKept(baseItem, item, previous[index]);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
isContainer(current) &&
|
|
93
|
+
isContainer(previous) &&
|
|
94
|
+
!Array.isArray(current) &&
|
|
95
|
+
!Array.isArray(previous)
|
|
96
|
+
) {
|
|
97
|
+
const baseObject = isContainer(base) && !Array.isArray(base) ? base : {};
|
|
98
|
+
const keys = new Set([
|
|
99
|
+
...Object.keys(baseObject),
|
|
100
|
+
...Object.keys(current),
|
|
101
|
+
...Object.keys(previous),
|
|
102
|
+
]);
|
|
103
|
+
const result: Record<string, unknown> = {};
|
|
104
|
+
for (const key of keys) {
|
|
105
|
+
const inCurrent = Object.hasOwn(current, key);
|
|
106
|
+
const inPrevious = Object.hasOwn(previous, key);
|
|
107
|
+
if (!inCurrent && inPrevious) continue;
|
|
108
|
+
if (inCurrent && !inPrevious) {
|
|
109
|
+
result[key] = current[key];
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (!inCurrent) {
|
|
113
|
+
if (Object.hasOwn(baseObject, key)) result[key] = baseObject[key];
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
result[key] = mergeKept(own(baseObject, key), current[key], previous[key]);
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
return current;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Reset values that keep user edits.
|
|
125
|
+
*
|
|
126
|
+
* Fields equal to `previous` take `base`. Dirty leaves, added keys, and
|
|
127
|
+
* array-length edits keep `current`. A removed key stays removed.
|
|
128
|
+
*/
|
|
129
|
+
export function valuesKeepingDirty<Value>(base: Value, current: Value, previous: Value): Value {
|
|
130
|
+
return mergeKept(base, current, previous) as Value;
|
|
131
|
+
}
|