@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.
Files changed (60) hide show
  1. package/LICENSE +166 -0
  2. package/README.md +213 -8
  3. package/dist/component/CheckboxField/CheckboxField.svelte +21 -3
  4. package/dist/component/CheckboxField/CheckboxField.svelte.d.ts +2 -2
  5. package/dist/component/CheckboxField/types.d.ts +13 -1
  6. package/dist/component/ChoicesField/Choices.svelte +5 -3
  7. package/dist/component/RichChoicesField/RichChoices.svelte +5 -3
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.js +1 -1
  10. package/dist/pattern/Field/Field.svelte +20 -4
  11. package/dist/pattern/Field/types.d.ts +1 -1
  12. package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte +18 -2
  13. package/dist/subcomponent/CheckboxInput/CheckboxInput.svelte.d.ts +4 -2
  14. package/dist/subcomponent/CheckboxInput/types.d.ts +7 -0
  15. package/dist/subcomponent/RadioInput/RadioInput.svelte +28 -4
  16. package/dist/subcomponent/RadioInput/RadioInput.svelte.d.ts +4 -2
  17. package/dist/subcomponent/RadioInput/types.d.ts +7 -0
  18. package/dist/utils/index.d.ts +3 -0
  19. package/dist/utils/index.js +3 -0
  20. package/dist/utils/owner/bindField.d.ts +5 -3
  21. package/dist/utils/owner/bindField.js +3 -1
  22. package/dist/utils/owner/bindFieldArray.d.ts +20 -0
  23. package/dist/utils/owner/bindFieldArray.js +62 -0
  24. package/dist/utils/owner/bindTanStackForm.d.ts +18 -0
  25. package/dist/utils/owner/bindTanStackForm.js +65 -0
  26. package/dist/utils/owner/createBinding.js +6 -0
  27. package/dist/utils/owner/createFormState.svelte.d.ts +12 -3
  28. package/dist/utils/owner/createFormState.svelte.js +43 -21
  29. package/dist/utils/owner/createSchemaValidator.d.ts +21 -0
  30. package/dist/utils/owner/createSchemaValidator.js +36 -0
  31. package/dist/utils/owner/createValidatedBinding.d.ts +2 -2
  32. package/dist/utils/owner/createValidatedBinding.js +8 -4
  33. package/dist/utils/owner/getPath.d.ts +6 -0
  34. package/dist/utils/owner/getPath.js +15 -0
  35. package/dist/utils/owner/index.d.ts +8 -0
  36. package/dist/utils/owner/index.js +8 -0
  37. package/dist/utils/owner/normalizePath.d.ts +6 -0
  38. package/dist/utils/owner/normalizePath.js +9 -0
  39. package/dist/utils/owner/readOwnerValue.d.ts +7 -0
  40. package/dist/utils/owner/readOwnerValue.js +11 -0
  41. package/dist/utils/owner/setPath.d.ts +12 -0
  42. package/dist/utils/owner/setPath.js +36 -0
  43. package/dist/utils/owner/splitPath.d.ts +7 -0
  44. package/dist/utils/owner/splitPath.js +9 -0
  45. package/dist/utils/owner/types.d.ts +141 -12
  46. package/dist/utils/remoteForm/bindRemoteField.d.ts +23 -0
  47. package/dist/utils/remoteForm/bindRemoteField.js +68 -0
  48. package/dist/utils/remoteForm/bindRemoteForm.d.ts +20 -0
  49. package/dist/utils/remoteForm/bindRemoteForm.js +31 -0
  50. package/dist/utils/remoteForm/constants.d.ts +42 -0
  51. package/dist/utils/remoteForm/constants.js +42 -0
  52. package/dist/utils/remoteForm/index.d.ts +3 -0
  53. package/dist/utils/remoteForm/index.js +2 -0
  54. package/dist/utils/remoteForm/types.d.ts +90 -0
  55. package/dist/utils/remoteForm/types.js +1 -0
  56. package/package.json +24 -11
  57. /package/dist/{component/ChoicesField → utils}/isChoiceChecked.d.ts +0 -0
  58. /package/dist/{component/ChoicesField → utils}/isChoiceChecked.js +0 -0
  59. /package/dist/{component/ChoicesField → utils}/toggleChoice.d.ts +0 -0
  60. /package/dist/{component/ChoicesField → utils}/toggleChoice.js +0 -0
@@ -0,0 +1,90 @@
1
+ import type { RemoteForm, RemoteFormFieldType, RemoteFormFieldValue, RemoteFormInput, RemoteFormIssue } from "@sveltejs/kit";
2
+ import type { FieldProps } from "../../pattern/index.js";
3
+ import type { REMOTE_INPUT_TYPES, REMOTE_MULTIPLE_INPUT_TYPES } from "./constants.js";
4
+ /** Every `inputType` a `Field` routes on. */
5
+ export type FieldInputType = FieldProps["inputType"];
6
+ /** Every input type a SvelteKit remote form field takes in `.as(type)`. */
7
+ export type KitInputType = RemoteFormFieldType<RemoteFormFieldValue>;
8
+ /**
9
+ * A SvelteKit remote form field that holds a value, as `bindRemoteField`
10
+ * uses it: `form.fields.age`, not a field holding nested fields.
11
+ */
12
+ export type RemoteField = {
13
+ /** Returns the value that will be posted. */
14
+ value(): unknown;
15
+ /** Writes the value that will be posted. */
16
+ set(value: never): unknown;
17
+ /** Returns the field's validation issues, if any. */
18
+ issues(): RemoteFormIssue[] | undefined;
19
+ /** Returns the props to spread on an input of a SvelteKit type. */
20
+ as(...args: never[]): {
21
+ name: string;
22
+ };
23
+ };
24
+ /**
25
+ * The value type of a remote form field, read back from its `value()`: an
26
+ * array's items are not optional.
27
+ */
28
+ export type RemoteValueOf<Field extends RemoteField> = NonNullable<ReturnType<Field["value"]>> extends readonly (infer Item)[] ? NonNullable<Item>[] : NonNullable<ReturnType<Field["value"]>>;
29
+ /** The `inputType`s whose value can hold several values. */
30
+ type MultipleInputType = keyof typeof REMOTE_MULTIPLE_INPUT_TYPES;
31
+ /**
32
+ * The SvelteKit type a `Field` `inputType` posts as, for a value type: the
33
+ * multiple type of a kind with one when the value is an array.
34
+ */
35
+ type KitTypeOf<Kind extends FieldInputType, Value> = [Value] extends [
36
+ readonly unknown[]
37
+ ] ? Kind extends MultipleInputType ? (typeof REMOTE_MULTIPLE_INPUT_TYPES)[Kind] : never : (typeof REMOTE_INPUT_TYPES)[Kind];
38
+ /** The value a field of `Kind` binds, from a remote value: a file is a list. */
39
+ type BoundValue<Kind extends FieldInputType, Value> = Kind extends "file" ? Value extends File ? File[] : Value : Value;
40
+ /** The value type of a `Field` of `Kind`. */
41
+ type FieldValueOf<Kind extends FieldInputType> = FieldProps extends infer Props ? Props extends {
42
+ inputType: infer Type;
43
+ value?: infer Bound;
44
+ } ? Kind extends Type ? Bound : never : never : never;
45
+ /** Tells whether a field of `Kind` binds a remote value of type `Value`. */
46
+ type Fits<Kind extends FieldInputType, Value> = Kind extends "custom" ? true : [KitTypeOf<Kind, Value>] extends [never] ? false : [KitTypeOf<Kind, Value>] extends [RemoteFormFieldType<Value>] ? [BoundValue<Kind, Value>] extends [NonNullable<FieldValueOf<Kind>>] ? true : false : false;
47
+ /**
48
+ * The `Field` `inputType`s that bind a remote value of type `Value`: the
49
+ * SvelteKit type each posts as takes the value, and the field binds it.
50
+ */
51
+ export type FittingInputType<Value> = {
52
+ [Kind in FieldInputType]: Fits<Kind, Value> extends true ? Kind : never;
53
+ }[FieldInputType];
54
+ /**
55
+ * The options `bindRemoteField` needs: the SvelteKit type of a custom field,
56
+ * and `multiple` for a kind that holds an array.
57
+ */
58
+ export type RemoteFieldOptions<Kind extends FieldInputType, Value> = Kind extends "custom" ? [
59
+ options: {
60
+ /** The SvelteKit type the custom control posts as. */
61
+ type: RemoteFormFieldType<Value>;
62
+ /** Is the value an array of checkbox values. */
63
+ multiple?: true;
64
+ }
65
+ ] : [Value] extends [readonly unknown[]] ? [options: {
66
+ multiple: true;
67
+ }] : [];
68
+ /** The prop that makes a `Field` of `Kind` hold several values. */
69
+ type MultipleProp<Kind extends FieldInputType> = Kind extends "select" | "file" ? {
70
+ readonly multiple: true;
71
+ } : Kind extends MultipleInputType ? {
72
+ readonly isMultiple: true;
73
+ } : unknown;
74
+ /**
75
+ * A remote form field's state, to spread onto a `Field`: its `inputType`, the
76
+ * name SvelteKit decodes, the bound value and the first issue's message.
77
+ */
78
+ export type RemoteFieldBinding<Kind extends FieldInputType, Value> = {
79
+ /** The field kind `Field` routes on. */
80
+ readonly inputType: Kind;
81
+ /** The posted name, with SvelteKit's type prefix and array suffix. */
82
+ readonly name: string;
83
+ /** The field's value. Writing it sets the remote form's value. */
84
+ value: BoundValue<Kind, Value> | undefined;
85
+ /** The first validation issue's message, if any. */
86
+ readonly error: string | undefined;
87
+ } & ([Value] extends [readonly unknown[]] ? MultipleProp<Kind> : unknown);
88
+ /** A SvelteKit remote form that takes input, as `bindRemoteForm` reads it. */
89
+ export type RemoteFormOf<Input extends RemoteFormInput> = Pick<RemoteForm<Input, unknown>, "fields">;
90
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/svelte-ds-global-form",
3
3
  "description": "Svelte form fields for the Pragma design system, with bindable values.",
4
- "version": "0.47.0",
4
+ "version": "0.49.0",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -56,21 +56,22 @@
56
56
  "test:server": "vitest --run --project=server"
57
57
  },
58
58
  "dependencies": {
59
- "@canonical/ds-utils": "^0.47.0",
60
- "@canonical/styles": "^0.47.0"
59
+ "@canonical/ds-utils": "^0.49.0",
60
+ "@canonical/styles": "^0.49.0"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@biomejs/biome": "2.6.9",
64
64
  "@canonical/biome-config": "0.47.0",
65
- "@canonical/storybook-addon-msw": "^0.47.0",
66
- "@canonical/storybook-config": "^0.47.0",
67
- "@canonical/storybook-helpers": "^0.47.0",
68
- "@canonical/svelte-ds-global": "^0.47.0",
69
- "@canonical/svelte-ssr-test": "^0.47.0",
70
- "@canonical/typescript-config-svelte": "^0.47.0",
65
+ "@canonical/storybook-addon-msw": "^0.49.0",
66
+ "@canonical/storybook-config": "^0.49.0",
67
+ "@canonical/storybook-helpers": "^0.49.0",
68
+ "@canonical/svelte-ds-global": "^0.49.0",
69
+ "@canonical/svelte-ssr-test": "^0.49.0",
70
+ "@canonical/typescript-config-svelte": "^0.49.0",
71
71
  "@canonical/webarchitect": "0.46.0",
72
72
  "@storybook/addon-docs": "^10.6.0",
73
73
  "@storybook/addon-svelte-csf": "^5.1.3",
74
+ "@sveltejs/kit": "2.70.3",
74
75
  "@sveltejs/package": "3.0.0",
75
76
  "@sveltejs/vite-plugin-svelte": "^7.0.0",
76
77
  "@tanstack/svelte-form": "1.33.5",
@@ -88,14 +89,26 @@
88
89
  "typescript": "~6.0.3",
89
90
  "vite": "^8.0.1",
90
91
  "vitest": "^5.0.0",
91
- "vitest-browser-svelte": "^3.0.0"
92
+ "vitest-browser-svelte": "^3.0.0",
93
+ "zod": "^4.6.7"
92
94
  },
93
95
  "peerDependencies": {
96
+ "@sveltejs/kit": "^2.70.0",
97
+ "@tanstack/svelte-form": "^1.33.5",
94
98
  "svelte": "^5.47.1"
95
99
  },
100
+ "peerDependenciesMeta": {
101
+ "@sveltejs/kit": {
102
+ "optional": true
103
+ },
104
+ "@tanstack/svelte-form": {
105
+ "optional": true
106
+ }
107
+ },
96
108
  "nx": {
97
109
  "tags": [
98
110
  "playwright"
99
111
  ]
100
- }
112
+ },
113
+ "gitHead": "9b71099ded16351ee14432246e3d8190f48767ed"
101
114
  }