This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
/
index.ts
206 lines (172 loc) 路 4.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
FormEvent,
FocusEvent,
MutableRefObject,
RefObject,
SyntheticEvent,
} from "react";
// Common
export type UsedRef = Record<string, boolean>;
// State
type Prop<V, T = any> = { [K in keyof V]?: V[K] extends T ? T : Prop<V[K]> };
export type Errors<V> = Prop<V>;
export interface FormState<V> {
values: V;
touched: Prop<V, boolean>;
errors: Errors<V>;
isDirty: boolean;
dirtyFields: Prop<V, boolean>;
isValidating: boolean;
isValid: boolean;
isSubmitting: boolean;
isSubmitted: boolean;
submitCount: number;
}
export type StateRef<V> = MutableRefObject<FormState<V>>;
export interface SetStateRef {
(path: string, value?: any): void;
}
export interface ResetStateRef<V> {
(
values: V | undefined,
exclude: (keyof FormState<V>)[],
callback: (nextValues: V) => void
): void;
}
export interface SetUsedStateRef {
(path: string): void;
}
export interface FormStateReturn<V> {
stateRef: StateRef<V>;
setStateRef: SetStateRef;
resetStateRef: ResetStateRef<V>;
setUsedStateRef: SetUsedStateRef;
}
// Hook
export type FormValues = Record<string, any>;
export type FieldElement =
| HTMLInputElement
| HTMLTextAreaElement
| HTMLSelectElement;
export type Fields = Record<
string,
{ field: FieldElement; options?: FieldElement[] }
>;
type Options<V> = Omit<
Return<V>,
"formRef" | "validate" | "handleReset" | "handleSubmit" | "controller"
>;
type Event = FormEvent<HTMLFormElement> | SyntheticEvent<any>;
interface OnReset<V> {
(values: V, options: Options<V>, event: Event): void;
}
interface OnSubmit<V> {
(values: V, options: Options<V>, event: Event): void | Promise<void>;
}
interface OnError<V> {
(errors: Errors<V>, options: Options<V>, event: Event): void;
}
export interface Debug<V> {
(formState: FormState<V>): void;
}
export interface Set {
(object: any, path: string, value?: unknown, immutable?: boolean): any;
}
interface FormValidator<V> {
(values: V, set: Set): Errors<V> | void | Promise<Errors<V> | void>;
}
export interface FieldValidator<V> {
(value: any, values: V): any | Promise<any>;
}
export interface ValidateRef<V> {
(validate: FieldValidator<V>): (field: FieldElement | null) => void;
}
export interface GetFormState {
(path: string | string[] | Record<string, string>, watch?: boolean): any;
}
export interface SetErrors<V> {
(
errors?: Errors<V> | ((previousErrors: Errors<V>) => Errors<V> | undefined)
): void;
}
export interface SetFieldError {
(name: string, error?: any | ((previousError?: any) => any)): void;
}
export interface SetValues<V> {
(
values: V | ((previousValues: V) => V),
options?: {
shouldValidate?: boolean;
touchedFields?: string[];
dirtyFields?: string[];
}
): void;
}
export interface SetFieldValue {
(
name: string,
value: any | ((previousValue: any) => any),
options?: {
[k in "shouldValidate" | "isTouched" | "isDirty"]?: boolean;
}
): void;
}
export interface ValidateForm<V> {
(): Promise<Errors<V>>;
}
export interface ValidateField<V> {
(name: string): Promise<Errors<V>>;
}
export interface Reset<V> {
(
values?: V | ((previousValues: V) => V),
exclude?: (keyof FormState<V>)[]
): void;
}
export interface EventHandler {
(event: Event): void;
}
export interface Controller<V, E = any> {
(
name: string,
options?: {
validate?: FieldValidator<V>;
value?: any;
parser?: (event: E) => any;
onChange?: (event: E, value?: any) => void;
onBlur?: (event: FocusEvent<any>) => void;
}
):
| {
name: string;
value: any;
onChange: (event: E) => void;
onBlur: (event: FocusEvent<any>) => void;
}
| Record<string, unknown>;
}
export interface Config<V> {
initialValues: V;
validate?: FormValidator<V>;
validateOnChange?: boolean;
validateOnBlur?: boolean;
onReset?: OnReset<V>;
onSubmit?: OnSubmit<V>;
onError?: OnError<V>;
debug?: Debug<V>;
}
export interface Return<V> {
formRef: RefObject<HTMLFormElement>;
validate: ValidateRef<V>;
getFormState: GetFormState;
setErrors: SetErrors<V>;
setFieldError: SetFieldError;
setValues: SetValues<V>;
setFieldValue: SetFieldValue;
validateForm: ValidateForm<V>;
validateField: ValidateField<V>;
reset: Reset<V>;
handleReset: EventHandler;
handleSubmit: EventHandler;
controller: Controller<V>;
}