@weser/forms 0.0.13 → 0.0.14

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,5 +1,6 @@
1
1
  import { ChangeEvent } from 'react';
2
2
  import { ZodType } from 'zod';
3
+ import { $ZodIssue } from '@zod/core';
3
4
  import { Field, Options } from './types.js';
4
5
  export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onInit, _onUpdate, _storedField, }?: Options<T>): {
5
6
  valid: boolean;
@@ -51,6 +52,7 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
51
52
  valid: boolean;
52
53
  errorMessage: string | undefined;
53
54
  };
55
+ _applyError: (issue: $ZodIssue) => void;
54
56
  value: T;
55
57
  disabled: boolean;
56
58
  touched: boolean;
package/dist/useField.js CHANGED
@@ -81,6 +81,13 @@ export default function useField(schema, { name, value = '', disabled = false, t
81
81
  onFocus: untouch,
82
82
  };
83
83
  }
84
+ function _applyError(issue) {
85
+ const errorMessage = formatErrorMessage(issue, field.value, name);
86
+ setField((field) => ({
87
+ ...field,
88
+ errorMessage,
89
+ }));
90
+ }
84
91
  const inputProps = {
85
92
  value: field.value,
86
93
  disabled: field.disabled,
@@ -108,5 +115,6 @@ export default function useField(schema, { name, value = '', disabled = false, t
108
115
  inputProps,
109
116
  props,
110
117
  _initial: initialField,
118
+ _applyError,
111
119
  };
112
120
  }
package/dist/useForm.d.ts CHANGED
@@ -53,6 +53,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
53
53
  valid: boolean;
54
54
  errorMessage: string | undefined;
55
55
  };
56
+ _applyError: (issue: z.core.$ZodIssue) => void;
56
57
  value: T;
57
58
  disabled: boolean;
58
59
  touched: boolean;
package/dist/useForm.js CHANGED
@@ -37,7 +37,11 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
37
37
  }
38
38
  function touchFields() {
39
39
  for (const name in fields.current) {
40
- fields.current[name].update({ touched: true });
40
+ fields.current[name].update({
41
+ touched: true,
42
+ // force revalidate
43
+ value: fields.current[name].value,
44
+ });
41
45
  }
42
46
  }
43
47
  function reset() {
@@ -65,11 +69,22 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
65
69
  }
66
70
  else {
67
71
  if (onError) {
72
+ if (parsed.error.issues.length > 0) {
73
+ _applyErrors(parsed.error.issues);
74
+ }
68
75
  onError(parsed.error);
69
76
  }
70
77
  }
71
78
  };
72
79
  }
80
+ function _applyErrors(issues) {
81
+ for (const issue of issues) {
82
+ const field = fields.current[issue.path[0]];
83
+ if (field) {
84
+ field._applyError(issue);
85
+ }
86
+ }
87
+ }
73
88
  return {
74
89
  useFormField,
75
90
  handleSubmit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/forms",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "React hooks for forms with zod schemas",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -43,9 +43,12 @@
43
43
  "forms",
44
44
  "zod"
45
45
  ],
46
+ "dependencies": {
47
+ "@zod/core": "^0.11.6"
48
+ },
46
49
  "peerDependencies": {
47
50
  "react": ">16.3.0",
48
- "zod": ">=3"
51
+ "zod": ">=4"
49
52
  },
50
53
  "devDependencies": {
51
54
  "@types/react": "^18.3.3",
@@ -53,10 +56,7 @@
53
56
  "react": "canary",
54
57
  "rimraf": "^3.0.2",
55
58
  "typescript": "^5.4.5",
56
- "zod": "4.0.0-beta.20250505T195954"
57
- },
58
- "dependencies": {
59
- "@zod/core": "^0.11.6"
59
+ "zod": "^4.1.11"
60
60
  },
61
- "gitHead": "2e5430bd1d2f421a9ab85dff0918b89023f8039b"
61
+ "gitHead": "2844a44c54a0ada9f675baf40226f87fe894c29f"
62
62
  }