@weser/forms 0.0.7 → 0.0.9
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/defaultFormatErrorMessage.d.ts +2 -2
- package/dist/types.d.ts +5 -3
- package/dist/useField.d.ts +10 -14
- package/dist/useField.js +34 -21
- package/dist/useFieldArray.d.ts +1 -0
- package/dist/useFieldArray.js +2 -0
- package/dist/useForm.d.ts +17 -23
- package/dist/useForm.js +30 -48
- package/package.json +5 -2
|
@@ -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
|
-
|
|
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
|
};
|
package/dist/useField.d.ts
CHANGED
|
@@ -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,
|
|
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,
|
|
5
|
-
|
|
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 =
|
|
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 =
|
|
26
|
-
|
|
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:
|
|
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:
|
|
62
|
-
onBlur:
|
|
76
|
+
onFocus: untouch,
|
|
77
|
+
onBlur: touch,
|
|
63
78
|
};
|
|
64
79
|
}
|
|
65
80
|
return {
|
|
66
|
-
onFocus:
|
|
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;
|
package/dist/useForm.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { FormEvent, ChangeEvent } from 'react';
|
|
2
|
-
import { z, ZodObject, ZodError, ZodRawShape
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
19
|
onChange: (e: C) => void;
|
|
@@ -22,19 +22,10 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
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
27
|
onChange: (e: C) => void;
|
|
29
28
|
};
|
|
30
|
-
initial: {
|
|
31
|
-
value: T;
|
|
32
|
-
disabled: boolean;
|
|
33
|
-
touched: boolean;
|
|
34
|
-
dirty: boolean;
|
|
35
|
-
valid: boolean;
|
|
36
|
-
errorMessage: string | undefined;
|
|
37
|
-
};
|
|
38
29
|
props: {
|
|
39
30
|
onFocus: () => void;
|
|
40
31
|
onBlur: () => void;
|
|
@@ -42,7 +33,6 @@ 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
37
|
onChange: (e: C) => void;
|
|
48
38
|
} | {
|
|
@@ -52,19 +42,23 @@ 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
46
|
onChange: (e: C) => void;
|
|
58
47
|
};
|
|
48
|
+
_initial: {
|
|
49
|
+
value: T;
|
|
50
|
+
disabled: boolean;
|
|
51
|
+
touched: boolean;
|
|
52
|
+
dirty: boolean;
|
|
53
|
+
valid: boolean;
|
|
54
|
+
errorMessage: string | undefined;
|
|
55
|
+
};
|
|
59
56
|
value: T;
|
|
60
57
|
disabled: boolean;
|
|
61
58
|
touched: boolean;
|
|
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
|
-
|
|
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 {
|
|
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].
|
|
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
|
|
14
|
-
function useFormField(
|
|
15
|
-
const
|
|
16
|
-
|
|
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
|
|
19
|
+
name,
|
|
20
20
|
formatErrorMessage,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
// internals
|
|
22
|
+
_storedField: stored,
|
|
23
|
+
_onInit: (data) => {
|
|
24
|
+
fields.current[name] = {
|
|
25
|
+
...field,
|
|
26
|
+
...data,
|
|
25
27
|
};
|
|
26
28
|
},
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
63
|
-
for (const name in fields) {
|
|
64
|
-
if (fields[name].
|
|
48
|
+
function checkDirty() {
|
|
49
|
+
for (const name in fields.current) {
|
|
50
|
+
if (fields.current[name].dirty) {
|
|
65
51
|
return true;
|
|
66
52
|
}
|
|
67
53
|
}
|
|
@@ -72,7 +58,7 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
|
|
|
72
58
|
e.stopPropagation();
|
|
73
59
|
e.preventDefault();
|
|
74
60
|
touchFields();
|
|
75
|
-
const data = mapFieldsToData(fields);
|
|
61
|
+
const data = mapFieldsToData(fields.current);
|
|
76
62
|
const parsed = schema.safeParse(data);
|
|
77
63
|
if (parsed.success) {
|
|
78
64
|
onSubmit(parsed.data);
|
|
@@ -84,14 +70,10 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
|
|
|
84
70
|
}
|
|
85
71
|
};
|
|
86
72
|
}
|
|
87
|
-
const formProps = {
|
|
88
|
-
noValidate: true,
|
|
89
|
-
};
|
|
90
73
|
return {
|
|
91
74
|
useFormField,
|
|
92
75
|
handleSubmit,
|
|
93
|
-
|
|
94
|
-
isDirty,
|
|
76
|
+
checkDirty,
|
|
95
77
|
reset,
|
|
96
78
|
};
|
|
97
79
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
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
|
-
"
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@zod/core": "^0.11.6"
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "abbfb42cee4e8bdbbd870bd1086ebc65b6074584"
|
|
59
62
|
}
|