@canonical/svelte-ds-global-form 0.47.0 → 0.49.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/LICENSE +166 -0
- package/README.md +213 -8
- package/dist/component/CheckboxField/CheckboxField.svelte +21 -3
- package/dist/component/CheckboxField/CheckboxField.svelte.d.ts +2 -2
- package/dist/component/CheckboxField/types.d.ts +13 -1
- package/dist/component/ChoicesField/Choices.svelte +5 -3
- package/dist/component/RichChoicesField/RichChoices.svelte +5 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/pattern/Field/Field.svelte +20 -4
- package/dist/pattern/Field/types.d.ts +1 -1
- package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte +18 -2
- package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte.d.ts +4 -2
- package/dist/subcomponent/CheckboxInput/types.d.ts +7 -0
- package/dist/subcomponent/RadioInput/RadioInput.svelte +28 -4
- package/dist/subcomponent/RadioInput/RadioInput.svelte.d.ts +4 -2
- package/dist/subcomponent/RadioInput/types.d.ts +7 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/owner/bindField.d.ts +5 -3
- package/dist/utils/owner/bindField.js +3 -1
- package/dist/utils/owner/bindFieldArray.d.ts +20 -0
- package/dist/utils/owner/bindFieldArray.js +62 -0
- package/dist/utils/owner/bindTanStackForm.d.ts +18 -0
- package/dist/utils/owner/bindTanStackForm.js +65 -0
- package/dist/utils/owner/createBinding.js +6 -0
- package/dist/utils/owner/createFormState.svelte.d.ts +12 -3
- package/dist/utils/owner/createFormState.svelte.js +43 -21
- package/dist/utils/owner/createSchemaValidator.d.ts +21 -0
- package/dist/utils/owner/createSchemaValidator.js +36 -0
- package/dist/utils/owner/createValidatedBinding.d.ts +2 -2
- package/dist/utils/owner/createValidatedBinding.js +8 -4
- package/dist/utils/owner/getPath.d.ts +6 -0
- package/dist/utils/owner/getPath.js +15 -0
- package/dist/utils/owner/index.d.ts +8 -0
- package/dist/utils/owner/index.js +8 -0
- package/dist/utils/owner/normalizePath.d.ts +6 -0
- package/dist/utils/owner/normalizePath.js +9 -0
- package/dist/utils/owner/readOwnerValue.d.ts +7 -0
- package/dist/utils/owner/readOwnerValue.js +11 -0
- package/dist/utils/owner/setPath.d.ts +12 -0
- package/dist/utils/owner/setPath.js +36 -0
- package/dist/utils/owner/splitPath.d.ts +7 -0
- package/dist/utils/owner/splitPath.js +9 -0
- package/dist/utils/owner/types.d.ts +141 -12
- package/dist/utils/remoteForm/bindRemoteField.d.ts +23 -0
- package/dist/utils/remoteForm/bindRemoteField.js +68 -0
- package/dist/utils/remoteForm/bindRemoteForm.d.ts +20 -0
- package/dist/utils/remoteForm/bindRemoteForm.js +31 -0
- package/dist/utils/remoteForm/constants.d.ts +42 -0
- package/dist/utils/remoteForm/constants.js +42 -0
- package/dist/utils/remoteForm/index.d.ts +3 -0
- package/dist/utils/remoteForm/index.js +2 -0
- package/dist/utils/remoteForm/types.d.ts +90 -0
- package/dist/utils/remoteForm/types.js +1 -0
- package/package.json +24 -11
- /package/dist/{component/ChoicesField → utils}/isChoiceChecked.d.ts +0 -0
- /package/dist/{component/ChoicesField → utils}/isChoiceChecked.js +0 -0
- /package/dist/{component/ChoicesField → utils}/toggleChoice.d.ts +0 -0
- /package/dist/{component/ChoicesField → utils}/toggleChoice.js +0 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import splitPath from "./splitPath.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads the value at a field's path in nested values, as `address.city` or
|
|
4
|
+
* `contacts[0].email`. Returns `undefined` when the path stops early.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function getPath(values, name) {
|
|
8
|
+
let current = values;
|
|
9
|
+
for (const segment of splitPath(name)) {
|
|
10
|
+
if (typeof current !== "object" || current === null)
|
|
11
|
+
return undefined;
|
|
12
|
+
current = current[segment];
|
|
13
|
+
}
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
export { default as bindField } from "./bindField.js";
|
|
2
|
+
export { default as bindFieldArray } from "./bindFieldArray.js";
|
|
3
|
+
export { default as bindTanStackForm } from "./bindTanStackForm.js";
|
|
2
4
|
export { default as createBinding } from "./createBinding.js";
|
|
3
5
|
export { default as createFormState } from "./createFormState.svelte.js";
|
|
6
|
+
export { default as createSchemaValidator } from "./createSchemaValidator.js";
|
|
4
7
|
export { default as createValidatedBinding } from "./createValidatedBinding.js";
|
|
5
8
|
export { getFormOwnerContext, setFormOwnerContext, } from "./formOwnerContext.js";
|
|
9
|
+
export { default as getPath } from "./getPath.js";
|
|
10
|
+
export { default as normalizePath } from "./normalizePath.js";
|
|
11
|
+
export { default as readOwnerValue } from "./readOwnerValue.js";
|
|
6
12
|
export { default as runLatest } from "./runLatest.js";
|
|
13
|
+
export { default as setPath } from "./setPath.js";
|
|
14
|
+
export { default as splitPath } from "./splitPath.js";
|
|
7
15
|
export type * from "./types.js";
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
export { default as bindField } from "./bindField.js";
|
|
2
|
+
export { default as bindFieldArray } from "./bindFieldArray.js";
|
|
3
|
+
export { default as bindTanStackForm } from "./bindTanStackForm.js";
|
|
2
4
|
export { default as createBinding } from "./createBinding.js";
|
|
3
5
|
export { default as createFormState } from "./createFormState.svelte.js";
|
|
6
|
+
export { default as createSchemaValidator } from "./createSchemaValidator.js";
|
|
4
7
|
export { default as createValidatedBinding } from "./createValidatedBinding.js";
|
|
5
8
|
export { getFormOwnerContext, setFormOwnerContext, } from "./formOwnerContext.js";
|
|
9
|
+
export { default as getPath } from "./getPath.js";
|
|
10
|
+
export { default as normalizePath } from "./normalizePath.js";
|
|
11
|
+
export { default as readOwnerValue } from "./readOwnerValue.js";
|
|
6
12
|
export { default as runLatest } from "./runLatest.js";
|
|
13
|
+
export { default as setPath } from "./setPath.js";
|
|
14
|
+
export { default as splitPath } from "./splitPath.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import splitPath from "./splitPath.js";
|
|
2
|
+
/**
|
|
3
|
+
* Writes a field name in one form, its segments joined with `.`, so both path
|
|
4
|
+
* forms name one field: `contacts[0].email` gives `contacts.0.email`.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function normalizePath(name) {
|
|
8
|
+
return splitPath(name).join(".");
|
|
9
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FormOwner, FormValues, Path, PathValue } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads a field's value from an owner: through its `getValue` when it has
|
|
4
|
+
* one, or by reading the path in its `values`.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function readOwnerValue<Values extends FormValues, Name extends Path<Values>>(owner: FormOwner<Values>, name: Name): PathValue<Values, Name> | undefined;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import getPath from "./getPath.js";
|
|
2
|
+
/**
|
|
3
|
+
* Reads a field's value from an owner: through its `getValue` when it has
|
|
4
|
+
* one, or by reading the path in its `values`.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function readOwnerValue(owner, name) {
|
|
8
|
+
return owner.getValue
|
|
9
|
+
? owner.getValue(name)
|
|
10
|
+
: getPath(owner.values, name);
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes the value at a field's path in nested values, as `address.city` or
|
|
3
|
+
* `contacts[0].email`, creating the objects along the path, or an array where
|
|
4
|
+
* the next segment is an index. Writing `undefined` drops the key, or leaves
|
|
5
|
+
* an array's slot `undefined` so the later items keep their indices; it
|
|
6
|
+
* creates nothing on the way.
|
|
7
|
+
*
|
|
8
|
+
* Each container is read back after it is created, so a `$state` proxy is
|
|
9
|
+
* written through and stays reactive.
|
|
10
|
+
* @note Impure: mutates `values` in place.
|
|
11
|
+
*/
|
|
12
|
+
export default function setPath(values: Record<string, unknown>, name: string, value: unknown): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import splitPath from "./splitPath.js";
|
|
2
|
+
/** Tells whether a path segment is an array index. */
|
|
3
|
+
const isIndex = (segment) => /^\d+$/.test(segment);
|
|
4
|
+
/**
|
|
5
|
+
* Writes the value at a field's path in nested values, as `address.city` or
|
|
6
|
+
* `contacts[0].email`, creating the objects along the path, or an array where
|
|
7
|
+
* the next segment is an index. Writing `undefined` drops the key, or leaves
|
|
8
|
+
* an array's slot `undefined` so the later items keep their indices; it
|
|
9
|
+
* creates nothing on the way.
|
|
10
|
+
*
|
|
11
|
+
* Each container is read back after it is created, so a `$state` proxy is
|
|
12
|
+
* written through and stays reactive.
|
|
13
|
+
* @note Impure: mutates `values` in place.
|
|
14
|
+
*/
|
|
15
|
+
export default function setPath(values, name, value) {
|
|
16
|
+
const segments = splitPath(name);
|
|
17
|
+
const last = segments.pop();
|
|
18
|
+
if (last === undefined)
|
|
19
|
+
return;
|
|
20
|
+
let parent = values;
|
|
21
|
+
for (const [index, segment] of segments.entries()) {
|
|
22
|
+
if (typeof parent[segment] !== "object" || parent[segment] === null) {
|
|
23
|
+
if (value === undefined)
|
|
24
|
+
return;
|
|
25
|
+
const next = segments.at(index + 1) ?? last;
|
|
26
|
+
parent[segment] = isIndex(next) ? [] : {};
|
|
27
|
+
}
|
|
28
|
+
parent = parent[segment];
|
|
29
|
+
}
|
|
30
|
+
if (value !== undefined)
|
|
31
|
+
parent[last] = value;
|
|
32
|
+
else if (Array.isArray(parent))
|
|
33
|
+
parent[last] = undefined;
|
|
34
|
+
else
|
|
35
|
+
delete parent[last];
|
|
36
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a field name into its path segments. Both react-hook-form's dotted
|
|
3
|
+
* form and SvelteKit's bracket form are read: `contacts.0.email` and
|
|
4
|
+
* `contacts[0].email` both give `["contacts", "0", "email"]`.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function splitPath(name: string): string[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits a field name into its path segments. Both react-hook-form's dotted
|
|
3
|
+
* form and SvelteKit's bracket form are read: `contacts.0.email` and
|
|
4
|
+
* `contacts[0].email` both give `["contacts", "0", "email"]`.
|
|
5
|
+
* @note Pure.
|
|
6
|
+
*/
|
|
7
|
+
export default function splitPath(name) {
|
|
8
|
+
return name.split(/[.[\]]+/).filter((segment) => segment !== "");
|
|
9
|
+
}
|
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
/** A form's values, by field name. */
|
|
2
2
|
export type FormValues = Record<string, unknown>;
|
|
3
|
+
/** The values a path stops at rather than reading into. */
|
|
4
|
+
type PathLeaf = string | number | boolean | bigint | symbol | null | undefined | Date | Blob | FileList;
|
|
5
|
+
/** Tells whether two types are the same type. */
|
|
6
|
+
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false;
|
|
7
|
+
/** Tells whether any member of the union `Seen` is `Value`. */
|
|
8
|
+
type IsSeen<Seen, Value> = Seen extends unknown ? IsEqual<Seen, Value> extends true ? true : never : never;
|
|
9
|
+
/** The paths to `Key` and below it, whose value is `Value`. */
|
|
10
|
+
type PathFrom<Key extends string, Value, Seen> = unknown extends Value ? Key | `${Key}.${string}` : Value extends PathLeaf ? Key : true extends IsSeen<Seen, Value> ? Key : Key | `${Key}.${PathIn<Value, Seen | Value>}`;
|
|
11
|
+
/** The paths inside `Values`, stopping at a type already walked through. */
|
|
12
|
+
type PathIn<Values, Seen> = Values extends readonly (infer Item)[] ? PathFrom<`${number}`, Item, Seen> : {
|
|
13
|
+
[Key in keyof Values & string]-?: PathFrom<Key, Values[Key], Seen>;
|
|
14
|
+
}[keyof Values & string];
|
|
15
|
+
/**
|
|
16
|
+
* A field name that reaches into nested values, as react-hook-form's `Path`:
|
|
17
|
+
* object keys and array indices joined with `.`, as `address.city` or
|
|
18
|
+
* `contacts.0.email`. The fields also accept SvelteKit's `contacts[0].email`
|
|
19
|
+
* at runtime; the types follow the dotted form.
|
|
20
|
+
*/
|
|
21
|
+
export type Path<Values> = Values extends unknown ? PathIn<Values, Values> : never;
|
|
22
|
+
/** The value of one segment of a path in `Values`. */
|
|
23
|
+
type SegmentValue<Values, Segment extends string> = Values extends readonly (infer Item)[] ? Segment extends `${number}` ? Item | undefined : undefined : Segment extends keyof Values ? Values[Segment] : undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The value at a path in `Values`, as react-hook-form's `PathValue`:
|
|
26
|
+
* `PathValue<{ address: { city: string } }, "address.city">` is `string`.
|
|
27
|
+
*/
|
|
28
|
+
export type PathValue<Values, P extends string> = unknown extends Values ? unknown : string extends keyof Values ? unknown extends Values[keyof Values] ? unknown : PathValueIn<Values, P> : PathValueIn<Values, P>;
|
|
29
|
+
/** The value at a path in `Values`, segment by segment. */
|
|
30
|
+
type PathValueIn<Values, P extends string> = P extends `${infer Segment}.${infer Rest}` ? PathValue<SegmentValue<Values, Segment>, Rest> : SegmentValue<Values, P>;
|
|
3
31
|
/**
|
|
4
32
|
* The state a `Form` reads and writes by field name: values, and optionally
|
|
5
33
|
* errors, blur and submission. Every read must be reactive under runes, as
|
|
@@ -7,17 +35,22 @@ export type FormValues = Record<string, unknown>;
|
|
|
7
35
|
* `createBinding`.
|
|
8
36
|
*/
|
|
9
37
|
export type FormOwner<Values extends FormValues = FormValues> = {
|
|
10
|
-
/** The current values, by field name. */
|
|
38
|
+
/** The current values, by field name, nested as the names say. */
|
|
11
39
|
readonly values: Values;
|
|
12
|
-
/**
|
|
13
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Returns the value at a field's path. Leave it unset and the fields read
|
|
42
|
+
* the path in `values` themselves.
|
|
43
|
+
*/
|
|
44
|
+
getValue?<Name extends Path<Values>>(name: Name): PathValue<Values, Name> | undefined;
|
|
45
|
+
/** Writes the value at a field's path. Writing `undefined` drops it. */
|
|
46
|
+
setValue<Name extends Path<Values>>(name: Name, value: PathValue<Values, Name> | undefined): void;
|
|
14
47
|
/**
|
|
15
48
|
* Returns the field's validation message, if any. A field shows every
|
|
16
49
|
* message it is given: when errors exist is the owner's business.
|
|
17
50
|
*/
|
|
18
|
-
getError?(name:
|
|
51
|
+
getError?(name: Path<Values>): string | undefined;
|
|
19
52
|
/** Called when the user leaves the field. */
|
|
20
|
-
blur?(name:
|
|
53
|
+
blur?(name: Path<Values>): void;
|
|
21
54
|
/**
|
|
22
55
|
* Called when the form is submitted, after native validation passed. Call
|
|
23
56
|
* `event.preventDefault()` to stop the browser posting the form.
|
|
@@ -40,8 +73,8 @@ export type BindingSource<Values extends FormValues = FormValues> = Omit<FormOwn
|
|
|
40
73
|
/** Calls `notify` after every change, and returns a function that stops it. */
|
|
41
74
|
subscribe(notify: () => void): () => void;
|
|
42
75
|
};
|
|
43
|
-
/** A form's validation messages, by field
|
|
44
|
-
export type FormErrors<Values extends FormValues = FormValues> = Partial<Record<
|
|
76
|
+
/** A form's validation messages, by field path joined with `.`. */
|
|
77
|
+
export type FormErrors<Values extends FormValues = FormValues> = Partial<Record<Path<Values>, string>>;
|
|
45
78
|
/**
|
|
46
79
|
* When `createFormState` validates, as react-hook-form's `mode`. Every mode
|
|
47
80
|
* also validates on submit, and after the first submit on every change.
|
|
@@ -55,6 +88,11 @@ export type ValidationMode = "onSubmit" | "onChange" | "onBlur" | "onTouched";
|
|
|
55
88
|
export type FormStateOptions<Values extends FormValues = FormValues> = {
|
|
56
89
|
/** The initial values, restored by `reset`. */
|
|
57
90
|
defaultValues?: Values;
|
|
91
|
+
/**
|
|
92
|
+
* Messages to show from the start, such as a SvelteKit action's
|
|
93
|
+
* `form.errors`. Validation replaces them, and `reset` clears them.
|
|
94
|
+
*/
|
|
95
|
+
errors?: FormErrors<Values>;
|
|
58
96
|
/** Returns the messages of the invalid fields. Leave a valid field out. */
|
|
59
97
|
validate?: (values: Readonly<Values>) => FormErrors<Values>;
|
|
60
98
|
/** When to validate. Default: "onSubmit". */
|
|
@@ -68,16 +106,17 @@ export type FormStateOptions<Values extends FormValues = FormValues> = {
|
|
|
68
106
|
};
|
|
69
107
|
/** The owner `createFormState` returns. */
|
|
70
108
|
export type FormState<Values extends FormValues = FormValues> = ResettableOwner<Values> & {
|
|
71
|
-
/** The current messages, by field
|
|
109
|
+
/** The current messages, by field path joined with `.`. */
|
|
72
110
|
readonly errors: Readonly<FormErrors<Values>>;
|
|
73
|
-
/** The fields the user has left, by field
|
|
74
|
-
readonly touched: Readonly<Partial<Record<
|
|
111
|
+
/** The fields the user has left, by field path joined with `.`. */
|
|
112
|
+
readonly touched: Readonly<Partial<Record<Path<Values>, true>>>;
|
|
75
113
|
/** Has the form been submitted since it was created or reset. */
|
|
76
114
|
readonly isSubmitted: boolean;
|
|
77
115
|
/** Is an asynchronous `onSubmit` running. */
|
|
78
116
|
readonly isSubmitting: boolean;
|
|
79
|
-
|
|
80
|
-
|
|
117
|
+
getValue<Name extends Path<Values>>(name: Name): PathValue<Values, Name> | undefined;
|
|
118
|
+
getError(name: Path<Values>): string | undefined;
|
|
119
|
+
blur(name: Path<Values>): void;
|
|
81
120
|
submit(event: SubmitEvent): void;
|
|
82
121
|
};
|
|
83
122
|
/** A field's asynchronous check: resolves to its message, or `undefined` when valid. */
|
|
@@ -105,3 +144,93 @@ export type LatestOutcome<Output> = {
|
|
|
105
144
|
} | {
|
|
106
145
|
status: "superseded";
|
|
107
146
|
};
|
|
147
|
+
/**
|
|
148
|
+
* A schema that follows Standard Schema v1, the interface Zod (3.24 and
|
|
149
|
+
* later), Valibot and ArkType schemas implement under their `~standard`
|
|
150
|
+
* property. The interface is copied here, as the specification intends, so the
|
|
151
|
+
* package needs no dependency to accept such a schema.
|
|
152
|
+
*/
|
|
153
|
+
export type StandardSchema<Input = unknown, Output = Input> = {
|
|
154
|
+
readonly "~standard": {
|
|
155
|
+
/** The version of the specification. */
|
|
156
|
+
readonly version: 1;
|
|
157
|
+
/** The library that made the schema. */
|
|
158
|
+
readonly vendor: string;
|
|
159
|
+
/** Validates an unknown value, synchronously or not. */
|
|
160
|
+
readonly validate: (value: unknown) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
161
|
+
/** The input and output types, for inference only. */
|
|
162
|
+
readonly types?: {
|
|
163
|
+
readonly input: Input;
|
|
164
|
+
readonly output: Output;
|
|
165
|
+
} | undefined;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/** The result of a Standard Schema validation: a value, or issues. */
|
|
169
|
+
export type StandardSchemaResult<Output> = {
|
|
170
|
+
readonly value: Output;
|
|
171
|
+
readonly issues?: undefined;
|
|
172
|
+
} | {
|
|
173
|
+
readonly issues: readonly StandardSchemaIssue[];
|
|
174
|
+
};
|
|
175
|
+
/** One problem a Standard Schema found. */
|
|
176
|
+
export type StandardSchemaIssue = {
|
|
177
|
+
/** The message to show. */
|
|
178
|
+
readonly message: string;
|
|
179
|
+
/** Where the problem is in the value: keys, or segments holding a key. */
|
|
180
|
+
readonly path?: readonly (PropertyKey | {
|
|
181
|
+
readonly key: PropertyKey;
|
|
182
|
+
})[] | undefined;
|
|
183
|
+
};
|
|
184
|
+
/** The type of one item of the array at `Name` in `Values`. */
|
|
185
|
+
export type FieldArrayItem<Values extends FormValues, Name extends Path<Values>> = NonNullable<PathValue<Values, Name>> extends readonly (infer Item)[] ? Item : unknown;
|
|
186
|
+
/** A list of repeated fields bound to an owner's array, by `bindFieldArray`. */
|
|
187
|
+
export type FieldArray<Item> = {
|
|
188
|
+
/**
|
|
189
|
+
* The current items, each with a key that stays with it across `move` and
|
|
190
|
+
* `remove`: key an `{#each}` by it.
|
|
191
|
+
*/
|
|
192
|
+
readonly items: readonly {
|
|
193
|
+
readonly key: string;
|
|
194
|
+
readonly value: Item;
|
|
195
|
+
}[];
|
|
196
|
+
/** Adds an item at the end. */
|
|
197
|
+
append(value: Item): void;
|
|
198
|
+
/** Removes the item at `index`. */
|
|
199
|
+
remove(index: number): void;
|
|
200
|
+
/** Moves the item at `from` to `to`, shifting the items between. */
|
|
201
|
+
move(from: number, to: number): void;
|
|
202
|
+
/** Replaces every item, with new keys. */
|
|
203
|
+
replace(values: readonly Item[]): void;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* The part of a TanStack Form instance (`createForm` or `FormApi`, version 1)
|
|
207
|
+
* that `bindTanStackForm` uses. It is written out here, so the package needs no
|
|
208
|
+
* dependency on TanStack Form to accept one; the names are TanStack's own.
|
|
209
|
+
*/
|
|
210
|
+
export type TanStackFormApi<Values extends FormValues = FormValues> = {
|
|
211
|
+
/** The form's state, whose `values` hold every value. */
|
|
212
|
+
readonly state: {
|
|
213
|
+
readonly values: Values;
|
|
214
|
+
};
|
|
215
|
+
/** The store holding the state, which tells its listeners of each change. */
|
|
216
|
+
readonly store: {
|
|
217
|
+
subscribe(listener: () => void): {
|
|
218
|
+
unsubscribe(): void;
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
/** Reads the value at a field's path. */
|
|
222
|
+
getFieldValue(field: never): unknown;
|
|
223
|
+
/** Writes the value at a field's path. */
|
|
224
|
+
setFieldValue(field: never, updater: never): void;
|
|
225
|
+
/** Reads a field's meta, which holds its errors. */
|
|
226
|
+
getFieldMeta(field: never): unknown;
|
|
227
|
+
/** Updates a field's meta. */
|
|
228
|
+
setFieldMeta(field: never, updater: never): void;
|
|
229
|
+
/** Runs a field's validators for one cause. */
|
|
230
|
+
validateField(field: never, cause: "blur"): unknown;
|
|
231
|
+
/** Validates the form and calls its `onSubmit` when it is valid. */
|
|
232
|
+
handleSubmit(): Promise<void>;
|
|
233
|
+
/** Returns every value to its default. */
|
|
234
|
+
reset(): void;
|
|
235
|
+
};
|
|
236
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FittingInputType, RemoteField, RemoteFieldBinding, RemoteFieldOptions, RemoteValueOf } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns a SvelteKit remote form field's state, to spread onto a `Field`:
|
|
4
|
+
*
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <Field {...bindRemoteField(form.fields.age, "number")} label="Age" />
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* `name` is the one SvelteKit's `.as(type)` gives, so it carries the `n:` or
|
|
10
|
+
* `b:` prefix that makes the server decode a number or a boolean, and `[]` for
|
|
11
|
+
* an array. `value` reads and writes the remote form's value, so any control
|
|
12
|
+
* binds, including those that fire no `input` event; a single file is bound
|
|
13
|
+
* as a list of one. `error` is the first issue's message.
|
|
14
|
+
*
|
|
15
|
+
* The `inputType` must post the field's value type: a number field takes
|
|
16
|
+
* `number`, `range` or `rating`, not `text`. A kind that can hold several
|
|
17
|
+
* values takes `{ multiple: true }` when the value is an array, and the
|
|
18
|
+
* binding then sets the field's `multiple` or `isMultiple`. A `custom` field
|
|
19
|
+
* names its SvelteKit type: `{ type: "number" }`. A `hidden` field takes its
|
|
20
|
+
* prefix from its current value, so give it one.
|
|
21
|
+
* @note Pure: the properties are getters and a setter over the field.
|
|
22
|
+
*/
|
|
23
|
+
export default function bindRemoteField<Field extends RemoteField, Kind extends FittingInputType<RemoteValueOf<Field>>>(field: Field, inputType: Kind, ...[options]: RemoteFieldOptions<Kind, RemoteValueOf<Field>>): RemoteFieldBinding<Kind, RemoteValueOf<Field>>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { REMOTE_INPUT_TYPES, REMOTE_MULTIPLE_INPUT_TYPES, } from "./constants.js";
|
|
2
|
+
/** The `inputType`s whose multiple prop is `isMultiple` rather than `multiple`. */
|
|
3
|
+
const IS_MULTIPLE_KINDS = new Set([
|
|
4
|
+
"choices",
|
|
5
|
+
"rich-choices",
|
|
6
|
+
"combobox",
|
|
7
|
+
]);
|
|
8
|
+
/**
|
|
9
|
+
* Returns a SvelteKit remote form field's state, to spread onto a `Field`:
|
|
10
|
+
*
|
|
11
|
+
* ```svelte
|
|
12
|
+
* <Field {...bindRemoteField(form.fields.age, "number")} label="Age" />
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* `name` is the one SvelteKit's `.as(type)` gives, so it carries the `n:` or
|
|
16
|
+
* `b:` prefix that makes the server decode a number or a boolean, and `[]` for
|
|
17
|
+
* an array. `value` reads and writes the remote form's value, so any control
|
|
18
|
+
* binds, including those that fire no `input` event; a single file is bound
|
|
19
|
+
* as a list of one. `error` is the first issue's message.
|
|
20
|
+
*
|
|
21
|
+
* The `inputType` must post the field's value type: a number field takes
|
|
22
|
+
* `number`, `range` or `rating`, not `text`. A kind that can hold several
|
|
23
|
+
* values takes `{ multiple: true }` when the value is an array, and the
|
|
24
|
+
* binding then sets the field's `multiple` or `isMultiple`. A `custom` field
|
|
25
|
+
* names its SvelteKit type: `{ type: "number" }`. A `hidden` field takes its
|
|
26
|
+
* prefix from its current value, so give it one.
|
|
27
|
+
* @note Pure: the properties are getters and a setter over the field.
|
|
28
|
+
*/
|
|
29
|
+
export default function bindRemoteField(field, inputType, ...[options]) {
|
|
30
|
+
const settings = options;
|
|
31
|
+
const isMultiple = settings?.multiple === true;
|
|
32
|
+
const kitType = (settings?.type ??
|
|
33
|
+
(isMultiple
|
|
34
|
+
? REMOTE_MULTIPLE_INPUT_TYPES[inputType]
|
|
35
|
+
: REMOTE_INPUT_TYPES[inputType]));
|
|
36
|
+
// SvelteKit needs a value to name some inputs: a checkbox of an array,
|
|
37
|
+
// whose value marks it as one; a radio, which it requires; and a hidden
|
|
38
|
+
// input, whose value's type sets the prefix. The value does not change the
|
|
39
|
+
// name otherwise.
|
|
40
|
+
const readName = () => {
|
|
41
|
+
const probe = kitType === "hidden" || kitType === "submit"
|
|
42
|
+
? (field.value() ?? "")
|
|
43
|
+
: kitType === "radio" || (kitType === "checkbox" && isMultiple)
|
|
44
|
+
? "on"
|
|
45
|
+
: undefined;
|
|
46
|
+
return field.as(kitType, probe).name;
|
|
47
|
+
};
|
|
48
|
+
const isFile = kitType === "file";
|
|
49
|
+
const binding = {
|
|
50
|
+
inputType,
|
|
51
|
+
get name() {
|
|
52
|
+
return readName();
|
|
53
|
+
},
|
|
54
|
+
get value() {
|
|
55
|
+
const value = field.value();
|
|
56
|
+
return (isFile && value !== undefined ? [value] : value);
|
|
57
|
+
},
|
|
58
|
+
set value(next) {
|
|
59
|
+
field.set((isFile ? next?.at(0) : next));
|
|
60
|
+
},
|
|
61
|
+
get error() {
|
|
62
|
+
return field.issues()?.at(0)?.message;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
if (!isMultiple || settings?.type !== undefined)
|
|
66
|
+
return binding;
|
|
67
|
+
return Object.defineProperty(binding, IS_MULTIPLE_KINDS.has(inputType) ? "isMultiple" : "multiple", { value: true, enumerable: true });
|
|
68
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { RemoteFormInput } from "@sveltejs/kit";
|
|
2
|
+
import { type FormOwner } from "../owner/index.js";
|
|
3
|
+
import type { RemoteFormOf } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Makes a SvelteKit remote form a form owner, for a `Form` or `bindField`:
|
|
6
|
+
*
|
|
7
|
+
* ```svelte
|
|
8
|
+
* <Form {...save} owner={bindRemoteForm(save)}>
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* `values` are the remote form's values; a field reads and writes its value
|
|
12
|
+
* at its path through the remote form's field, and shows its first issue's
|
|
13
|
+
* message. A name may be the posted one: SvelteKit's `n:` and `b:` prefixes
|
|
14
|
+
* and `[]` suffix are dropped to find the field, so a number or checkbox
|
|
15
|
+
* field can take its name from `.as(type).name` and still post its type.
|
|
16
|
+
* The owner has no `submit`: the remote form handles the submit and its
|
|
17
|
+
* validation, and marks a field touched when its control changes.
|
|
18
|
+
* @note Pure: reads and writes the remote form.
|
|
19
|
+
*/
|
|
20
|
+
export default function bindRemoteForm<Input extends RemoteFormInput>(form: RemoteFormOf<Input>): FormOwner<Input>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { splitPath } from "../owner/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Makes a SvelteKit remote form a form owner, for a `Form` or `bindField`:
|
|
4
|
+
*
|
|
5
|
+
* ```svelte
|
|
6
|
+
* <Form {...save} owner={bindRemoteForm(save)}>
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* `values` are the remote form's values; a field reads and writes its value
|
|
10
|
+
* at its path through the remote form's field, and shows its first issue's
|
|
11
|
+
* message. A name may be the posted one: SvelteKit's `n:` and `b:` prefixes
|
|
12
|
+
* and `[]` suffix are dropped to find the field, so a number or checkbox
|
|
13
|
+
* field can take its name from `.as(type).name` and still post its type.
|
|
14
|
+
* The owner has no `submit`: the remote form handles the submit and its
|
|
15
|
+
* validation, and marks a field touched when its control changes.
|
|
16
|
+
* @note Pure: reads and writes the remote form.
|
|
17
|
+
*/
|
|
18
|
+
export default function bindRemoteForm(form) {
|
|
19
|
+
const root = form.fields;
|
|
20
|
+
const readField = (name) => splitPath(name.replace(/^[nb]:/, "")).reduce((node, segment) => node[segment], root);
|
|
21
|
+
return {
|
|
22
|
+
get values() {
|
|
23
|
+
return (root.value() ?? {});
|
|
24
|
+
},
|
|
25
|
+
getValue: (name) => readField(name).value(),
|
|
26
|
+
setValue: (name, value) => {
|
|
27
|
+
readField(name).set(value);
|
|
28
|
+
},
|
|
29
|
+
getError: (name) => readField(name).issues()?.at(0)?.message,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SvelteKit input type each `Field` `inputType` posts as, which gives the
|
|
3
|
+
* posted name its prefix: `n:` for a number, `b:` for a checkbox. A textarea
|
|
4
|
+
* posts as text; a choice group as a radio; a phone number as a `tel`. A
|
|
5
|
+
* custom field names its own type.
|
|
6
|
+
*/
|
|
7
|
+
export declare const REMOTE_INPUT_TYPES: {
|
|
8
|
+
readonly text: "text";
|
|
9
|
+
readonly email: "email";
|
|
10
|
+
readonly tel: "tel";
|
|
11
|
+
readonly url: "url";
|
|
12
|
+
readonly password: "password";
|
|
13
|
+
readonly number: "number";
|
|
14
|
+
readonly checkbox: "checkbox";
|
|
15
|
+
readonly switch: "checkbox";
|
|
16
|
+
readonly hidden: "hidden";
|
|
17
|
+
readonly range: "range";
|
|
18
|
+
readonly rating: "number";
|
|
19
|
+
readonly select: "select";
|
|
20
|
+
readonly combobox: "select";
|
|
21
|
+
readonly choices: "radio";
|
|
22
|
+
readonly textarea: "text";
|
|
23
|
+
readonly date: "date";
|
|
24
|
+
readonly time: "time";
|
|
25
|
+
readonly datetime: "datetime-local";
|
|
26
|
+
readonly file: "file";
|
|
27
|
+
readonly color: "color";
|
|
28
|
+
readonly phone: "tel";
|
|
29
|
+
readonly "rich-choices": "radio";
|
|
30
|
+
readonly custom: null;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The SvelteKit input type of each `Field` `inputType` that can hold several
|
|
34
|
+
* values, when it does: the posted name ends in `[]`.
|
|
35
|
+
*/
|
|
36
|
+
export declare const REMOTE_MULTIPLE_INPUT_TYPES: {
|
|
37
|
+
readonly select: "select multiple";
|
|
38
|
+
readonly combobox: "select multiple";
|
|
39
|
+
readonly choices: "checkbox";
|
|
40
|
+
readonly "rich-choices": "checkbox";
|
|
41
|
+
readonly file: "file multiple";
|
|
42
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SvelteKit input type each `Field` `inputType` posts as, which gives the
|
|
3
|
+
* posted name its prefix: `n:` for a number, `b:` for a checkbox. A textarea
|
|
4
|
+
* posts as text; a choice group as a radio; a phone number as a `tel`. A
|
|
5
|
+
* custom field names its own type.
|
|
6
|
+
*/
|
|
7
|
+
export const REMOTE_INPUT_TYPES = {
|
|
8
|
+
text: "text",
|
|
9
|
+
email: "email",
|
|
10
|
+
tel: "tel",
|
|
11
|
+
url: "url",
|
|
12
|
+
password: "password",
|
|
13
|
+
number: "number",
|
|
14
|
+
checkbox: "checkbox",
|
|
15
|
+
switch: "checkbox",
|
|
16
|
+
hidden: "hidden",
|
|
17
|
+
range: "range",
|
|
18
|
+
rating: "number",
|
|
19
|
+
select: "select",
|
|
20
|
+
combobox: "select",
|
|
21
|
+
choices: "radio",
|
|
22
|
+
textarea: "text",
|
|
23
|
+
date: "date",
|
|
24
|
+
time: "time",
|
|
25
|
+
datetime: "datetime-local",
|
|
26
|
+
file: "file",
|
|
27
|
+
color: "color",
|
|
28
|
+
phone: "tel",
|
|
29
|
+
"rich-choices": "radio",
|
|
30
|
+
custom: null,
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The SvelteKit input type of each `Field` `inputType` that can hold several
|
|
34
|
+
* values, when it does: the posted name ends in `[]`.
|
|
35
|
+
*/
|
|
36
|
+
export const REMOTE_MULTIPLE_INPUT_TYPES = {
|
|
37
|
+
select: "select multiple",
|
|
38
|
+
combobox: "select multiple",
|
|
39
|
+
choices: "checkbox",
|
|
40
|
+
"rich-choices": "checkbox",
|
|
41
|
+
file: "file multiple",
|
|
42
|
+
};
|