@weser/forms 0.0.6 → 0.0.8

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,2 +1,2 @@
1
- import { ZodIssue } from 'zod';
2
- export default function defaultFormatErrorMessage(error: ZodIssue): string;
1
+ import { $ZodIssue } from '@zod/core';
2
+ export default function defaultFormatErrorMessage(error: $ZodIssue): string;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ZodIssue } from 'zod';
1
+ import { $ZodIssue } from '@zod/core';
2
2
  export type Field<T> = {
3
3
  value: T;
4
4
  disabled: boolean;
@@ -14,6 +14,8 @@ export type Options<T> = {
14
14
  touched?: boolean;
15
15
  showValidationOn?: 'submit' | 'blur' | 'change';
16
16
  parseValue?: (e: any) => T;
17
- formatErrorMessage?: (error: ZodIssue, value: T, name?: string) => string;
18
- _onUpdateValue?: (value: T, dirty: boolean) => void;
17
+ formatErrorMessage?: (error: $ZodIssue, value: T, name?: string) => string;
18
+ _onInit?: (field: Field<T>) => void;
19
+ _onUpdate?: (field: Partial<Field<T>>) => void;
20
+ _storedField?: Field<T>;
19
21
  };
@@ -1,18 +1,17 @@
1
1
  import { ChangeEvent } from 'react';
2
2
  import { ZodType } from 'zod';
3
3
  import { Field, Options } from './types.js';
4
- export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onUpdateValue, }?: Options<T>): {
5
- required: boolean;
4
+ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn, parseValue, formatErrorMessage, _onInit, _onUpdate, _storedField, }?: Options<T>): {
6
5
  valid: boolean;
7
6
  update: (data: Partial<Field<T>>) => void;
8
7
  reset: () => void;
8
+ validate: () => import("zod").ZodSafeParseResult<unknown>;
9
9
  errorMessage: string | undefined;
10
10
  inputProps: {
11
11
  onFocus: () => void;
12
12
  onBlur: () => void;
13
13
  value: T;
14
14
  disabled: boolean;
15
- required: boolean;
16
15
  name: string | undefined;
17
16
  'data-valid': boolean;
18
17
  onChange: (e: C) => void;
@@ -21,19 +20,10 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
21
20
  onBlur?: undefined;
22
21
  value: T;
23
22
  disabled: boolean;
24
- required: boolean;
25
23
  name: string | undefined;
26
24
  'data-valid': boolean;
27
25
  onChange: (e: C) => void;
28
26
  };
29
- initial: {
30
- value: T;
31
- disabled: boolean;
32
- touched: boolean;
33
- dirty: boolean;
34
- valid: boolean;
35
- errorMessage: string | undefined;
36
- };
37
27
  props: {
38
28
  onFocus: () => void;
39
29
  onBlur: () => void;
@@ -41,7 +31,6 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
41
31
  disabled: boolean;
42
32
  name: string | undefined;
43
33
  valid: boolean;
44
- required: boolean;
45
34
  errorMessage: string | undefined;
46
35
  onChange: (e: C) => void;
47
36
  } | {
@@ -51,10 +40,17 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
51
40
  disabled: boolean;
52
41
  name: string | undefined;
53
42
  valid: boolean;
54
- required: boolean;
55
43
  errorMessage: string | undefined;
56
44
  onChange: (e: C) => void;
57
45
  };
46
+ _initial: {
47
+ value: T;
48
+ disabled: boolean;
49
+ touched: boolean;
50
+ dirty: boolean;
51
+ valid: boolean;
52
+ errorMessage: string | undefined;
53
+ };
58
54
  value: T;
59
55
  disabled: boolean;
60
56
  touched: boolean;
package/dist/useField.js CHANGED
@@ -1,15 +1,14 @@
1
- import { useState } from 'react';
1
+ import { useEffect, useState } from 'react';
2
2
  import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
3
3
  import defaultParseValue from './defaultParseValue.js';
4
- export default function useField(schema, { name, value = '', disabled = false, touched = false, showValidationOn = 'submit', parseValue = (defaultParseValue), formatErrorMessage = defaultFormatErrorMessage, _onUpdateValue, } = {}) {
5
- const isOptional = schema.isOptional();
6
- function validate(value) {
4
+ export default function useField(schema, { name, value = '', disabled = false, touched = false, showValidationOn = 'submit', parseValue = (defaultParseValue), formatErrorMessage = defaultFormatErrorMessage, _onInit, _onUpdate, _storedField, } = {}) {
5
+ function _validate(value) {
7
6
  const { success, error } = schema.safeParse(value);
8
7
  if (!success) {
9
8
  return formatErrorMessage(error.issues[0], value, name);
10
9
  }
11
10
  }
12
- const message = validate(value);
11
+ const message = _validate(value);
13
12
  const initialField = {
14
13
  value,
15
14
  disabled,
@@ -18,24 +17,35 @@ export default function useField(schema, { name, value = '', disabled = false, t
18
17
  valid: !message,
19
18
  errorMessage: message,
20
19
  };
21
- const [field, setField] = useState(initialField);
20
+ const [field, setField] = useState(_storedField ?? initialField);
21
+ useEffect(() => {
22
+ if (_onInit && !_storedField) {
23
+ _onInit(field);
24
+ }
25
+ }, [_onInit, _storedField]);
22
26
  function update(data) {
23
27
  if (data.value !== undefined) {
24
28
  const dirty = data.value !== initialField.value;
25
- const errorMessage = validate(data.value);
26
- if (_onUpdateValue) {
27
- _onUpdateValue(data.value, dirty);
28
- }
29
- setField((field) => ({
30
- ...field,
29
+ const errorMessage = _validate(data.value);
30
+ const _data = {
31
31
  touched: showValidationOn === 'change' ? dirty : field.touched,
32
32
  dirty,
33
33
  ...data,
34
34
  errorMessage,
35
35
  valid: !errorMessage,
36
+ };
37
+ if (_onUpdate) {
38
+ _onUpdate(_data);
39
+ }
40
+ setField((field) => ({
41
+ ...field,
42
+ ..._data,
36
43
  }));
37
44
  }
38
45
  else {
46
+ if (_onUpdate) {
47
+ _onUpdate(data);
48
+ }
39
49
  setField((field) => ({
40
50
  ...field,
41
51
  ...data,
@@ -43,33 +53,37 @@ export default function useField(schema, { name, value = '', disabled = false, t
43
53
  }
44
54
  }
45
55
  function reset() {
56
+ if (_onUpdate) {
57
+ _onUpdate(initialField);
58
+ }
46
59
  setField(initialField);
47
60
  }
61
+ function validate() {
62
+ return schema.safeParse(field.value);
63
+ }
48
64
  function onChange(e) {
49
65
  update({ value: parseValue(e) });
50
66
  }
51
- const required = !isOptional;
52
67
  // Only show validation error when is touched
53
68
  const valid = !field.touched ? true : !field.errorMessage;
54
69
  // Only show errrorMessage and validation styles if the field is touched according to the config
55
70
  const errorMessage = field.touched ? field.errorMessage : undefined;
56
- const touch = () => update({ touched: false });
71
+ const touch = () => update({ touched: true });
57
72
  const untouch = () => update({ touched: false });
58
73
  function getListeners() {
59
74
  if (showValidationOn === 'blur') {
60
75
  return {
61
- onFocus: touch,
62
- onBlur: untouch,
76
+ onFocus: untouch,
77
+ onBlur: touch,
63
78
  };
64
79
  }
65
80
  return {
66
- onFocus: touch,
81
+ onFocus: untouch,
67
82
  };
68
83
  }
69
84
  const inputProps = {
70
85
  value: field.value,
71
86
  disabled: field.disabled,
72
- required,
73
87
  name,
74
88
  'data-valid': valid,
75
89
  onChange,
@@ -80,20 +94,19 @@ export default function useField(schema, { name, value = '', disabled = false, t
80
94
  disabled: field.disabled,
81
95
  name,
82
96
  valid,
83
- required,
84
97
  errorMessage,
85
98
  onChange,
86
99
  ...getListeners(),
87
100
  };
88
101
  return {
89
102
  ...field,
90
- required,
91
103
  valid,
92
104
  update,
93
105
  reset,
106
+ validate,
94
107
  errorMessage,
95
108
  inputProps,
96
- initial: initialField,
97
109
  props,
110
+ _initial: initialField,
98
111
  };
99
112
  }
@@ -0,0 +1 @@
1
+ export default function useFieldArray(): void;
@@ -0,0 +1,2 @@
1
+ export default function useFieldArray() {
2
+ }
package/dist/useForm.d.ts CHANGED
@@ -1,39 +1,30 @@
1
1
  import { FormEvent, ChangeEvent } from 'react';
2
- import { z, ZodObject, ZodError, ZodRawShape, ZodIssue } from 'zod';
3
- import { Field, Options } from './types.js';
4
- export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, formatErrorMessage?: (error: ZodIssue, value: any, name: string) => string): {
5
- useFormField: <T = string>(name: keyof S, options?: Omit<Options<T>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
6
- reset: () => void;
7
- required: boolean;
2
+ import { z, ZodObject, ZodError, ZodRawShape } from 'zod';
3
+ import { $ZodIssue } from '@zod/core';
4
+ import { Options } from './types.js';
5
+ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, formatErrorMessage?: (error: $ZodIssue, value: any, name?: string) => string): {
6
+ useFormField: <T = string, C = ChangeEvent<HTMLInputElement>>(_name: keyof S, options?: Omit<Options<T>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
8
7
  valid: boolean;
9
- update: (data: Partial<Field<T>>) => void;
8
+ update: (data: Partial<import("./types.js").Field<T>>) => void;
9
+ reset: () => void;
10
+ validate: () => z.ZodSafeParseResult<unknown>;
10
11
  errorMessage: string | undefined;
11
12
  inputProps: {
12
13
  onFocus: () => void;
13
14
  onBlur: () => void;
14
15
  value: T;
15
16
  disabled: boolean;
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;
23
23
  value: T;
24
24
  disabled: boolean;
25
- required: boolean;
26
25
  name: string | undefined;
27
26
  'data-valid': boolean;
28
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
29
- };
30
- initial: {
31
- value: T;
32
- disabled: boolean;
33
- touched: boolean;
34
- dirty: boolean;
35
- valid: boolean;
36
- errorMessage: string | undefined;
27
+ onChange: (e: C) => void;
37
28
  };
38
29
  props: {
39
30
  onFocus: () => void;
@@ -42,9 +33,8 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
42
33
  disabled: boolean;
43
34
  name: string | undefined;
44
35
  valid: boolean;
45
- required: boolean;
46
36
  errorMessage: string | undefined;
47
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
37
+ onChange: (e: C) => void;
48
38
  } | {
49
39
  onFocus: () => void;
50
40
  onBlur?: undefined;
@@ -52,9 +42,16 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
52
42
  disabled: boolean;
53
43
  name: string | undefined;
54
44
  valid: boolean;
55
- required: boolean;
56
45
  errorMessage: string | undefined;
57
- onChange: (e: ChangeEvent<HTMLInputElement>) => void;
46
+ onChange: (e: C) => void;
47
+ };
48
+ _initial: {
49
+ value: T;
50
+ disabled: boolean;
51
+ touched: boolean;
52
+ dirty: boolean;
53
+ valid: boolean;
54
+ errorMessage: string | undefined;
58
55
  };
59
56
  value: T;
60
57
  disabled: boolean;
@@ -62,9 +59,6 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
62
59
  dirty: boolean;
63
60
  };
64
61
  handleSubmit: (onSubmit: (data: z.infer<typeof schema>) => void, onError?: (error: ZodError) => void) => (e: FormEvent<HTMLFormElement>) => void;
65
- formProps: {
66
- noValidate: boolean;
67
- };
68
- isDirty: () => boolean;
62
+ checkDirty: () => boolean;
69
63
  reset: () => void;
70
64
  };
package/dist/useForm.js CHANGED
@@ -1,67 +1,53 @@
1
- import { useEffect, useRef, useState } from 'react';
1
+ import { useRef } from 'react';
2
2
  import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
3
3
  import useField from './useField.js';
4
4
  function mapFieldsToData(fields) {
5
5
  const obj = {};
6
6
  for (const name in fields) {
7
- obj[name] = fields[name].ref.current.value;
7
+ obj[name] = fields[name].value;
8
8
  }
9
9
  return obj;
10
10
  }
11
- // TODO: accept refined schemas, not possible due to https://github.com/colinhacks/zod/issues/2474
12
11
  export default function useForm(schema, formatErrorMessage = defaultFormatErrorMessage) {
13
- const [fields, setFields] = useState({});
14
- function useFormField(name, options = {}) {
15
- const shape = schema.shape[name];
16
- // @ts-ignore
12
+ const fields = useRef({});
13
+ function useFormField(_name, options = {}) {
14
+ const name = String(_name);
15
+ const shape = schema.shape[_name];
16
+ const stored = fields.current[name];
17
17
  const field = useField(shape, {
18
18
  ...options,
19
- name: name,
19
+ name,
20
20
  formatErrorMessage,
21
- _onUpdateValue: (value, dirty) => {
22
- ref.current = {
23
- value,
24
- dirty,
21
+ // internals
22
+ _storedField: stored,
23
+ _onInit: (data) => {
24
+ fields.current[name] = {
25
+ ...field,
26
+ ...data,
25
27
  };
26
28
  },
27
- });
28
- const ref = useRef({
29
- value: field.value,
30
- dirty: false,
31
- });
32
- function reset() {
33
- ref.current = {
34
- value: field.initial.value,
35
- dirty: false,
36
- };
37
- field.reset();
38
- }
39
- useEffect(() => setFields((fields) => ({
40
- ...fields,
41
- [name]: {
42
- ref,
43
- update: field.update,
44
- reset,
29
+ _onUpdate: (data) => {
30
+ fields.current[name] = {
31
+ ...fields.current[name],
32
+ ...data,
33
+ };
45
34
  },
46
- })), []);
47
- return {
48
- ...field,
49
- reset,
50
- };
35
+ });
36
+ return field;
51
37
  }
52
38
  function touchFields() {
53
- for (const name in fields) {
54
- fields[name].update({ touched: true });
39
+ for (const name in fields.current) {
40
+ fields.current[name].update({ touched: true });
55
41
  }
56
42
  }
57
43
  function reset() {
58
- for (const name in fields) {
59
- fields[name].reset();
44
+ for (const name in fields.current) {
45
+ fields.current[name].reset();
60
46
  }
61
47
  }
62
- function isDirty() {
63
- for (const name in fields) {
64
- if (fields[name].ref.current.dirty) {
48
+ function checkDirty() {
49
+ for (const name in fields.current) {
50
+ if (fields.current[name].dirty) {
65
51
  return true;
66
52
  }
67
53
  }
@@ -69,9 +55,10 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
69
55
  }
70
56
  function handleSubmit(onSubmit, onError) {
71
57
  return (e) => {
58
+ e.stopPropagation();
72
59
  e.preventDefault();
73
60
  touchFields();
74
- const data = mapFieldsToData(fields);
61
+ const data = mapFieldsToData(fields.current);
75
62
  const parsed = schema.safeParse(data);
76
63
  if (parsed.success) {
77
64
  onSubmit(parsed.data);
@@ -83,14 +70,10 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
83
70
  }
84
71
  };
85
72
  }
86
- const formProps = {
87
- noValidate: true,
88
- };
89
73
  return {
90
74
  useFormField,
91
75
  handleSubmit,
92
- formProps,
93
- isDirty,
76
+ checkDirty,
94
77
  reset,
95
78
  };
96
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/forms",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "React hooks for forms with zod schemas",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -55,5 +55,8 @@
55
55
  "typescript": "^5.4.5",
56
56
  "zod": "4.0.0-beta.20250505T195954"
57
57
  },
58
- "gitHead": "44a3bbb88cf4c563fd606cab6c65ca99d82c00a3"
58
+ "dependencies": {
59
+ "@zod/core": "^0.11.6"
60
+ },
61
+ "gitHead": "9dce5f658f08431e80506b022369229dfeff7dd7"
59
62
  }