File size: 4,031 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface FieldState {
	value: any;
	errors: string[] | null;
	isShowingErrors: boolean;
	isPendingValidation: boolean;
	isValidating: boolean;
}

export interface FormState {
	[ key: string ]: FieldState;
}

export interface ControllerOptions {
	initialState?: FormState;
	initialFields?: { [ key: string ]: any };
	fieldNames?: string[];
	sanitizerFunction?: ( fieldValues: any, callback: ( newFieldValues: any ) => void ) => void;
	validatorFunction?: (
		fieldValues: any,
		callback: ( error: any, fieldErrors: any ) => void
	) => void;
	skipSanitizeAndValidateOnFieldChange?: boolean;
	loadFunction?: ( callback: ( error: any, fieldValues: any ) => void ) => void;
	onNewState: ( newState: FormState ) => void;
	onError: ( error: any ) => void;
	debounceWait?: number;
	hideFieldErrorsOnChange?: boolean;
}

export class Controller {
	constructor( options: ControllerOptions );
	getInitialState(): FormState;
	handleFieldChange( change: { name: string; value: any; hideError?: boolean } ): void;
	handleSubmit( onComplete: ( hasErrors: boolean ) => void ): void;
	sanitize(): void;
	validate(): void;
	resetFields( fieldValues: { [ key: string ]: any } ): void;
}

export function getFieldValue( formState: FormState, fieldName: string ): any;
export function setFieldsValidating( formState: FormState ): FormState;
export function setFieldErrors(
	formState: FormState,
	fieldErrors: { [ key: string ]: string[] },
	hideFieldErrorsOnChange: boolean
): FormState;
export function getErrorMessages( formState: FormState ): string[];
export function getInvalidFields( formState: FormState ): { [ key: string ]: FieldState };
export function getFieldErrorMessages(
	formState: FormState,
	fieldName: string
): string[] | undefined;
export function hasErrors( formState: FormState ): boolean;
export function isFieldDisabled( formState: FormState, fieldName: string ): boolean;
export function isFieldInvalid( formState: FormState, fieldName: string ): boolean;
export function isFieldPendingValidation( formState: FormState, fieldName: string ): boolean;
export function isFieldValidating( formState: FormState, fieldName: string ): boolean;
export function getAllFieldValues( formState: FormState ): { [ key: string ]: any };
export function isSubmitButtonDisabled( formState: FormState ): boolean;
export function isFieldValid( formState: FormState, fieldName: string ): boolean;
export function isFieldPossiblyValid( formState: FormState, fieldName: string ): boolean;
export function showFieldValidationLoading( formState: FormState, fieldName: string ): boolean;
export function createInitialFormState( fieldValues: { [ key: string ]: any } ): FormState;
export function createNullFieldValues( fieldNames: string[] ): { [ key: string ]: null };
export function initializeFields(
	formState: FormState,
	fieldValues: { [ key: string ]: any }
): FormState;
export function changeFieldValue(
	formState: FormState,
	name: string,
	value: any,
	hideFieldErrorsOnChange: boolean
): FormState;

declare const _default: {
	Controller: typeof Controller;
	getFieldValue: typeof getFieldValue;
	setFieldsValidating: typeof setFieldsValidating;
	setFieldErrors: typeof setFieldErrors;
	getErrorMessages: typeof getErrorMessages;
	getInvalidFields: typeof getInvalidFields;
	getFieldErrorMessages: typeof getFieldErrorMessages;
	hasErrors: typeof hasErrors;
	isFieldDisabled: typeof isFieldDisabled;
	isFieldInvalid: typeof isFieldInvalid;
	isFieldPendingValidation: typeof isFieldPendingValidation;
	isFieldValidating: typeof isFieldValidating;
	getAllFieldValues: typeof getAllFieldValues;
	isSubmitButtonDisabled: typeof isSubmitButtonDisabled;
	isFieldValid: typeof isFieldValid;
	isFieldPossiblyValid: typeof isFieldPossiblyValid;
	showFieldValidationLoading: typeof showFieldValidationLoading;
	createInitialFormState: typeof createInitialFormState;
	createNullFieldValues: typeof createNullFieldValues;
	initializeFields: typeof initializeFields;
	changeFieldValue: typeof changeFieldValue;
};

export default _default;