This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.ts
152 lines (142 loc) Β· 4.14 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
import {
FieldValues,
Control,
Message,
ValidationRule,
ValidateResult,
} from 'react-hook-form';
export type ArrayWithLength<N extends number> = { [K in N]: any };
export interface DeepPathArray<TValues extends FieldValues, TPath>
extends ReadonlyArray<string | number> {
['0']: keyof TValues;
['1']?: TPath extends {
['0']: infer K0;
}
? K0 extends keyof TValues
? keyof TValues[K0]
: never
: never;
['2']?: TPath extends {
['0']: infer K0;
['1']: infer K1;
}
? K0 extends keyof TValues
? K1 extends keyof TValues[K0]
? keyof TValues[K0][K1]
: never
: never
: never;
['3']?: TPath extends {
['0']: infer K0;
['1']: infer K1;
['2']: infer K2;
}
? K0 extends keyof TValues
? K1 extends keyof TValues[K0]
? K2 extends keyof TValues[K0][K1]
? keyof TValues[K0][K1][K2]
: never
: never
: never
: never;
['4']?: TPath extends {
['0']: infer K0;
['1']: infer K1;
['2']: infer K2;
['3']: infer K3;
}
? K0 extends keyof TValues
? K1 extends keyof TValues[K0]
? K2 extends keyof TValues[K0][K1]
? K3 extends keyof TValues[K0][K1][K2]
? keyof TValues[K0][K1][K2][K3]
: never
: never
: never
: never
: never;
['5']?: TPath extends {
['0']: infer K0;
['1']: infer K1;
['2']: infer K2;
['3']: infer K3;
['4']: infer K4;
}
? K0 extends keyof TValues
? K1 extends keyof TValues[K0]
? K2 extends keyof TValues[K0][K1]
? K3 extends keyof TValues[K0][K1][K2]
? K4 extends keyof TValues[K0][K1][K2][K3]
? keyof TValues[K0][K1][K2][K3][K4]
: never
: never
: never
: never
: never
: never;
}
export type DeepPathArrayValue<
TValues extends FieldValues,
TPath extends DeepPathArray<TValues, TPath>
> = TPath extends ArrayWithLength<0 | 1 | 2 | 3 | 4 | 5 | 6>
? any
: TPath extends ArrayWithLength<0 | 1 | 2 | 3 | 4 | 5>
? TValues[TPath[0]][TPath[1]][TPath[2]][TPath[3]][TPath[4]][TPath[5]]
: TPath extends ArrayWithLength<0 | 1 | 2 | 3 | 4>
? TValues[TPath[0]][TPath[1]][TPath[2]][TPath[3]][TPath[4]]
: TPath extends ArrayWithLength<0 | 1 | 2 | 3>
? TValues[TPath[0]][TPath[1]][TPath[2]][TPath[3]]
: TPath extends ArrayWithLength<0 | 1 | 2>
? TValues[TPath[0]][TPath[1]][TPath[2]]
: TPath extends ArrayWithLength<0 | 1>
? TValues[TPath[0]][TPath[1]]
: TPath extends ArrayWithLength<0>
? TValues[TPath[0]]
: never;
export type DeepPath<TValues extends FieldValues, TPath> =
| DeepPathArray<TValues, TPath>
| keyof TValues;
export type DeepPathValue<
TValues extends FieldValues,
TPath extends DeepPath<TValues, TPath>
> = TPath extends DeepPathArray<TValues, TPath>
? DeepPathArrayValue<TValues, TPath>
: TPath extends keyof TValues
? TValues[TPath]
: any;
export type Options<TControl extends Control> = {
control?: TControl;
};
export type Validate<TFieldValue> = (
data: TFieldValue,
) => ValidateResult | Promise<ValidateResult>;
export type ValidationRules<TFieldValue> = Partial<{
required: Message | ValidationRule<boolean>;
min: ValidationRule<number | string>;
max: ValidationRule<number | string>;
maxLength: ValidationRule<number | string>;
minLength: ValidationRule<number | string>;
pattern: ValidationRule<RegExp>;
validate: Validate<TFieldValue> | Record<string, Validate<TFieldValue>>;
}>;
export type Assign<TValues extends object, U extends object> = TValues &
Omit<U, keyof TValues>;
export type ControllerProps<
TFieldValues extends FieldValues,
TFieldName extends DeepPath<TFieldValues, TFieldName>,
TAs extends 'input' | 'select' | 'textarea'
> = Assign<
{
name: TFieldName;
as?: TAs;
rules?: ValidationRules<DeepPathValue<TFieldValues, TFieldName>>;
onFocus?: () => void;
defaultValue?: DeepPathValue<TFieldValues, TFieldName>;
render?: (props: {
onChange: (...event: any[]) => void;
onBlur: () => void;
value: DeepPathValue<TFieldValues, TFieldName>;
}) => React.ReactElement;
},
JSX.IntrinsicElements[TAs]
>;