formaze 2.1.0 → 2.2.0
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 +104 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { SubmitHandler, DefaultValues } from "react-hook-form";
|
|
3
|
+
|
|
4
|
+
export interface FormProps<T extends z.ZodSchema>
|
|
5
|
+
extends React.FormHTMLAttributes<HTMLFormElement> {
|
|
6
|
+
schema?: T;
|
|
7
|
+
onSubmit: SubmitHandler<z.infer<T>>;
|
|
8
|
+
defaultValues?: DefaultValues<z.infer<T>>;
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
/** default is onSubmit */
|
|
11
|
+
mode?: "onBlur" | "onChange" | "onSubmit" | "onTouched" | "all";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
export interface InputProps<T extends z.ZodSchema>
|
|
16
|
+
extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
17
|
+
name: keyof z.infer<T>;
|
|
18
|
+
label: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface FormFieldProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
22
|
+
name: string;
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type OptionalAndCustomMessageConfig = {
|
|
27
|
+
optional?: boolean;
|
|
28
|
+
customMessage?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type StringFieldConfig = {
|
|
32
|
+
type: "string";
|
|
33
|
+
minLength?: { value: number; message?: string };
|
|
34
|
+
maxLength?: { value: number; message?: string };
|
|
35
|
+
regex?: { value: RegExp; message?: string };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type EmailFieldConfig = {
|
|
39
|
+
type: "email";
|
|
40
|
+
regex?: { value: RegExp; message?: string };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type PasswordFieldConfig = {
|
|
44
|
+
type: "password";
|
|
45
|
+
minLength?: { value: number; message?: string };
|
|
46
|
+
maxLength?: { value: number; message?: string };
|
|
47
|
+
regex?: { value: RegExp; message?: string };
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type NumberFieldConfig = {
|
|
51
|
+
type: "number";
|
|
52
|
+
min?: { value: number | string | Date; message?: string };
|
|
53
|
+
max?: { value: number | string | Date; message?: string };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type DateFieldConfig = {
|
|
57
|
+
type: "date";
|
|
58
|
+
min?: { value: string; message?: string };
|
|
59
|
+
max?: { value: string; message?: string };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type BooleanFieldConfig = {
|
|
63
|
+
type: "boolean";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type FieldConfig = OptionalAndCustomMessageConfig &
|
|
67
|
+
(
|
|
68
|
+
| StringFieldConfig
|
|
69
|
+
| EmailFieldConfig
|
|
70
|
+
| PasswordFieldConfig
|
|
71
|
+
| NumberFieldConfig
|
|
72
|
+
| DateFieldConfig
|
|
73
|
+
| BooleanFieldConfig
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
export type SchemaConfig<T extends Record<string, FieldConfig>> = {
|
|
77
|
+
[K in keyof T]: T[K];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type SchemaKeyValuePair<
|
|
81
|
+
T extends SchemaConfig<Record<string, FieldConfig>>
|
|
82
|
+
> = {
|
|
83
|
+
[K in keyof T]: T[K]["optional"] extends true
|
|
84
|
+
? z.ZodOptional<
|
|
85
|
+
T[K]["type"] extends "email" | "password" | "string"
|
|
86
|
+
? z.ZodString
|
|
87
|
+
: T[K]["type"] extends "number"
|
|
88
|
+
? z.ZodNumber
|
|
89
|
+
: T[K]["type"] extends "boolean"
|
|
90
|
+
? z.ZodBoolean
|
|
91
|
+
: T[K]["type"] extends "date"
|
|
92
|
+
? z.ZodDate
|
|
93
|
+
: z.ZodTypeAny
|
|
94
|
+
>
|
|
95
|
+
: T[K]["type"] extends "string" | "email" | "password"
|
|
96
|
+
? z.ZodString
|
|
97
|
+
: T[K]["type"] extends "number"
|
|
98
|
+
? z.ZodNumber
|
|
99
|
+
: T[K]["type"] extends "boolean"
|
|
100
|
+
? z.ZodBoolean
|
|
101
|
+
: T[K]["type"] extends "date"
|
|
102
|
+
? z.ZodDate
|
|
103
|
+
: z.ZodTypeAny;
|
|
104
|
+
};
|