@formisch/svelte 0.3.0 → 0.4.1

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.
@@ -1,69 +1,39 @@
1
1
  import { createFormStore, getFieldBool, INTERNAL, validateFormInput, } from '../../core/index.svelte';
2
2
  import { onMount } from 'svelte';
3
3
  import * as v from 'valibot';
4
+ // @__NO_SIDE_EFFECTS__
4
5
  export function createForm(config) {
5
- var _a;
6
- var internalFormStore = createFormStore(config, function (input) {
7
- return v.safeParseAsync(config.schema, input);
8
- });
9
- onMount(function () {
6
+ const internalFormStore = createFormStore(config, (input) => v.safeParseAsync(config.schema, input));
7
+ onMount(() => {
10
8
  if (config.validate === 'initial') {
11
9
  validateFormInput(internalFormStore);
12
10
  }
13
11
  });
14
- var isTouched = $derived(getFieldBool(internalFormStore, 'isTouched'));
15
- var isDirty = $derived(getFieldBool(internalFormStore, 'isDirty'));
16
- var isValid = $derived(!getFieldBool(internalFormStore, 'errors'));
17
- return _a = {},
18
- _a[INTERNAL] = internalFormStore,
19
- Object.defineProperty(_a, "isSubmitting", {
20
- get: function () {
21
- return internalFormStore.isSubmitting.value;
22
- },
23
- enumerable: false,
24
- configurable: true
25
- }),
26
- Object.defineProperty(_a, "isSubmitted", {
27
- get: function () {
28
- return internalFormStore.isSubmitted.value;
29
- },
30
- enumerable: false,
31
- configurable: true
32
- }),
33
- Object.defineProperty(_a, "isValidating", {
34
- get: function () {
35
- return internalFormStore.isValidating.value;
36
- },
37
- enumerable: false,
38
- configurable: true
39
- }),
40
- Object.defineProperty(_a, "isTouched", {
41
- get: function () {
42
- return isTouched;
43
- },
44
- enumerable: false,
45
- configurable: true
46
- }),
47
- Object.defineProperty(_a, "isDirty", {
48
- get: function () {
49
- return isDirty;
50
- },
51
- enumerable: false,
52
- configurable: true
53
- }),
54
- Object.defineProperty(_a, "isValid", {
55
- get: function () {
56
- return isValid;
57
- },
58
- enumerable: false,
59
- configurable: true
60
- }),
61
- Object.defineProperty(_a, "errors", {
62
- get: function () {
63
- return internalFormStore.errors.value;
64
- },
65
- enumerable: false,
66
- configurable: true
67
- }),
68
- _a;
12
+ const isTouched = $derived(getFieldBool(internalFormStore, 'isTouched'));
13
+ const isDirty = $derived(getFieldBool(internalFormStore, 'isDirty'));
14
+ const isValid = $derived(!getFieldBool(internalFormStore, 'errors'));
15
+ return {
16
+ [INTERNAL]: internalFormStore,
17
+ get isSubmitting() {
18
+ return internalFormStore.isSubmitting.value;
19
+ },
20
+ get isSubmitted() {
21
+ return internalFormStore.isSubmitted.value;
22
+ },
23
+ get isValidating() {
24
+ return internalFormStore.isValidating.value;
25
+ },
26
+ get isTouched() {
27
+ return isTouched;
28
+ },
29
+ get isDirty() {
30
+ return isDirty;
31
+ },
32
+ get isValid() {
33
+ return isValid;
34
+ },
35
+ get errors() {
36
+ return internalFormStore.errors.value;
37
+ },
38
+ };
69
39
  }
@@ -1,7 +1,21 @@
1
1
  import { type RequiredPath, type Schema, type ValidPath } from '../../core/index.svelte';
2
2
  import type * as v from 'valibot';
3
3
  import type { FieldStore, FormStore, MaybeGetter } from '../../types/index.ts';
4
+ /**
5
+ * Use field config interface.
6
+ */
4
7
  export interface UseFieldConfig<TSchema extends Schema = Schema, TFieldPath extends RequiredPath = RequiredPath> {
8
+ /**
9
+ * The path to the field within the form schema.
10
+ */
5
11
  readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
6
12
  }
13
+ /**
14
+ * Creates a reactive field store of a specific field within a form store.
15
+ *
16
+ * @param form The form store instance or getter function.
17
+ * @param config The field configuration or getter function.
18
+ *
19
+ * @returns The field store with reactive properties and element props.
20
+ */
7
21
  export declare function useField<TSchema extends Schema, TFieldPath extends RequiredPath>(form: MaybeGetter<FormStore<TSchema>>, config: MaybeGetter<UseFieldConfig<TSchema, TFieldPath>>): FieldStore<TSchema, TFieldPath>;
@@ -1,15 +1,15 @@
1
1
  import { getElementInput, getFieldBool, getFieldInput, getFieldStore, INTERNAL, setFieldBool, setFieldInput, validateIfRequired, } from '../../core/index.svelte';
2
2
  import { createAttachmentKey } from 'svelte/attachments';
3
3
  import { unwrap } from '../../utils/index';
4
+ // @__NO_SIDE_EFFECTS__
4
5
  export function useField(form, config) {
5
- var _a;
6
- var path = $derived(unwrap(config).path);
7
- var internalFormStore = $derived(unwrap(form)[INTERNAL]);
8
- var internalFieldStore = $derived(getFieldStore(internalFormStore, path));
9
- var input = $derived(getFieldInput(internalFieldStore));
10
- var isTouched = $derived(getFieldBool(internalFieldStore, 'isTouched'));
11
- var isDirty = $derived(getFieldBool(internalFieldStore, 'isDirty'));
12
- var isValid = $derived(!getFieldBool(internalFieldStore, 'errors'));
6
+ const path = $derived(unwrap(config).path);
7
+ const internalFormStore = $derived(unwrap(form)[INTERNAL]);
8
+ const internalFieldStore = $derived(getFieldStore(internalFormStore, path));
9
+ const input = $derived(getFieldInput(internalFieldStore));
10
+ const isTouched = $derived(getFieldBool(internalFieldStore, 'isTouched'));
11
+ const isDirty = $derived(getFieldBool(internalFieldStore, 'isDirty'));
12
+ const isValid = $derived(!getFieldBool(internalFieldStore, 'errors'));
13
13
  return {
14
14
  get path() {
15
15
  return path;
@@ -29,32 +29,31 @@ export function useField(form, config) {
29
29
  get isValid() {
30
30
  return isValid;
31
31
  },
32
- props: (_a = {
33
- get name() {
34
- return internalFieldStore.name;
35
- },
36
- autofocus: !!internalFieldStore.errors.value
32
+ props: {
33
+ get name() {
34
+ return internalFieldStore.name;
37
35
  },
38
- _a[createAttachmentKey()] = function (element) {
36
+ autofocus: !!internalFieldStore.errors.value,
37
+ [createAttachmentKey()](element) {
39
38
  internalFieldStore.elements.push(element);
40
- return function () {
41
- internalFieldStore.elements = internalFieldStore.elements.filter(function (el) { return el !== element; });
39
+ return () => {
40
+ internalFieldStore.elements = internalFieldStore.elements.filter((el) => el !== element);
42
41
  };
43
42
  },
44
- _a.onfocus = function () {
43
+ onfocus() {
45
44
  setFieldBool(internalFieldStore, 'isTouched', true);
46
45
  validateIfRequired(internalFormStore, internalFieldStore, 'touch');
47
46
  },
48
- _a.oninput = function (event) {
47
+ oninput(event) {
49
48
  setFieldInput(internalFormStore, path, getElementInput(event.currentTarget, internalFieldStore));
50
49
  validateIfRequired(internalFormStore, internalFieldStore, 'input');
51
50
  },
52
- _a.onchange = function () {
51
+ onchange() {
53
52
  validateIfRequired(internalFormStore, internalFieldStore, 'change');
54
53
  },
55
- _a.onblur = function () {
54
+ onblur() {
56
55
  validateIfRequired(internalFormStore, internalFieldStore, 'blur');
57
56
  },
58
- _a),
57
+ },
59
58
  };
60
59
  }
@@ -1,7 +1,21 @@
1
1
  import { type RequiredPath, type Schema, type ValidArrayPath } from '../../core/index.svelte';
2
2
  import type * as v from 'valibot';
3
3
  import type { FieldArrayStore, FormStore, MaybeGetter } from '../../types/index.ts';
4
+ /**
5
+ * Use field array config interface.
6
+ */
4
7
  export interface UseFieldArrayConfig<TSchema extends Schema = Schema, TFieldArrayPath extends RequiredPath = RequiredPath> {
8
+ /**
9
+ * The path to the field array within the form schema.
10
+ */
5
11
  readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
6
12
  }
13
+ /**
14
+ * Creates a reactive field array store of a specific field array within a form store.
15
+ *
16
+ * @param form The form store instance or getter function.
17
+ * @param config The field array configuration or getter function.
18
+ *
19
+ * @returns The field array store with reactive properties for array management.
20
+ */
7
21
  export declare function useFieldArray<TSchema extends Schema, TFieldArrayPath extends RequiredPath>(form: MaybeGetter<FormStore<TSchema>>, config: MaybeGetter<UseFieldArrayConfig<TSchema, TFieldArrayPath>>): FieldArrayStore<TSchema, TFieldArrayPath>;
@@ -1,11 +1,12 @@
1
1
  import { getFieldBool, getFieldStore, INTERNAL, } from '../../core/index.svelte';
2
2
  import { unwrap } from '../../utils/index';
3
+ // @__NO_SIDE_EFFECTS__
3
4
  export function useFieldArray(form, config) {
4
- var path = $derived(unwrap(config).path);
5
- var internalFieldStore = $derived(getFieldStore(unwrap(form)[INTERNAL], path));
6
- var isTouched = $derived(getFieldBool(internalFieldStore, 'isTouched'));
7
- var isDirty = $derived(getFieldBool(internalFieldStore, 'isDirty'));
8
- var isValid = $derived(!getFieldBool(internalFieldStore, 'errors'));
5
+ const path = $derived(unwrap(config).path);
6
+ const internalFieldStore = $derived(getFieldStore(unwrap(form)[INTERNAL], path));
7
+ const isTouched = $derived(getFieldBool(internalFieldStore, 'isTouched'));
8
+ const isDirty = $derived(getFieldBool(internalFieldStore, 'isDirty'));
9
+ const isValid = $derived(!getFieldBool(internalFieldStore, 'errors'));
9
10
  return {
10
11
  get path() {
11
12
  return path;
@@ -2,37 +2,97 @@ import type { FieldElement, PartialValues, PathValue, RequiredPath, Schema, Vali
2
2
  import type { FocusEventHandler, FormEventHandler } from 'svelte/elements';
3
3
  import type * as v from 'valibot';
4
4
  /**
5
- * Value type of the field element props.
5
+ * Field element props interface.
6
6
  */
7
7
  export interface FieldElementProps {
8
+ /**
9
+ * The name of the field.
10
+ */
8
11
  readonly name: string;
12
+ /**
13
+ * Whether the field should autofocus.
14
+ */
9
15
  readonly autofocus: boolean;
16
+ /**
17
+ * The element reference callback with cleanup.
18
+ */
10
19
  readonly [ref: symbol]: (element: FieldElement) => () => void;
20
+ /**
21
+ * The focus event handler of the field element.
22
+ */
11
23
  readonly onfocus: FocusEventHandler<FieldElement>;
24
+ /**
25
+ * The input event handler of the field element.
26
+ */
12
27
  readonly oninput: FormEventHandler<FieldElement>;
28
+ /**
29
+ * The change event handler of the field element.
30
+ */
13
31
  readonly onchange: FormEventHandler<FieldElement>;
32
+ /**
33
+ * The blur event handler of the field element.
34
+ */
14
35
  readonly onblur: FocusEventHandler<FieldElement>;
15
36
  }
16
37
  /**
17
- * Value type of the field store.
38
+ * Field store interface.
18
39
  */
19
40
  export interface FieldStore<TSchema extends Schema = Schema, TFieldPath extends RequiredPath = RequiredPath> {
41
+ /**
42
+ * The path to the field within the form.
43
+ */
20
44
  readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
45
+ /**
46
+ * The current input value of the field.
47
+ */
21
48
  readonly input: PartialValues<PathValue<v.InferInput<TSchema>, TFieldPath>>;
49
+ /**
50
+ * The current error messages of the field.
51
+ */
22
52
  readonly errors: [string, ...string[]] | null;
53
+ /**
54
+ * Whether the field has been touched.
55
+ */
23
56
  readonly isTouched: boolean;
57
+ /**
58
+ * Whether the field input differs from its initial value.
59
+ */
24
60
  readonly isDirty: boolean;
61
+ /**
62
+ * Whether the field is valid according to the schema.
63
+ */
25
64
  readonly isValid: boolean;
65
+ /**
66
+ * The props for the field element.
67
+ */
26
68
  readonly props: FieldElementProps;
27
69
  }
28
70
  /**
29
- * Value type of the field array store.
71
+ * Field array store interface.
30
72
  */
31
73
  export interface FieldArrayStore<TSchema extends Schema = Schema, TFieldArrayPath extends RequiredPath = RequiredPath> {
74
+ /**
75
+ * The path to the array field within the form.
76
+ */
32
77
  readonly path: ValidArrayPath<v.InferInput<TSchema>, TFieldArrayPath>;
78
+ /**
79
+ * The item IDs of the array field.
80
+ */
33
81
  readonly items: string[];
82
+ /**
83
+ * The current error messages of the field array.
84
+ */
34
85
  readonly errors: [string, ...string[]] | null;
86
+ /**
87
+ * Whether the field array has been touched.
88
+ */
35
89
  readonly isTouched: boolean;
90
+ /**
91
+ * Whether the field array input differs from its initial value.
92
+ */
36
93
  readonly isDirty: boolean;
94
+ /**
95
+ * Whether the field array is valid according to the schema.
96
+ */
37
97
  readonly isValid: boolean;
38
98
  }
@@ -1,10 +1,37 @@
1
1
  import type { BaseFormStore, Schema } from '../core/index.svelte';
2
+ /**
3
+ * Form store interface.
4
+ */
2
5
  export interface FormStore<TSchema extends Schema = Schema> extends BaseFormStore<TSchema> {
6
+ /**
7
+ * Whether the form is currently submitting.
8
+ */
3
9
  readonly isSubmitting: boolean;
10
+ /**
11
+ * Whether the form has been submitted.
12
+ */
4
13
  readonly isSubmitted: boolean;
14
+ /**
15
+ * Whether the form is currently validating.
16
+ */
5
17
  readonly isValidating: boolean;
18
+ /**
19
+ * Whether any field in the form has been touched.
20
+ */
6
21
  readonly isTouched: boolean;
22
+ /**
23
+ * Whether any field in the form differs from its initial value.
24
+ */
7
25
  readonly isDirty: boolean;
26
+ /**
27
+ * Whether the form is valid according to the schema.
28
+ */
8
29
  readonly isValid: boolean;
30
+ /**
31
+ * The current error messages of the form.
32
+ *
33
+ * Hint: This property only contains validation errors at the root level
34
+ * of the form. To get all errors from all fields, use `getAllErrors`.
35
+ */
9
36
  readonly errors: [string, ...string[]] | null;
10
37
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formisch/svelte",
3
3
  "description": "The modular and type-safe form library for Svelte",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "license": "MIT",
6
6
  "author": "Fabian Hiller",
7
7
  "homepage": "https://formisch.dev",
@@ -51,8 +51,8 @@
51
51
  "typescript": "^5.9.2",
52
52
  "typescript-eslint": "^8.43.0",
53
53
  "vite": "^7.1.5",
54
- "@formisch/core": "0.4.0",
55
- "@formisch/methods": "0.3.0"
54
+ "@formisch/core": "0.4.1",
55
+ "@formisch/methods": "0.4.1"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "svelte": "^5.29.0",