This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
/
react-cool-form.d.ts
184 lines (153 loc) 路 4.31 KB
/
react-cool-form.d.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
declare module "react-cool-form" {
import { FormEvent, FocusEvent, RefObject, SyntheticEvent } from "react";
export type FormValues = Record<string, any>;
type Prop<V, T = any> = { [K in keyof V]?: V[K] extends T ? T : Prop<V[K]> };
type Errors<V> = Prop<V>;
export interface FormState<V = FormValues> {
readonly values: V;
readonly touched: Prop<V, boolean>;
readonly errors: Errors<V>;
readonly isDirty: boolean;
readonly dirtyFields: Prop<V, boolean>;
readonly isValidating: boolean;
readonly isValid: boolean;
readonly isSubmitting: boolean;
readonly isSubmitted: boolean;
readonly submitCount: number;
}
type Options<V> = Omit<
Return<V>,
"formRef" | "validate" | "handleReset" | "handleSubmit" | "controller"
>;
type Event = FormEvent<HTMLFormElement> | SyntheticEvent<any>;
export interface OnReset<V = FormValues> {
(values: V, options: Options<V>, event: Event): void;
}
export interface OnSubmit<V = FormValues> {
(values: V, options: Options<V>, event: Event): void | Promise<void>;
}
export interface OnError<V = FormValues> {
(errors: Errors<V>, options: Options<V>, event: Event): void;
}
export interface Debug<V> {
(formState: FormState<V>): void;
}
interface Set {
(object: any, path: string, value?: unknown, immutable?: boolean): any;
}
export interface FormValidator<V = FormValues> {
(values: V, set: Set): Errors<V> | void | Promise<Errors<V> | void>;
}
export interface FieldValidator<V = FormValues> {
(value: any, values: V): any | Promise<any>;
}
interface ValidateRef<V> {
(validate: FieldValidator<V>): (
field: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null
) => void;
}
interface GetFormState {
(path: string | string[] | Record<string, string>, watch?: boolean): any;
}
interface SetErrors<V> {
(
errors?:
| Errors<V>
| ((previousErrors: Errors<V>) => Errors<V> | undefined)
): void;
}
interface SetFieldError {
(name: string, error?: any | ((previousError?: any) => any)): void;
}
interface SetValues<V> {
(
values: V | ((previousValues: V) => V),
options?: {
shouldValidate?: boolean;
touchedFields?: string[];
dirtyFields?: string[];
}
): void;
}
interface SetFieldValue {
(
name: string,
value: any | ((previousValue: any) => any),
options?: {
[k in "shouldValidate" | "isTouched" | "isDirty"]?: boolean;
}
): void;
}
interface ValidateForm<V> {
(): Promise<Errors<V>>;
}
interface ValidateField<V> {
(name: string): Promise<Errors<V>>;
}
interface Reset<V> {
(
values?: V | ((previousValues: V) => V),
exclude?: (keyof FormState<V>)[]
): void;
}
interface EventHandler {
(event: Event): void;
}
export interface Parser<E = any, R = any> {
(event: E): R;
}
export interface OnChange<E = any> {
(event: E, value?: any): void;
}
export interface OnBlur {
(event: FocusEvent<any>): void;
}
export interface Controller<V = FormValues, E = any> {
(
name: string,
options?: {
validate?: FieldValidator<V>;
value?: any;
parser?: Parser<E>;
onChange?: OnChange<E>;
onBlur?: OnBlur;
}
):
| {
name: string;
value: any;
onChange: (event: E) => void;
onBlur: OnBlur;
}
| Record<string, unknown>;
}
export interface Config<V = FormValues> {
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 = FormValues> {
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>;
}
const useForm: <V extends FormValues = FormValues>(
config: Config<V>
) => Return<V>;
export default useForm;
}