/
types.ts
222 lines (192 loc) · 7.1 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
import { ComputedRef, DeepReadonly, Ref } from 'vue';
import { SchemaOf, AnySchema, AnyObjectSchema } from 'yup';
import { FieldValidationMetaInfo } from '../../shared';
export type GenericFormValues = Record<string, unknown>;
export interface ValidationResult {
errors: string[];
valid: boolean;
}
export type YupValidator = AnySchema | AnyObjectSchema;
export type Locator = { __locatorRef: string } & ((values: GenericFormValues) => unknown);
export type MaybeRef<T> = Ref<T> | T;
export interface FieldMeta<TValue> {
touched: boolean;
dirty: boolean;
valid: boolean;
validated: boolean;
pending: boolean;
initialValue?: TValue;
}
export interface FormMeta<TValues extends Record<string, any>> {
touched: boolean;
dirty: boolean;
valid: boolean;
validated: boolean;
pending: boolean;
initialValues?: TValues;
}
export interface FieldState<TValue = unknown> {
value: TValue;
touched: boolean;
errors: string[];
}
/**
* validated-only: only mutate the previously validated fields
* silent: do not mutate any field
* force: validate all fields and mutate their state
*/
export type SchemaValidationMode = 'validated-only' | 'silent' | 'force';
export interface ValidationOptions {
mode: SchemaValidationMode;
}
export interface FieldEntry<TValue = unknown> {
value: TValue;
key: string | number;
isFirst: boolean;
isLast: boolean;
}
export interface FieldArrayContext<TValue = unknown> {
fields: DeepReadonly<Ref<FieldEntry<TValue>[]>>;
remove(idx: number): void;
replace(newArray: TValue[]): void;
update(idx: number, value: TValue): void;
push(value: TValue): void;
swap(indexA: number, indexB: number): void;
insert(idx: number, value: TValue): void;
prepend(value: TValue): void;
}
export interface PrivateFieldArrayContext {
reset(): void;
}
export interface PrivateFieldContext<TValue = unknown> {
id: number;
name: MaybeRef<string>;
value: Ref<TValue>;
meta: FieldMeta<TValue>;
errors: Ref<string[]>;
errorMessage: Ref<string | undefined>;
label?: MaybeRef<string | undefined>;
type?: string;
bails?: boolean;
checkedValue?: MaybeRef<TValue>;
uncheckedValue?: MaybeRef<TValue>;
checked?: Ref<boolean>;
resetField(state?: Partial<FieldState<TValue>>): void;
handleReset(): void;
validate(opts?: Partial<ValidationOptions>): Promise<ValidationResult>;
handleChange(e: Event | unknown, shouldValidate?: boolean): void;
handleBlur(e?: Event): void;
setState(state: Partial<FieldState<TValue>>): void;
setTouched(isTouched: boolean): void;
setErrors(message: string | string[]): void;
setValue(value: TValue): void;
}
export type FieldContext<TValue = unknown> = Omit<PrivateFieldContext<TValue>, 'id' | 'instances'>;
export type GenericValidateFunction = (
value: unknown,
ctx: FieldValidationMetaInfo
) => boolean | string | Promise<boolean | string>;
export interface FormState<TValues> {
values: TValues;
errors: Partial<Record<keyof TValues, string | undefined>>;
touched: Partial<Record<keyof TValues, boolean>>;
submitCount: number;
}
export type FormErrors<TValues extends GenericFormValues> = Partial<Record<keyof TValues, string | undefined>>;
export type FormErrorBag<TValues extends GenericFormValues> = Partial<Record<keyof TValues, string[]>>;
export interface SetFieldValueOptions {
force: boolean;
}
export interface FormActions<TValues extends GenericFormValues> {
setFieldValue<T extends keyof TValues>(field: T, value: TValues[T], opts?: Partial<SetFieldValueOptions>): void;
setFieldError: (field: keyof TValues, message: string | string[] | undefined) => void;
setErrors: (fields: FormErrors<TValues>) => void;
setValues<T extends keyof TValues>(fields: Partial<Record<T, TValues[T]>>): void;
setFieldTouched: (field: keyof TValues, isTouched: boolean) => void;
setTouched: (fields: Partial<Record<keyof TValues, boolean>>) => void;
resetForm: (state?: Partial<FormState<TValues>>) => void;
}
export interface FormValidationResult<TValues> {
valid: boolean;
results: Partial<Record<keyof TValues, ValidationResult>>;
errors: Partial<Record<keyof TValues, string>>;
}
export interface SubmissionContext<TValues extends GenericFormValues = GenericFormValues> extends FormActions<TValues> {
evt?: Event;
}
export type SubmissionHandler<TValues extends GenericFormValues = GenericFormValues, TReturn = unknown> = (
values: TValues,
ctx: SubmissionContext<TValues>
) => TReturn;
export interface InvalidSubmissionContext<TValues extends GenericFormValues = GenericFormValues> {
values: TValues;
evt?: Event;
errors: Partial<Record<keyof TValues, string>>;
results: Partial<Record<keyof TValues, ValidationResult>>;
}
export type InvalidSubmissionHandler<TValues extends GenericFormValues = GenericFormValues> = (
ctx: InvalidSubmissionContext<TValues>
) => void;
export type RawFormSchema<TValues> = Record<keyof TValues, string | GenericValidateFunction | Record<string, any>>;
export type FieldPathLookup<TValues extends Record<string, any> = Record<string, any>> = Partial<
Record<keyof TValues, PrivateFieldContext | PrivateFieldContext[]>
>;
export interface PrivateFormContext<TValues extends Record<string, any> = Record<string, any>>
extends FormActions<TValues> {
formId: number;
values: TValues;
fieldsByPath: Ref<FieldPathLookup>;
fieldArraysLookup: Record<string, PrivateFieldArrayContext>;
submitCount: Ref<number>;
schema?: MaybeRef<RawFormSchema<TValues> | SchemaOf<TValues> | undefined>;
errorBag: Ref<FormErrorBag<TValues>>;
errors: ComputedRef<FormErrors<TValues>>;
meta: ComputedRef<FormMeta<TValues>>;
isSubmitting: Ref<boolean>;
validateSchema?: (mode: SchemaValidationMode) => Promise<FormValidationResult<TValues>>;
validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues>>;
validateField(field: keyof TValues): Promise<ValidationResult>;
setFieldErrorBag(field: string, messages: string | string[]): void;
stageInitialValue(path: string, value: unknown): void;
unsetInitialValue(path: string): void;
register(field: PrivateFieldContext): void;
unregister(field: PrivateFieldContext): void;
handleSubmit<TReturn = unknown>(
cb: SubmissionHandler<TValues, TReturn>,
onSubmitValidationErrorCb?: InvalidSubmissionHandler<TValues>
): (e?: Event) => Promise<TReturn | undefined>;
setFieldInitialValue(path: string, value: unknown): void;
}
export interface FormContext<TValues extends Record<string, any> = Record<string, any>>
extends Omit<
PrivateFormContext<TValues>,
| 'formId'
| 'register'
| 'unregister'
| 'fieldsByPath'
| 'schema'
| 'validateSchema'
| 'errorBag'
| 'setFieldErrorBag'
| 'stageInitialValue'
| 'setFieldInitialValue'
| 'unsetInitialValue'
| 'fieldArraysLookup'
> {
handleReset: () => void;
submitForm: (e?: unknown) => Promise<void>;
}
export interface DevtoolsPluginFieldState {
name: string;
value: any;
initialValue: any;
errors: string[];
meta: FieldMeta<any>;
}
export interface DevtoolsPluginFormState {
meta: FormMeta<Record<string, any>>;
errors: FormErrors<Record<string, any>>;
values: Record<string, any>;
isSubmitting: boolean;
submitCount: number;
}