@weser/forms 0.0.5 → 0.0.7

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.
@@ -0,0 +1 @@
1
+ export default function defaultIsEmpty<T>(value: T): boolean | string;
@@ -0,0 +1,6 @@
1
+ export default function defaultIsEmpty(value) {
2
+ if (typeof value === 'string') {
3
+ return value.length === 0;
4
+ }
5
+ return false;
6
+ }
package/dist/types.d.ts CHANGED
@@ -14,6 +14,6 @@ export type Options<T> = {
14
14
  touched?: boolean;
15
15
  showValidationOn?: 'submit' | 'blur' | 'change';
16
16
  parseValue?: (e: any) => T;
17
- formatErrorMessage?: (error: ZodIssue, name?: string) => string;
17
+ formatErrorMessage?: (error: ZodIssue, value: T, name?: string) => string;
18
18
  _onUpdateValue?: (value: T, dirty: boolean) => void;
19
19
  };
@@ -1,7 +1,7 @@
1
1
  import { ChangeEvent } from 'react';
2
- import { ZodSchema } from 'zod';
2
+ import { ZodType } from 'zod';
3
3
  import { Field, Options } from './types.js';
4
- export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodSchema, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onUpdateValue, }?: Options<T>): {
4
+ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onUpdateValue, }?: Options<T>): {
5
5
  required: boolean;
6
6
  valid: boolean;
7
7
  update: (data: Partial<Field<T>>) => void;
package/dist/useField.js CHANGED
@@ -4,12 +4,9 @@ import defaultParseValue from './defaultParseValue.js';
4
4
  export default function useField(schema, { name, value = '', disabled = false, touched = false, showValidationOn = 'submit', parseValue = (defaultParseValue), formatErrorMessage = defaultFormatErrorMessage, _onUpdateValue, } = {}) {
5
5
  const isOptional = schema.isOptional();
6
6
  function validate(value) {
7
- const res = schema.safeParse(value);
8
- if (res.success) {
9
- return;
10
- }
11
- else {
12
- return formatErrorMessage(res.error.errors[0], name);
7
+ const { success, error } = schema.safeParse(value);
8
+ if (!success) {
9
+ return formatErrorMessage(error.issues[0], value, name);
13
10
  }
14
11
  }
15
12
  const message = validate(value);
package/dist/useForm.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { FormEvent, ChangeEvent } from 'react';
2
2
  import { z, ZodObject, ZodError, ZodRawShape, ZodIssue } from 'zod';
3
3
  import { Field, Options } from './types.js';
4
- export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, formatErrorMessage?: (error: ZodIssue, name?: string) => string): {
5
- useFormField: <T = string>(name: keyof S, options?: Omit<Options<T>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
4
+ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, formatErrorMessage?: (error: ZodIssue, value: any, name: string) => string): {
5
+ useFormField: <T = string, C = ChangeEvent<HTMLInputElement>>(name: keyof S, options?: Omit<Options<T>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
6
6
  reset: () => void;
7
7
  required: boolean;
8
8
  valid: boolean;
@@ -16,7 +16,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
16
16
  required: boolean;
17
17
  name: string | undefined;
18
18
  'data-valid': boolean;
19
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
19
+ onChange: (e: C) => void;
20
20
  } | {
21
21
  onFocus: () => void;
22
22
  onBlur?: undefined;
@@ -25,7 +25,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
25
25
  required: boolean;
26
26
  name: string | undefined;
27
27
  'data-valid': boolean;
28
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
28
+ onChange: (e: C) => void;
29
29
  };
30
30
  initial: {
31
31
  value: T;
@@ -44,7 +44,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
44
44
  valid: boolean;
45
45
  required: boolean;
46
46
  errorMessage: string | undefined;
47
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
47
+ onChange: (e: C) => void;
48
48
  } | {
49
49
  onFocus: () => void;
50
50
  onBlur?: undefined;
@@ -54,7 +54,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
54
54
  valid: boolean;
55
55
  required: boolean;
56
56
  errorMessage: string | undefined;
57
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
57
+ onChange: (e: C) => void;
58
58
  };
59
59
  value: T;
60
60
  disabled: boolean;
package/dist/useForm.js CHANGED
@@ -69,6 +69,7 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
69
69
  }
70
70
  function handleSubmit(onSubmit, onError) {
71
71
  return (e) => {
72
+ e.stopPropagation();
72
73
  e.preventDefault();
73
74
  touchFields();
74
75
  const data = mapFieldsToData(fields);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/forms",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "React hooks for forms with zod schemas",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -52,7 +52,8 @@
52
52
  "ava": "^6.1.3",
53
53
  "react": "canary",
54
54
  "rimraf": "^3.0.2",
55
- "typescript": "^5.4.5"
55
+ "typescript": "^5.4.5",
56
+ "zod": "4.0.0-beta.20250505T195954"
56
57
  },
57
- "gitHead": "cafc1b7a26fbd5dbc1341ecd1bd758fdeda37f2f"
58
+ "gitHead": "5f42d8868acf40e4d8c417883d6b23b4cfe8ca90"
58
59
  }