-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
334 lines (300 loc) · 7.79 KB
/
types.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { Component } from 'vue';
declare const $NestedValue: unique symbol;
export type ObjectType = Record<string, any>;
export type NativeObjectType =
| Date
| Blob
| File
| FileList
| Function
| RegExp;
export type NestedValue<T extends ObjectType> = {
[$NestedValue]: never;
} & T;
export type NestedValueType = {
[$NestedValue]: never;
};
export type UnpackNestedValue<T> = T extends NestedValue<infer U>
? U
: T extends NativeObjectType
? T
: T extends Array<infer A>
? UnpackNestedValue<A>[]
: T extends ObjectType
? { [K in keyof T]: UnpackNestedValue<T[K]> }
: T;
export type UnpackFieldState<T> = T extends NestedValueType
? FieldState
: T extends NativeObjectType
? FieldState
: T extends Array<infer A>
? UnpackFieldState<A>[]
: T extends ObjectType
? { [K in keyof T]: UnpackFieldState<T[K]> }
: FieldState;
// `${number}` => 0
export type ArrayPathToString<T> = T extends `${number}` ? 0 : T;
// all ${number}` => 0
export type NormalizePath<T extends string> = T extends `${infer A}.${infer B}`
? `${ArrayPathToString<A>}.${NormalizePath<B>}`
: ArrayPathToString<T>;
// get value of object by key path
// key path: a.b.0.c
export type InternalKeyPathValue<V, Path extends string> = Path extends ''
? V
: V extends ObjectType
? V extends NestedValueType | NativeObjectType
? never
: Path extends `${infer Key}.${infer Rest}`
? KeyPathValue<V[Key], Rest>
: V[Path] extends NestedValue<infer U>
? U
: V extends Array<infer U>
? U | undefined
: V[Path]
: never;
export type KeyPathValue<
V extends ObjectType,
Path extends string
> = string extends Path ? any : InternalKeyPathValue<V, NormalizePath<Path>>;
export type FormType = Record<string, any>;
export declare type DeepPartial<T extends ObjectType> = T extends
| NativeObjectType
| NestedValueType
? T
: T extends Array<infer U>
? U extends ObjectType
? DeepPartial<U>[]
: U[]
: {
[K in keyof T]?: DeepPartial<T[K]>;
};
export type FieldValues<
T extends FormType = FormType,
P extends boolean = false
> = P extends true ? UnpackNestedValue<DeepPartial<T>> : UnpackNestedValue<T>;
export type DisposablePromise<T = any> = {
promise: Promise<T>;
dispose?: () => void;
};
export type FieldError = {
type?: string;
message: string;
};
export type FormErrors<T> = T extends NestedValueType | NativeObjectType
? FieldError | null | undefined
: T extends Array<infer U>
? {
[key: number]: FormErrors<U>;
}
: T extends ObjectType
? {
[K in keyof T]?: FormErrors<T[K]>;
}
: FieldError | null | undefined;
export type FormState<
T extends FormType = FormType,
VFK extends string = string
> = {
// 当前值 { a: { b: { c: 222 }, d: [{ e: 2}] } }
values: FieldValues<T>;
// 默认值
defaultValues: FieldValues<T, true>;
// field error or virtualField error
error: FieldError | null;
// only field error
fieldError: FieldError | null;
// only virtual error
virtualError: FieldError | null;
// all field errors
fieldErrors: FormErrors<T>;
// all virtual field errors
virtualErrors: Partial<Record<VFK, FieldError | null>>;
// 是否有错误
isError: boolean;
// field Error
isFieldError: boolean;
// virtual error
isVirtualError: boolean;
// 正在验证
isValidating: boolean;
// field validating
isFieldValidating: boolean;
// virtual validating
isVirtualValidating: boolean;
// 值被更改过, 当前值和默认值不相等
isDirty: boolean;
// 字段有过交互,比如 input focus
isTouched: boolean;
// 是否改变过
isChanged: boolean;
// 是否提交过
isSubmitted: boolean;
// 正在提交
isSubmitting: boolean;
// 提交次数
submitCount: number;
};
export type FieldState = {
// 错误信息
error: FieldError | null;
// 是否有错误
isError: boolean;
// 正在验证
isValidating: boolean;
// 当前值和默认值不相等
isDirty: boolean;
// 字段有过交互,比如 input focus
isTouched: boolean;
// 是否更改过
isChanged: boolean;
};
export type VirtualFieldState = {
// 错误信息
error: FieldError | null;
// 是否有错误
isError: boolean;
// 正在验证
isValidating: boolean;
};
export type ValidateFunc<V, Deps, Rules = FieldRule<V, Deps>[]> = (
v: V,
deps: Deps | undefined,
rules: Rules
) => DisposablePromise<FieldError | null>;
export type VirtualValidateFunc<V, Rules = VirtualFieldRule<V>[]> = (
v: V,
rules: Rules
) => DisposablePromise<FieldError | null>;
export type Validator<V = any, Deps = any> = (
value: V | undefined,
deps?: Deps
) => string | Promise<string> | DisposablePromise<string>;
export type VirtualFieldValidator<V = any> = (
value: V
) => string | Promise<string> | DisposablePromise<string>;
export type FieldRule<V = any, Deps = any> = {
type?: string;
// short flags
required?: boolean;
requiredLength?: boolean;
minLength?: number;
maxLength?: number;
min?: number;
max?: number;
pattern?: RegExp;
// builtin validators
alpha?: boolean;
alphaNum?: boolean;
decimal?: boolean;
numeric?: boolean;
email?: boolean;
integer?: boolean;
ipAddress?: boolean;
macAddress?: boolean;
// custom validator
validator?: Validator<V, Deps>;
// message
message?: string;
// debounce validate the rule
debounce?: number;
};
export type VirtualFieldRule<V = any> = {
type?: string;
// short flags
required?: boolean;
requiredLength?: boolean;
minLength?: number;
maxLength?: number;
min?: number;
max?: number;
pattern?: RegExp;
// builtin validators
alpha?: boolean;
alphaNum?: boolean;
decimal?: boolean;
numeric?: boolean;
email?: boolean;
integer?: boolean;
ipAddress?: boolean;
macAddress?: boolean;
// custom validator
validator?: VirtualFieldValidator<V>;
// message
message?: string;
// debounce validate the rule
debounce?: number;
};
export type InputLikeRef =
| Element
| Component
| {
focus?: () => void;
};
// Join<'a.b', 'c.d'> => 'a.b.c.d'
export type Join<K, P> = K extends string | number
? P extends string | number
? '' extends K
? P
: `${K}${'' extends P ? '' : '.'}${P}`
: ''
: '';
// list all paths
export type AutoPath<T extends ObjectType, L extends string = ''> = T extends
| NestedValueType
| NativeObjectType
? L
: T extends Array<infer U>
? Join<L, `${number}`> | Join<Join<L, `${number}`>, AutoPath<U>>
: T extends ObjectType
? { [key: string]: any } extends T
? string
: {
[K in keyof T]: Join<L, K> | Join<Join<L, K>, AutoPath<T[K]>>;
}[keyof T]
: L;
export type FieldPath<T extends ObjectType, L extends string = ''> = T extends
| NestedValueType
| NativeObjectType
? L
: T extends Array<infer U>
? Join<Join<L, `${number}`>, FieldPath<U>>
: T extends ObjectType
? { [key: string]: any } extends T
? string
: {
[K in keyof T]: Join<Join<L, K>, FieldPath<T[K]>>;
}[keyof T]
: L;
export type ArrayFieldPath<
T extends ObjectType,
L extends string = ''
> = T extends NestedValueType | NativeObjectType
? never
: T extends Array<infer U>
? L | Join<Join<L, `${number}`>, ArrayFieldPath<U>>
: T extends ObjectType
? { [key: string]: any } extends T
? string
: {
[K in keyof T]: Join<Join<L, K>, ArrayFieldPath<T[K]>>;
}[keyof T]
: never;
export type ArrayItem<T> = T extends Array<infer U> ? U : never;
export type FieldProps<
T extends FormType = FormType,
N extends string = string
> = {
value: KeyPathValue<T, N>;
onChange?: (v: KeyPathValue<T, N> | Event) => void;
onInput?: (v: KeyPathValue<T, N> | Event) => void;
onBlur: () => void;
onFocus: () => void;
ref: (el: InputLikeRef | null) => void;
};
export type FieldScope<
T extends FormType = FormType,
N extends string = string
> = {
field: FieldProps<T, N>;
};