@formisch/react 0.2.2 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as v from "valibot";
2
- import { ChangeEventHandler, FocusEventHandler, FormHTMLAttributes, ReactElement } from "react";
2
+ import { ChangeEventHandler, FocusEventHandler, FormEvent, FormHTMLAttributes, ReactElement } from "react";
3
3
 
4
- //#region ../../packages/core/dist/index.vanilla.d.ts
4
+ //#region ../../packages/core/dist/index.react.d.ts
5
5
 
6
6
  //#region src/types/schema.d.ts
7
7
  /**
@@ -184,12 +184,6 @@ interface InternalValueStore extends InternalBaseStore {
184
184
  */
185
185
  type InternalFieldStore = InternalArrayStore | InternalObjectStore | InternalValueStore;
186
186
  //#endregion
187
- //#region src/values.d.ts
188
- /**
189
- * Internal symbol constant.
190
- */
191
- declare const INTERNAL: "~internal";
192
- //#endregion
193
187
  //#region src/types/utils.d.ts
194
188
  /**
195
189
  * Checks if a type is `any`.
@@ -212,6 +206,12 @@ type DeepPartial<TValue> = TValue extends readonly unknown[] ? number extends TV
212
206
  */
213
207
  type PartialValues<TValue> = TValue extends readonly unknown[] ? number extends TValue["length"] ? TValue : { [Key in keyof TValue]: PartialValues<TValue[Key]> } : TValue extends Record<PropertyKey, unknown> ? { [Key in keyof TValue]: PartialValues<TValue[Key]> } : TValue | undefined;
214
208
  //#endregion
209
+ //#region src/values.d.ts
210
+ /**
211
+ * Internal symbol constant.
212
+ */
213
+ declare const INTERNAL: "~internal";
214
+ //#endregion
215
215
  //#region src/types/form.d.ts
216
216
  /**
217
217
  * Validation mode type.
@@ -286,10 +286,12 @@ interface BaseFormStore<TSchema extends Schema = Schema> {
286
286
  */
287
287
  readonly [INTERNAL]: InternalFormStore<TSchema>;
288
288
  }
289
+ //#endregion
290
+ //#region src/types/form.react.d.ts
289
291
  /**
290
- * Submit handler type.
292
+ * Submit handler type for React.
291
293
  */
292
- type SubmitHandler<TSchema extends Schema> = (output: v.InferOutput<TSchema>, event: SubmitEvent) => MaybePromise<unknown>;
294
+ type SubmitHandler<TSchema extends Schema> = (output: v.InferOutput<TSchema>, event: FormEvent<HTMLFormElement>) => MaybePromise<unknown>;
293
295
  //#endregion
294
296
  //#region src/types/path.d.ts
295
297
  /**
@@ -358,9 +360,8 @@ type ValidArrayPath<TValue, TPath extends RequiredPath> = TPath extends LazyArra
358
360
  * @param toInternalFieldStore The destination field store to copy to.
359
361
  */
360
362
  //#endregion
361
- //#region ../../packages/methods/dist/index.vanilla.d.ts
363
+ //#region ../../packages/methods/dist/index.react.d.ts
362
364
  //#region src/focus/focus.d.ts
363
-
364
365
  /**
365
366
  * Focus field config interface.
366
367
  */
@@ -472,7 +473,7 @@ declare function getInput<TSchema extends Schema>(form: BaseFormStore<TSchema>):
472
473
  */
473
474
  declare function getInput<TSchema extends Schema, TFieldPath extends RequiredPath | undefined = undefined>(form: BaseFormStore<TSchema>, config: TFieldPath extends RequiredPath ? GetFieldInputConfig<TSchema, TFieldPath> : GetFormInputConfig): PartialValues<TFieldPath extends RequiredPath ? PathValue<v.InferInput<TSchema>, TFieldPath> : v.InferInput<TSchema>>;
474
475
  //#endregion
475
- //#region src/handleSubmit/handleSubmit.d.ts
476
+ //#region src/handleSubmit/handleSubmit.react.d.ts
476
477
  /**
477
478
  * Creates a submit event handler for the form that prevents default browser
478
479
  * submission, validates the form input, and calls the provided handler if
@@ -483,7 +484,7 @@ declare function getInput<TSchema extends Schema, TFieldPath extends RequiredPat
483
484
  *
484
485
  * @returns A submit event handler function to attach to the form element.
485
486
  */
486
- declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitHandler<TSchema>): (event: SubmitEvent) => Promise<void>;
487
+ declare function handleSubmit<TSchema extends Schema>(form: BaseFormStore<TSchema>, handler: SubmitHandler<TSchema>): (event: FormEvent<HTMLFormElement>) => Promise<void>;
487
488
  //#endregion
488
489
  //#region src/insert/insert.d.ts
489
490
  /**
@@ -720,10 +721,12 @@ interface SetFieldInputConfig<TSchema extends Schema, TFieldPath extends Require
720
721
  */
721
722
  readonly input: PathValue<v.InferInput<TSchema>, TFieldPath>;
722
723
  }
724
+ //#endregion
725
+ //#region src/setInput/setInput.react.d.ts
723
726
  /**
724
727
  * Sets the input value of a specific field or the entire form. This updates
725
728
  * the field value(s) and triggers validation if required by the form's
726
- * validation mode.
729
+ * validation mode for both input and change events.
727
730
  *
728
731
  * @param form The form store to set input on.
729
732
  * @param config The set form input configuration specifying the new input values.
@@ -732,7 +735,7 @@ declare function setInput<TSchema extends Schema>(form: BaseFormStore<TSchema>,
732
735
  /**
733
736
  * Sets the input value of a specific field or the entire form. This updates
734
737
  * the field value(s) and triggers validation if required by the form's
735
- * validation mode.
738
+ * validation mode for both input and change events.
736
739
  *
737
740
  * @param form The form store to set input on.
738
741
  * @param config The set input configuration specifying the path and new value.
@@ -856,6 +859,10 @@ interface FieldStore<TSchema extends Schema = Schema, TFieldPath extends Require
856
859
  */
857
860
  readonly isValid: boolean;
858
861
  /**
862
+ * Sets the field input value programmatically.
863
+ */
864
+ readonly onChange: (value: PartialValues<PathValue<v.InferInput<TSchema>, TFieldPath>>) => void;
865
+ /**
859
866
  * The props to spread onto the field element for integration.
860
867
  */
861
868
  readonly props: FieldElementProps;
@@ -1070,4 +1077,4 @@ declare function useFieldArray<TSchema extends Schema, TFieldArrayPath extends R
1070
1077
  */
1071
1078
  declare function useForm<TSchema extends Schema>(config: FormConfig<TSchema>): FormStore<TSchema>;
1072
1079
  //#endregion
1073
- export { type DeepPartial, Field, FieldArray, FieldArrayProps, FieldArrayStore, type FieldElement, FieldElementProps, FieldProps, FieldStore, FocusFieldConfig, Form, type FormConfig, FormProps, FormStore, GetFieldErrorsConfig, GetFieldInputConfig, GetFormErrorsConfig, GetFormInputConfig, InsertConfig, MoveConfig, type PartialValues, type PathValue, RemoveConfig, ReplaceConfig, type RequiredPath, ResetFieldConfig, ResetFormConfig, type Schema, SetFieldErrorsConfig, SetFieldInputConfig, SetFormErrorsConfig, SetFormInputConfig, type SubmitHandler, SwapConfig, UseFieldArrayConfig, UseFieldConfig, type ValidArrayPath, type ValidPath, ValidateFormConfig, type ValidationMode, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, useForm, validate };
1080
+ export { type DeepPartial, Field, FieldArray, FieldArrayProps, FieldArrayStore, type FieldElement, FieldElementProps, FieldProps, FieldStore, FocusFieldConfig, Form, type FormConfig, FormProps, FormStore, GetFieldErrorsConfig, GetFieldInputConfig, GetFormErrorsConfig, GetFormInputConfig, InsertConfig, MoveConfig, type PartialValues, type PathValue, RemoveConfig, ReplaceConfig, type RequiredPath, ResetFieldConfig, ResetFormConfig, type Schema, SetFieldErrorsConfig, type SetFieldInputConfig, SetFormErrorsConfig, type SetFormInputConfig, type SubmitHandler, SwapConfig, UseFieldArrayConfig, UseFieldConfig, type ValidArrayPath, type ValidPath, ValidateFormConfig, type ValidationMode, focus, getAllErrors, getErrors, getInput, handleSubmit, insert, move, remove, replace, reset, setErrors, setInput, submit, swap, useField, useFieldArray, useForm, validate };
package/dist/index.js CHANGED
@@ -2,11 +2,11 @@ import * as v from "valibot";
2
2
  import { useCallback, useEffect, useLayoutEffect, useMemo, useReducer, useRef } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
 
5
- //#region ../../packages/core/dist/index.vanilla.js
5
+ //#region ../../packages/core/dist/index.react.js
6
6
  /**
7
7
  * The current framework being used.
8
8
  */
9
- const framework = "vanilla";
9
+ const framework = "react";
10
10
  /**
11
11
  * Creates a unique identifier string.
12
12
  *
@@ -141,7 +141,7 @@ function initializeFieldStore(internalFieldStore, schema, initialInput, path, nu
141
141
  initializeFieldStore(internalFieldStore.children[index], schema.item, initialInput[index], path);
142
142
  path.pop();
143
143
  }
144
- } else for (let index = 0; index < schema.items; index++) {
144
+ } else for (let index = 0; index < schema.items.length; index++) {
145
145
  internalFieldStore.children[index] = {};
146
146
  path.push(index);
147
147
  initializeFieldStore(internalFieldStore.children[index], schema.items[index], initialInput?.[index], path);
@@ -454,7 +454,7 @@ function setNestedInput(internalFieldStore, input) {
454
454
  }
455
455
  for (let index = 0; index < arrayInput.length; index++) setNestedInput(internalFieldStore.children[index], arrayInput[index]);
456
456
  internalFieldStore.input.value = input == null ? input : true;
457
- internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== items.length;
457
+ internalFieldStore.isDirty.value = internalFieldStore.startInput.value !== internalFieldStore.input.value || internalFieldStore.startItems.value.length !== internalFieldStore.items.value.length;
458
458
  } else if (internalFieldStore.kind === "object") {
459
459
  for (const key in internalFieldStore.children) setNestedInput(internalFieldStore.children[key], input?.[key]);
460
460
  internalFieldStore.input.value = input == null ? input : true;
@@ -619,7 +619,7 @@ function validateIfRequired(internalFormStore, internalFieldStore, validationMod
619
619
  const INTERNAL = "~internal";
620
620
 
621
621
  //#endregion
622
- //#region ../../packages/methods/dist/index.vanilla.js
622
+ //#region ../../packages/methods/dist/index.react.js
623
623
  /**
624
624
  * Focuses the first input element of a field. This is useful for
625
625
  * programmatically setting focus to a specific field, such as after
@@ -659,7 +659,7 @@ function getInput(form, config) {
659
659
  return getFieldInput(config?.path ? getFieldStore(form[INTERNAL], config.path) : form[INTERNAL]);
660
660
  }
661
661
  /* @__NO_SIDE_EFFECTS__ */
662
- function handleSubmit(form, handler) {
662
+ function handleSubmit$1(form, handler) {
663
663
  return async (event) => {
664
664
  event.preventDefault();
665
665
  const internalFormStore = form[INTERNAL];
@@ -675,6 +675,10 @@ function handleSubmit(form, handler) {
675
675
  }
676
676
  };
677
677
  }
678
+ /* @__NO_SIDE_EFFECTS__ */
679
+ function handleSubmit(form, handler) {
680
+ return /* @__PURE__ */ handleSubmit$1(form, handler);
681
+ }
678
682
  /**
679
683
  * Inserts a new item into a field array at the specified index. All items at
680
684
  * or after the insertion point are shifted up by one index.
@@ -814,7 +818,9 @@ function setInput(form, config) {
814
818
  batch(() => {
815
819
  const internalFormStore = form[INTERNAL];
816
820
  setFieldInput(internalFormStore, config.path ?? [], config.input);
817
- validateIfRequired(internalFormStore, config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore, "input");
821
+ const fieldOrFormStore = config.path ? getFieldStore(internalFormStore, config.path) : internalFormStore;
822
+ validateIfRequired(internalFormStore, fieldOrFormStore, "input");
823
+ validateIfRequired(internalFormStore, fieldOrFormStore, "change");
818
824
  });
819
825
  }
820
826
  /**
@@ -917,6 +923,11 @@ function useField(form, config) {
917
923
  get isValid() {
918
924
  return !getFieldBool(internalFieldStore, "errors");
919
925
  },
926
+ onChange(value) {
927
+ setFieldInput(internalFormStore, config.path, value);
928
+ validateIfRequired(internalFormStore, internalFieldStore, "input");
929
+ validateIfRequired(internalFormStore, internalFieldStore, "change");
930
+ },
920
931
  props: {
921
932
  name: internalFieldStore.name,
922
933
  autoFocus: !!internalFieldStore.errors.value,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formisch/react",
3
3
  "description": "The modular and type-safe form library for React",
4
- "version": "0.2.2",
4
+ "version": "0.3.0",
5
5
  "license": "MIT",
6
6
  "author": "Fabian Hiller",
7
7
  "homepage": "https://formisch.dev",
@@ -33,7 +33,6 @@
33
33
  "access": "public"
34
34
  },
35
35
  "devDependencies": {
36
- "@eslint/js": "^9.39.1",
37
36
  "@types/node": "^24.10.1",
38
37
  "@types/react": "^19.2.5",
39
38
  "@types/react-dom": "^19.2.3",
@@ -46,10 +45,10 @@
46
45
  "react-dom": "^19.2.1",
47
46
  "tsdown": "^0.16.8",
48
47
  "typescript": "~5.9.3",
49
- "typescript-eslint": "^8.46.4",
50
48
  "vite": "^7.2.4",
51
- "@formisch/core": "0.4.5",
52
- "@formisch/methods": "0.5.2"
49
+ "@formisch/core": "0.5.0",
50
+ "@formisch/eslint-config": "0.1.0",
51
+ "@formisch/methods": "0.6.0"
53
52
  },
54
53
  "peerDependencies": {
55
54
  "react": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",