-
Notifications
You must be signed in to change notification settings - Fork 29.9k
/
index.d.ts
188 lines (161 loc) · 6.46 KB
/
index.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
185
186
187
188
import * as React from "react";
// Helper Types
export type FormValue = any;
export type FormError = string | undefined;
export interface Nested<T> {
[key: string]: T | Nested<T>;
}
export type FormValues = Nested<FormValue>;
export type Touched = Nested<boolean>;
export interface FormErrors {
[key: string]: FormError;
}
export type NestedErrors = Nested<FormErrors>;
export type RenderReturn = React.JSX.Element | false | null | never[];
export interface FormState {
values: FormValues;
touched: Touched;
errors: FormErrors;
nestedErrors: NestedErrors;
dirty?: boolean | undefined;
}
export interface FormProps {
component?: React.ElementType<{ formApi: FormApi }> | undefined;
render?: ((formApi: FormApi) => RenderReturn) | undefined;
dontValidateOnMount?: boolean | undefined;
validateOnSubmit?: boolean | undefined;
defaultValues?: FormValues | undefined;
onSubmit?(values: FormValues, submissionEvent: React.SyntheticEvent<any>, formApi: FormApi): void;
preSubmit?(values: FormValues, formApi: FormApi): FormValues;
onSubmitFailure?(errors: FormErrors, formApi: FormApi): void;
formDidUpdate?(formState: FormState): void;
preValidate?(values: FormValues): FormValues;
validateError?: ValidateValuesFunction | undefined;
validateWarning?: ValidateValuesFunction | undefined;
validateSuccess?: ((values: FormValues, errors: FormErrors) => FormErrors) | undefined;
asyncValidators?: {
[field: string]: (value: FormValue) => Promise<any>;
} | undefined;
dontPreventDefault?: boolean | undefined;
getApi?: ((formApi: FormApi) => void) | undefined;
}
export interface FormApi {
// State
values: FormValues;
touched: Touched;
errors: FormErrors;
warnings: FormErrors;
successes: FormErrors;
submits: number;
submitted: boolean;
asyncValidations: number;
validating: { [field: string]: boolean };
validationFailures: number;
validationFailed: { [field: string]: boolean };
// Methods
submitForm(event: React.SyntheticEvent<any>): void;
setValue(fieldName: string, value: any): void;
setAllValues(values: FormValues): void;
setError(field: string, error: string): void;
setWarning(field: string, warning: string): void;
setSuccess(field: string, success: string): void;
setTouched(field: string, touched: boolean): void;
setAllTouched(touches: { [field: string]: boolean }): void;
addValue(name: string, value: any): void;
removeValue(name: string, index: number): void;
swapValues(name: string, index1: number, index2: number): void;
resetAll(): void;
getFormState(): FormState;
setFormState(state: FormState): void;
}
export type ValidateValuesFunction = (values: FormValues) => FormErrors;
export interface FormFunctionProps extends FormProps, FormState, FormApi {}
export interface FormContext {
formApi: FormApi;
}
export class Form extends React.Component<
FormProps & { children?: ((props: FormFunctionProps) => RenderReturn) | RenderReturn | undefined }
> implements React.ChildContextProvider<FormContext> {
static defaultProps: FormProps;
static childContextTypes: {
formApi: React.Validator<any>;
};
getDefaultState(): FormState;
getChildContext(): FormContext;
componentWillMount(): void;
componentWillReceiveProps(nextProps: Readonly<Partial<FormProps>>, nextContext: any): void;
componentWillUnmount(): void;
render(): RenderReturn;
}
export const NestedForm: React.FunctionComponent<FieldProps>;
export function FormField(component: React.ComponentType<any>): React.ComponentClass<any>;
// Fields
export interface FieldApi {
getValue(): FormValue;
getError(): FormError;
getWarning(): FormError;
getSuccess(): FormError;
getTouched(): boolean;
getFieldName(): string;
setValue(value: FormValue): void;
setError(error: FormError): void;
setWarning(warning: FormError): void;
setSuccess(success: FormError): void;
setTouched(touched: boolean): void;
}
export interface FieldProps {
children?: React.ReactNode;
field?: string | string[] | Array<string | number> | Array<(string | Array<string | number>)> | undefined;
showErrors?: boolean | undefined;
errorBefore?: boolean | undefined;
isForm?: boolean | undefined;
}
export type SelectOptions = Array<{
value: FormValue;
label: string;
}>;
export interface SelectProps extends FieldProps, React.SelectHTMLAttributes<HTMLSelectElement> {
options: SelectOptions;
placeholder?: string | undefined;
}
export const Select: React.FunctionComponent<SelectProps>;
export const Text: React.FunctionComponent<FieldProps & React.InputHTMLAttributes<HTMLInputElement>>;
export const TextArea: React.FunctionComponent<FieldProps & React.TextareaHTMLAttributes<HTMLTextAreaElement>>;
export interface RadioGroupContext {
group: FieldApi;
}
export class RadioGroup extends React.Component<
Omit<FieldProps, "children"> & { children?: ((props: FieldApi) => RenderReturn) | RenderReturn | undefined }
> implements React.ChildContextProvider<RadioGroupContext> {
getChildContext(): {
group: FieldApi;
};
}
export const Radio: React.FunctionComponent<
FieldProps & React.InputHTMLAttributes<HTMLInputElement> & { group: FieldApi }
>;
export const Checkbox: React.FunctionComponent<FieldProps & React.InputHTMLAttributes<HTMLInputElement>>;
// Styled Fields
export interface StyledProps extends FieldProps {
noMessage?: boolean | undefined;
messageBefore?: boolean | undefined;
touchValidation?: boolean | undefined;
}
export const StyledCheckbox: React.FunctionComponent<
StyledProps & React.InputHTMLAttributes<HTMLInputElement> & { label: string }
>;
export const StyledTextArea: React.FunctionComponent<StyledProps & React.TextareaHTMLAttributes<HTMLTextAreaElement>>;
export const StyledSelect: React.FunctionComponent<
StyledProps & SelectProps & React.InputHTMLAttributes<HTMLSelectElement>
>;
export const StyledText: React.FunctionComponent<StyledProps & React.InputHTMLAttributes<HTMLInputElement>>;
export const StyledRadio: React.FunctionComponent<
StyledProps & React.InputHTMLAttributes<HTMLInputElement> & { group: FieldApi; label: string }
>;
export class StyledRadioGroup extends React.Component<
Omit<StyledProps, "children"> & { children?: ((props: FieldApi) => RenderReturn) | RenderReturn | undefined }
> implements React.ChildContextProvider<RadioGroupContext> {
getChildContext(): {
group: FieldApi;
};
}