-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
types.ts
158 lines (132 loc) · 5 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
export type GetInputPropsType = 'input' | 'checkbox';
export type FormStatus = Record<string, boolean>;
export interface FormFieldValidationResult {
hasError: boolean;
error: React.ReactNode;
}
export interface FormValidationResult {
hasErrors: boolean;
errors: FormErrors;
}
export type FormErrors = Record<string, React.ReactNode>;
export interface ReorderPayload {
from: number;
to: number;
}
type Rule<Value, Values> = (value: Value, values: Values, path: string) => React.ReactNode;
type FormRule<Value, Values> = Value extends Array<infer ListValue>
?
| Partial<{
[Key in keyof ListValue]: ListValue[Key] extends Array<infer NestedListItem>
? FormRulesRecord<NestedListItem> | Rule<ListValue[Key], Values>
: FormRulesRecord<ListValue[Key]> | Rule<ListValue[Key], Values>;
}>
| Rule<Value, Values>
: Value extends Record<string, unknown>
? FormRulesRecord<Value> | Rule<Value, Values>
: Rule<Value, Values>;
export type FormRulesRecord<Values> = Partial<{
[Key in keyof Values]: FormRule<Values[Key], Values>;
}>;
export type FormValidateInput<Values> = FormRulesRecord<Values> | ((values: Values) => FormErrors);
export type LooseKeys<Values> = keyof Values | (string & {});
export type SetValues<Values> = React.Dispatch<React.SetStateAction<Partial<Values>>>;
export type SetErrors = React.Dispatch<React.SetStateAction<FormErrors>>;
export type SetFormStatus = React.Dispatch<React.SetStateAction<FormStatus>>;
export type OnSubmit<Values, TransformValues extends _TransformValues<Values>> = (
handleSubmit: (
values: ReturnType<TransformValues>,
event: React.FormEvent<HTMLFormElement>
) => void,
handleValidationFailure?: (
errors: FormErrors,
values: Values,
event: React.FormEvent<HTMLFormElement>
) => void
) => (event?: React.FormEvent<HTMLFormElement>) => void;
export type OnReset = (event: React.FormEvent<HTMLFormElement>) => void;
export type GetInputProps<Values> = <Field extends LooseKeys<Values>>(
path: Field,
options?: { type?: GetInputPropsType; withError?: boolean; withFocus?: boolean }
) => any;
export type SetFieldValue<Values> = <Field extends LooseKeys<Values>>(
path: Field,
value: Field extends keyof Values ? Values[Field] : unknown
) => void;
export type ClearFieldError = (path: unknown) => void;
export type ClearFieldDirty = (path: unknown) => void;
export type ClearErrors = () => void;
export type Reset = () => void;
export type Validate = () => FormValidationResult;
export type ValidateField<Values> = <Field extends LooseKeys<Values>>(
path: Field
) => FormFieldValidationResult;
export type SetFieldError<Values> = <Field extends LooseKeys<Values>>(
path: Field,
error: React.ReactNode
) => void;
export type ReorderListItem<Values> = <Field extends LooseKeys<Values>>(
path: Field,
payload: ReorderPayload
) => void;
export type InsertListItem<Values> = <Field extends LooseKeys<Values>>(
path: Field,
item: unknown,
index?: number
) => void;
export type RemoveListItem<Values> = <Field extends LooseKeys<Values>>(
path: Field,
index: number
) => void;
export type GetFieldStatus<Values> = <Field extends LooseKeys<Values>>(path?: Field) => boolean;
export type ResetStatus = () => void;
export type ResetDirty<Values> = (values?: Values) => void;
export type IsValid<Values> = <Field extends LooseKeys<Values>>(path?: Field) => boolean;
export type _TransformValues<Values> = (values: Values) => unknown;
export interface UseFormInput<
Values,
TransformValues extends _TransformValues<Values> = (values: Values) => Values
> {
initialValues?: Values;
initialErrors?: FormErrors;
initialTouched?: FormStatus;
initialDirty?: FormStatus;
transformValues?: TransformValues;
validate?: FormValidateInput<Values>;
clearInputErrorOnChange?: boolean;
validateInputOnChange?: boolean | LooseKeys<Values>[];
validateInputOnBlur?: boolean | LooseKeys<Values>[];
}
export interface UseFormReturnType<
Values,
TransformValues extends _TransformValues<Values> = (values: Values) => Values
> {
values: Values;
errors: FormErrors;
setValues: SetValues<Values>;
setErrors: SetErrors;
setFieldValue: SetFieldValue<Values>;
setFieldError: SetFieldError<Values>;
clearFieldError: ClearFieldError;
clearErrors: ClearErrors;
reset: Reset;
validate: Validate;
validateField: ValidateField<Values>;
reorderListItem: ReorderListItem<Values>;
removeListItem: RemoveListItem<Values>;
insertListItem: InsertListItem<Values>;
getInputProps: GetInputProps<Values>;
onSubmit: OnSubmit<Values, TransformValues>;
onReset: OnReset;
isDirty: GetFieldStatus<Values>;
isTouched: GetFieldStatus<Values>;
setTouched: SetFormStatus;
setDirty: SetFormStatus;
resetTouched: ResetStatus;
resetDirty: ResetDirty<Values>;
isValid: IsValid<Values>;
}
export type UseForm<
Values = Record<string, unknown>,
TransformValues extends _TransformValues<Values> = (values: Values) => Values
> = (input?: UseFormInput<Values, TransformValues>) => UseFormReturnType<Values, TransformValues>;