@formisch/svelte 0.5.6 → 0.7.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.
@@ -3,6 +3,7 @@
3
3
  INTERNAL,
4
4
  type Schema,
5
5
  type SubmitHandler,
6
+ type SubmitEventHandler,
6
7
  } from '../../core/index.svelte';
7
8
  import { handleSubmit } from '../../methods/index.svelte';
8
9
  import type { FormStore } from '../../types/index';
@@ -23,7 +24,7 @@
23
24
  /**
24
25
  * The submit handler called when the form is submitted and validation succeeds.
25
26
  */
26
- onsubmit: SubmitHandler<TSchema>;
27
+ onsubmit: SubmitHandler<TSchema> | SubmitEventHandler<TSchema>;
27
28
  /**
28
29
  * The child elements to render within the form.
29
30
  */
@@ -1,4 +1,4 @@
1
- import { type Schema, type SubmitHandler } from '../../core/index.svelte';
1
+ import { type Schema, type SubmitHandler, type SubmitEventHandler } from '../../core/index.svelte';
2
2
  import type { FormStore } from '../../types/index';
3
3
  import type { Snippet } from 'svelte';
4
4
  import type { HTMLFormAttributes } from 'svelte/elements';
@@ -13,7 +13,7 @@ export type FormProps<TSchema extends Schema = Schema> = Omit<HTMLFormAttributes
13
13
  /**
14
14
  * The submit handler called when the form is submitted and validation succeeds.
15
15
  */
16
- onsubmit: SubmitHandler<TSchema>;
16
+ onsubmit: SubmitHandler<TSchema> | SubmitEventHandler<TSchema>;
17
17
  /**
18
18
  * The child elements to render within the form.
19
19
  */
@@ -295,7 +295,11 @@ interface BaseFormStore<TSchema extends Schema = Schema> {
295
295
  /**
296
296
  * Submit handler type.
297
297
  */
298
- type SubmitHandler<TSchema extends Schema> = (output: v.InferOutput<TSchema>, event: SubmitEvent) => MaybePromise<unknown>;
298
+ type SubmitHandler<TSchema extends Schema> = (output: v.InferOutput<TSchema>) => MaybePromise<unknown>;
299
+ /**
300
+ * Submit event handler type.
301
+ */
302
+ type SubmitEventHandler<TSchema extends Schema> = (output: v.InferOutput<TSchema>, event: SubmitEvent) => MaybePromise<unknown>;
299
303
  //#endregion
300
304
  //#region src/types/path.d.ts
301
305
  /**
@@ -547,7 +551,7 @@ declare function validateIfRequired(internalFormStore: InternalFormStore, intern
547
551
  /**
548
552
  * Framework type.
549
553
  */
550
- type Framework = "preact" | "qwik" | "solid" | "svelte" | "vanilla" | "vue";
554
+ type Framework = "preact" | "qwik" | "react" | "solid" | "svelte" | "vue";
551
555
  //#endregion
552
556
  //#region src/framework/index.svelte.d.ts
553
557
  /**
@@ -578,4 +582,4 @@ declare function createSignal<T>(initialValue: T): Signal<T>;
578
582
  */
579
583
  declare function batch<T>(fn: () => T): T;
580
584
  //#endregion
581
- export { BaseFormStore, Batch, DeepPartial, FieldElement, FieldSchema, FormConfig, INTERNAL, InternalArrayStore, InternalBaseStore, InternalFieldStore, InternalFormStore, InternalObjectStore, InternalValueStore, IsAny, IsNever, MaybePromise, PartialValues, Path, PathKey, PathValue, RequiredPath, Schema, Signal, SubmitHandler, Untrack, ValidArrayPath, ValidPath, ValidateFormInputConfig, ValidationMode, batch, copyItemState, createFormStore, createId, createSignal, framework, getElementInput, getFieldBool, getFieldInput, getFieldStore, initializeFieldStore, resetItemState, setFieldBool, setFieldInput, setInitialFieldInput, swapItemState, untrack, validateFormInput, validateIfRequired, walkFieldStore };
585
+ export { BaseFormStore, Batch, DeepPartial, FieldElement, FieldSchema, FormConfig, INTERNAL, InternalArrayStore, InternalBaseStore, InternalFieldStore, InternalFormStore, InternalObjectStore, InternalValueStore, IsAny, IsNever, MaybePromise, PartialValues, Path, PathKey, PathValue, RequiredPath, Schema, Signal, SubmitEventHandler, SubmitHandler, Untrack, ValidArrayPath, ValidPath, ValidateFormInputConfig, ValidationMode, batch, copyItemState, createFormStore, createId, createSignal, framework, getElementInput, getFieldBool, getFieldInput, getFieldStore, initializeFieldStore, resetItemState, setFieldBool, setFieldInput, setInitialFieldInput, swapItemState, untrack, validateFormInput, validateIfRequired, walkFieldStore };
@@ -86,7 +86,7 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path, nu
86
86
  initializeFieldStore(internalFieldStore.children[index], schema.item, initialInput[index], path);
87
87
  path.pop();
88
88
  }
89
- } else for (let index = 0; index < schema.items; index++) {
89
+ } else for (let index = 0; index < schema.items.length; index++) {
90
90
  internalFieldStore.children[index] = {};
91
91
  path.push(index);
92
92
  initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput?.[index], path);
@@ -426,7 +426,7 @@ function setNestedInput(internalFieldStore, input) {
426
426
  }
427
427
  for (let index = 0; index < arrayInput.length; index++) setNestedInput(internalFieldStore.children[index], arrayInput[index]);
428
428
  internalFieldStore.input.value = input == null ? input : true;
429
- internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== items.length;
429
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== internalFieldStore.items.value.length;
430
430
  } else if (internalFieldStore.kind === "object") {
431
431
  for (const key in internalFieldStore.children) setNestedInput(internalFieldStore.children[key], input?.[key]);
432
432
  internalFieldStore.input.value = input == null ? input : true;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { DeepPartial, FieldElement, FormConfig, PartialValues, PathValue, RequiredPath, Schema, SubmitHandler, ValidArrayPath, ValidationMode, ValidPath, } from './core/index.svelte';
1
+ export type { DeepPartial, FieldElement, FormConfig, PartialValues, PathValue, RequiredPath, Schema, SubmitEventHandler, SubmitHandler, ValidArrayPath, ValidationMode, ValidPath, } from './core/index.svelte';
2
2
  export * from './methods/index.svelte';
3
3
  export * from './components/index';
4
4
  export * from './runes/index';
@@ -1,4 +1,4 @@
1
- import { BaseFormStore, DeepPartial, PartialValues, PathValue, RequiredPath, Schema, SubmitHandler, ValidArrayPath, ValidPath } from "../core/index.svelte";
1
+ import { BaseFormStore, DeepPartial, PartialValues, PathValue, RequiredPath, Schema, SubmitEventHandler, SubmitHandler, ValidArrayPath, ValidPath } from "../core/index.svelte";
2
2
  import * as v from "valibot";
3
3
 
4
4
  //#region src/focus/focus.d.ts
@@ -116,6 +116,17 @@ declare function getInput<TSchema extends Schema, TFieldPath extends RequiredPat
116
116
  //#endregion
117
117
  //#region src/handleSubmit/handleSubmit.d.ts
118
118
  /**
119
+ * Creates a submit event handler for the form that validates the form input,
120
+ * and calls the provided handler if validation succeeds. This is designed to
121
+ * be used with the form's onsubmit event.
122
+ *
123
+ * @param form The form store to handle submission for.
124
+ * @param handler The submit handler function called with validated output if validation succeeds.
125
+ *
126
+ * @returns A submit event handler function to attach to the form element.
127
+ */
128
+ declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitHandler<TSchema>): () => Promise<void>;
129
+ /**
119
130
  * Creates a submit event handler for the form that prevents default browser
120
131
  * submission, validates the form input, and calls the provided handler if
121
132
  * validation succeeds. This is designed to be used with the form's onsubmit event.
@@ -125,7 +136,7 @@ declare function getInput<TSchema extends Schema, TFieldPath extends RequiredPat
125
136
  *
126
137
  * @returns A submit event handler function to attach to the form element.
127
138
  */
128
- declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitHandler<TSchema>): (event: SubmitEvent) => Promise<void>;
139
+ declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitEventHandler<TSchema>): (event: SubmitEvent) => Promise<void>;
129
140
  //#endregion
130
141
  //#region src/insert/insert.d.ts
131
142
  /**
@@ -54,7 +54,7 @@ function getInput(form, config) {
54
54
  /* @__NO_SIDE_EFFECTS__ */
55
55
  function handleSubmit(form, handler) {
56
56
  return async (event) => {
57
- event.preventDefault();
57
+ event?.preventDefault();
58
58
  const internalFormStore = form[INTERNAL];
59
59
  internalFormStore.isSubmitted.value = true;
60
60
  internalFormStore.isSubmitting.value = true;
@@ -29,6 +29,10 @@ export function useField(form, config) {
29
29
  get isValid() {
30
30
  return isValid;
31
31
  },
32
+ onInput(value) {
33
+ setFieldInput(internalFormStore, path, value);
34
+ validateIfRequired(internalFormStore, internalFieldStore, 'input');
35
+ },
32
36
  props: {
33
37
  get name() {
34
38
  return internalFieldStore.name;
@@ -62,6 +62,10 @@ export interface FieldStore<TSchema extends Schema = Schema, TFieldPath extends
62
62
  * Whether the field is valid according to the schema.
63
63
  */
64
64
  readonly isValid: boolean;
65
+ /**
66
+ * Sets the field input value programmatically.
67
+ */
68
+ readonly onInput: (value: PartialValues<PathValue<v.InferInput<TSchema>, TFieldPath>>) => void;
65
69
  /**
66
70
  * The props for the field element.
67
71
  */
@@ -1,2 +1,9 @@
1
1
  import type { MaybeGetter } from '../../types/index';
2
+ /**
3
+ * Unwraps a value that may be a getter function.
4
+ *
5
+ * @param value The value or getter function.
6
+ *
7
+ * @returns The unwrapped value.
8
+ */
2
9
  export declare function unwrap<T>(value: MaybeGetter<T>): T;
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.5.6",
4
+ "version": "0.7.0",
5
5
  "license": "MIT",
6
6
  "author": "Fabian Hiller",
7
7
  "homepage": "https://formisch.dev",
@@ -37,7 +37,6 @@
37
37
  },
38
38
  "devDependencies": {
39
39
  "@eslint/compat": "^1.3.2",
40
- "@eslint/js": "^9.35.0",
41
40
  "@sveltejs/adapter-auto": "^6.1.0",
42
41
  "@sveltejs/kit": "^2.37.1",
43
42
  "@sveltejs/package": "^2.5.0",
@@ -49,11 +48,11 @@
49
48
  "svelte": "^5.38.8",
50
49
  "svelte-check": "^4.3.1",
51
50
  "typescript": "^5.9.2",
52
- "typescript-eslint": "^8.43.0",
53
51
  "valibot": "^1.2.0",
54
52
  "vite": "^7.1.5",
55
- "@formisch/core": "0.4.5",
56
- "@formisch/methods": "0.5.2"
53
+ "@formisch/core": "0.6.0",
54
+ "@formisch/eslint-config": "0.1.0",
55
+ "@formisch/methods": "0.7.0"
57
56
  },
58
57
  "peerDependencies": {
59
58
  "svelte": "^5.29.0",