@weser/forms 0.0.17 → 0.0.18
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 +1 -1
- package/dist/types.d.ts +3 -2
- package/dist/useField.d.ts +5 -5
- package/dist/useField.js +7 -5
- package/dist/useForm.d.ts +13 -6
- package/dist/useForm.js +11 -8
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -7,13 +7,14 @@ export type Field<T> = {
|
|
|
7
7
|
valid: boolean;
|
|
8
8
|
errorMessage?: string;
|
|
9
9
|
};
|
|
10
|
-
export type Options<T> = {
|
|
10
|
+
export type Options<T, C> = {
|
|
11
11
|
name?: string;
|
|
12
12
|
value?: T;
|
|
13
13
|
disabled?: boolean;
|
|
14
14
|
touched?: boolean;
|
|
15
15
|
showValidationOn?: 'submit' | 'blur' | 'change';
|
|
16
|
-
|
|
16
|
+
parseEvent?: (e: C) => T;
|
|
17
|
+
formatValue?: (value: T) => any;
|
|
17
18
|
formatErrorMessage?: (error: $ZodIssue, value: T, name?: string) => string;
|
|
18
19
|
_onInit?: (field: Field<T>) => void;
|
|
19
20
|
_onUpdate?: (field: Partial<Field<T>>) => void;
|
package/dist/useField.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ChangeEvent } from 'react';
|
|
|
2
2
|
import { ZodType } from 'zod';
|
|
3
3
|
import { $ZodIssue } from 'zod/v4/core';
|
|
4
4
|
import { Field, Options } from './types.js';
|
|
5
|
-
export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn,
|
|
5
|
+
export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(schema: ZodType, { name, value, disabled, touched, showValidationOn, parseEvent, formatValue, formatErrorMessage, _onInit, _onUpdate, _storedField, }?: Options<T, C>): {
|
|
6
6
|
valid: boolean;
|
|
7
7
|
update: (data: Partial<Field<T>>) => void;
|
|
8
8
|
reset: () => void;
|
|
@@ -11,7 +11,7 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
|
|
|
11
11
|
inputProps: {
|
|
12
12
|
onFocus: () => void;
|
|
13
13
|
onBlur: () => void;
|
|
14
|
-
value:
|
|
14
|
+
value: any;
|
|
15
15
|
disabled: boolean;
|
|
16
16
|
name: string | undefined;
|
|
17
17
|
'data-valid': boolean;
|
|
@@ -19,7 +19,7 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
|
|
|
19
19
|
} | {
|
|
20
20
|
onFocus: () => void;
|
|
21
21
|
onBlur?: undefined;
|
|
22
|
-
value:
|
|
22
|
+
value: any;
|
|
23
23
|
disabled: boolean;
|
|
24
24
|
name: string | undefined;
|
|
25
25
|
'data-valid': boolean;
|
|
@@ -28,7 +28,7 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
|
|
|
28
28
|
props: {
|
|
29
29
|
onFocus: () => void;
|
|
30
30
|
onBlur: () => void;
|
|
31
|
-
value:
|
|
31
|
+
value: any;
|
|
32
32
|
disabled: boolean;
|
|
33
33
|
name: string | undefined;
|
|
34
34
|
valid: boolean;
|
|
@@ -37,7 +37,7 @@ export default function useField<T = string, C = ChangeEvent<HTMLInputElement>>(
|
|
|
37
37
|
} | {
|
|
38
38
|
onFocus: () => void;
|
|
39
39
|
onBlur?: undefined;
|
|
40
|
-
value:
|
|
40
|
+
value: any;
|
|
41
41
|
disabled: boolean;
|
|
42
42
|
name: string | undefined;
|
|
43
43
|
valid: boolean;
|
package/dist/useField.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
2
|
import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const defaultParseEvent = (e) => e.target.value;
|
|
4
|
+
const defaultFormatValue = (value) => value;
|
|
5
|
+
export default function useField(schema, { name, value = '', disabled = false, touched = false, showValidationOn = 'submit', parseEvent = (defaultParseEvent), formatValue = (defaultFormatValue), formatErrorMessage = defaultFormatErrorMessage, _onInit, _onUpdate, _storedField, } = {}) {
|
|
5
6
|
function _validate(value) {
|
|
6
7
|
const { success, error } = schema.safeParse(value);
|
|
7
8
|
if (!success && error.issues.length > 0) {
|
|
@@ -62,7 +63,7 @@ export default function useField(schema, { name, value = '', disabled = false, t
|
|
|
62
63
|
return schema.safeParse(field.value);
|
|
63
64
|
}
|
|
64
65
|
function onChange(e) {
|
|
65
|
-
update({ value:
|
|
66
|
+
update({ value: parseEvent(e) });
|
|
66
67
|
}
|
|
67
68
|
// Only show validation error when is touched
|
|
68
69
|
const valid = !field.touched ? true : !field.errorMessage;
|
|
@@ -88,8 +89,9 @@ export default function useField(schema, { name, value = '', disabled = false, t
|
|
|
88
89
|
errorMessage,
|
|
89
90
|
}));
|
|
90
91
|
}
|
|
92
|
+
const inputValue = formatValue(field.value);
|
|
91
93
|
const inputProps = {
|
|
92
|
-
value:
|
|
94
|
+
value: inputValue,
|
|
93
95
|
disabled: field.disabled,
|
|
94
96
|
name,
|
|
95
97
|
'data-valid': valid,
|
|
@@ -97,7 +99,7 @@ export default function useField(schema, { name, value = '', disabled = false, t
|
|
|
97
99
|
...getListeners(),
|
|
98
100
|
};
|
|
99
101
|
const props = {
|
|
100
|
-
value:
|
|
102
|
+
value: inputValue,
|
|
101
103
|
disabled: field.disabled,
|
|
102
104
|
name,
|
|
103
105
|
valid,
|
package/dist/useForm.d.ts
CHANGED
|
@@ -2,8 +2,14 @@ import { FormEvent, ChangeEvent } from 'react';
|
|
|
2
2
|
import { z, ZodObject, ZodError, ZodRawShape } from 'zod';
|
|
3
3
|
import { $ZodIssue } from 'zod/v4/core';
|
|
4
4
|
import { Options } from './types.js';
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import useField from './useField.js';
|
|
6
|
+
export type FieldsMap = Record<string, ReturnType<typeof useField<any, any>>>;
|
|
7
|
+
type Config = {
|
|
8
|
+
formatErrorMessage?: (error: $ZodIssue, value: any, name?: string) => string;
|
|
9
|
+
_onUpdate?: (fields: FieldsMap) => void;
|
|
10
|
+
};
|
|
11
|
+
export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, { formatErrorMessage, _onUpdate }?: Config): {
|
|
12
|
+
useFormField: <T = string, C = ChangeEvent<HTMLInputElement>>(_name: keyof S, options?: Omit<Options<T, C>, 'formatErrorMessage' | 'name' | '_onUpdateValue'>) => {
|
|
7
13
|
valid: boolean;
|
|
8
14
|
update: (data: Partial<import("./types.js").Field<T>>) => void;
|
|
9
15
|
reset: () => void;
|
|
@@ -12,7 +18,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
12
18
|
inputProps: {
|
|
13
19
|
onFocus: () => void;
|
|
14
20
|
onBlur: () => void;
|
|
15
|
-
value:
|
|
21
|
+
value: any;
|
|
16
22
|
disabled: boolean;
|
|
17
23
|
name: string | undefined;
|
|
18
24
|
'data-valid': boolean;
|
|
@@ -20,7 +26,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
20
26
|
} | {
|
|
21
27
|
onFocus: () => void;
|
|
22
28
|
onBlur?: undefined;
|
|
23
|
-
value:
|
|
29
|
+
value: any;
|
|
24
30
|
disabled: boolean;
|
|
25
31
|
name: string | undefined;
|
|
26
32
|
'data-valid': boolean;
|
|
@@ -29,7 +35,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
29
35
|
props: {
|
|
30
36
|
onFocus: () => void;
|
|
31
37
|
onBlur: () => void;
|
|
32
|
-
value:
|
|
38
|
+
value: any;
|
|
33
39
|
disabled: boolean;
|
|
34
40
|
name: string | undefined;
|
|
35
41
|
valid: boolean;
|
|
@@ -38,7 +44,7 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
38
44
|
} | {
|
|
39
45
|
onFocus: () => void;
|
|
40
46
|
onBlur?: undefined;
|
|
41
|
-
value:
|
|
47
|
+
value: any;
|
|
42
48
|
disabled: boolean;
|
|
43
49
|
name: string | undefined;
|
|
44
50
|
valid: boolean;
|
|
@@ -63,3 +69,4 @@ export default function useForm<S extends ZodRawShape>(schema: ZodObject<S>, for
|
|
|
63
69
|
checkDirty: () => boolean;
|
|
64
70
|
reset: () => void;
|
|
65
71
|
};
|
|
72
|
+
export {};
|
package/dist/useForm.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { useRef } from 'react';
|
|
2
2
|
import defaultFormatErrorMessage from './defaultFormatErrorMessage.js';
|
|
3
3
|
import useField from './useField.js';
|
|
4
|
-
function
|
|
5
|
-
const obj = {};
|
|
6
|
-
for (const name in fields) {
|
|
7
|
-
obj[name] = fields[name].value;
|
|
8
|
-
}
|
|
9
|
-
return obj;
|
|
10
|
-
}
|
|
11
|
-
export default function useForm(schema, formatErrorMessage = defaultFormatErrorMessage) {
|
|
4
|
+
export default function useForm(schema, { formatErrorMessage = defaultFormatErrorMessage, _onUpdate } = {}) {
|
|
12
5
|
const fields = useRef({});
|
|
13
6
|
function useFormField(_name, options = {}) {
|
|
14
7
|
const name = String(_name);
|
|
@@ -31,6 +24,9 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
|
|
|
31
24
|
...fields.current[name],
|
|
32
25
|
...data,
|
|
33
26
|
};
|
|
27
|
+
if (_onUpdate) {
|
|
28
|
+
_onUpdate(fields.current);
|
|
29
|
+
}
|
|
34
30
|
},
|
|
35
31
|
});
|
|
36
32
|
return field;
|
|
@@ -92,3 +88,10 @@ export default function useForm(schema, formatErrorMessage = defaultFormatErrorM
|
|
|
92
88
|
reset,
|
|
93
89
|
};
|
|
94
90
|
}
|
|
91
|
+
function mapFieldsToData(fields) {
|
|
92
|
+
const obj = {};
|
|
93
|
+
for (const name in fields) {
|
|
94
|
+
obj[name] = fields[name].value;
|
|
95
|
+
}
|
|
96
|
+
return obj;
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/forms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
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,5 @@
|
|
|
55
55
|
"typescript": "^5.4.5",
|
|
56
56
|
"zod": "^4.1.11"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "4dd312c8cf505d0c06d25ca489b893a6c6c0d9fe"
|
|
59
59
|
}
|