@opentf/afw-form 1.34.0 → 1.36.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/index.d.ts +85 -0
- package/package.json +11 -6
package/index.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Types for @opentf/afw-form: path-based reactive forms over AFW's signals.
|
|
2
|
+
|
|
3
|
+
export { signal, computed, batch, effect, untracked, reactive, getCurrentInstance } from "@opentf/afw";
|
|
4
|
+
|
|
5
|
+
/** When the form validates. */
|
|
6
|
+
export type ValidationMode = "onChange" | "onBlur" | "onSubmit";
|
|
7
|
+
/** When the form validates again once a field has an error. */
|
|
8
|
+
export type ReValidationMode = "onChange" | "onBlur";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Errors keyed by field path, as nested objects: `{ email: "Required", user: { name: "Too short" } }`.
|
|
12
|
+
* A falsy leaf means no error.
|
|
13
|
+
*/
|
|
14
|
+
export interface FormErrors {
|
|
15
|
+
[path: string]: string | false | null | undefined | FormErrors;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FormOptions<T extends object> {
|
|
19
|
+
/** The starting values; they also set the shape `register` paths address. */
|
|
20
|
+
initialValues?: T;
|
|
21
|
+
/** Returns the errors object directly. May be async. */
|
|
22
|
+
validate?: (values: T) => FormErrors | undefined | null | Promise<FormErrors | undefined | null>;
|
|
23
|
+
/** Returns `{ errors }`, the shape schema resolvers produce. May be async. */
|
|
24
|
+
validator?: (values: T) => { errors?: FormErrors } | Promise<{ errors?: FormErrors }>;
|
|
25
|
+
/** When to validate: a mode, or a signal holding one. Default `"onBlur"`. */
|
|
26
|
+
mode?: ValidationMode | { value: ValidationMode | undefined };
|
|
27
|
+
/** When to validate again after the first error. Default `"onChange"`. */
|
|
28
|
+
reValidateMode?: ReValidationMode | { value: ReValidationMode | undefined };
|
|
29
|
+
/** Identifies the form within its component, in place of its call position. */
|
|
30
|
+
key?: string | number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What `register(path)` returns: spread it onto an input. */
|
|
34
|
+
export interface FieldBinding {
|
|
35
|
+
/** The field path. */
|
|
36
|
+
name: string;
|
|
37
|
+
/** The current value (`""` before the field has one). */
|
|
38
|
+
value: any;
|
|
39
|
+
/** The current value, read as a boolean by checkboxes. */
|
|
40
|
+
checked: any;
|
|
41
|
+
/** The field's current error, if any. */
|
|
42
|
+
error: { readonly value: string | undefined };
|
|
43
|
+
/** Whether the field has been blurred. */
|
|
44
|
+
isTouched: { readonly value: boolean };
|
|
45
|
+
/** Writes the input's value (or `checked`) back to the form. */
|
|
46
|
+
oninput: (event: Event) => void;
|
|
47
|
+
/** Marks the field touched and runs `onBlur` validation. */
|
|
48
|
+
onblur: () => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Form<T extends object> {
|
|
52
|
+
/** Bind a field by dotted path: `register("user.name")`, `register("items.0.qty")`. */
|
|
53
|
+
register(path: string): FieldBinding;
|
|
54
|
+
/** A submit handler: validates, then calls `fn(values)` only when the form is valid. */
|
|
55
|
+
handleSubmit(fn: (values: T) => unknown): (event?: Event) => Promise<void>;
|
|
56
|
+
/** Reset to the initial values, or to `values`, which become the new baseline. */
|
|
57
|
+
reset(values?: T): void;
|
|
58
|
+
/** Set a field's error from outside the validator; an empty message clears it. */
|
|
59
|
+
setError(path: string, message?: string | null): void;
|
|
60
|
+
/** The current values, reactive per path. */
|
|
61
|
+
readonly values: T;
|
|
62
|
+
/** The current errors, keyed by path, reactive per path. */
|
|
63
|
+
readonly errors: FormErrors;
|
|
64
|
+
/** Which fields have been blurred, mirroring the values' shape. */
|
|
65
|
+
readonly touched: Record<string, any>;
|
|
66
|
+
/** Which top-level fields differ from the baseline. */
|
|
67
|
+
readonly changed: Record<string, boolean>;
|
|
68
|
+
/** No errors are set. */
|
|
69
|
+
readonly isValid: boolean;
|
|
70
|
+
/** The values differ from the baseline. */
|
|
71
|
+
readonly isChanged: boolean;
|
|
72
|
+
/** At least one field has been blurred. */
|
|
73
|
+
readonly isTouched: boolean;
|
|
74
|
+
/** A submit handler is running. */
|
|
75
|
+
readonly isSubmitting: boolean;
|
|
76
|
+
/** An async validator is running. */
|
|
77
|
+
readonly isValidating: boolean;
|
|
78
|
+
/** A submit has completed. */
|
|
79
|
+
readonly isSubmitted: boolean;
|
|
80
|
+
/** The number of submit attempts. */
|
|
81
|
+
readonly submitCount: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Create a form, stable across re-runs of the component body that calls it. */
|
|
85
|
+
export function createForm<T extends object = Record<string, any>>(options?: FormOptions<T>): Form<T>;
|
package/package.json
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentf/afw-form",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.36.0",
|
|
4
4
|
"description": "Path-based reactive forms for AFW.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
7
8
|
"type": "module",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"build": "cp ../../LICENSE LICENSE"
|
|
10
11
|
},
|
|
11
12
|
"exports": {
|
|
12
|
-
".":
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"default": "./index.js"
|
|
16
|
+
}
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
15
|
-
"index.js"
|
|
19
|
+
"index.js",
|
|
20
|
+
"index.d.ts"
|
|
16
21
|
],
|
|
17
22
|
"keywords": [
|
|
18
23
|
"afw",
|
|
@@ -34,13 +39,13 @@
|
|
|
34
39
|
"@opentf/std": "^0.20.0"
|
|
35
40
|
},
|
|
36
41
|
"peerDependencies": {
|
|
37
|
-
"@opentf/afw": "0.
|
|
42
|
+
"@opentf/afw": "0.41.0"
|
|
38
43
|
},
|
|
39
44
|
"publishConfig": {
|
|
40
45
|
"access": "public"
|
|
41
46
|
},
|
|
42
47
|
"devDependencies": {
|
|
43
|
-
"@opentf/afw": "0.
|
|
44
|
-
"@opentf/afw-test": "1.
|
|
48
|
+
"@opentf/afw": "0.41.0",
|
|
49
|
+
"@opentf/afw-test": "1.37.0"
|
|
45
50
|
}
|
|
46
51
|
}
|