@mobx-ecosystem/mobx-form
Version:
provides the ability to use forms with validation in MobX stores
93 lines (92 loc) • 2.84 kB
TypeScript
import { FieldOptionsType, FormErrors, FormServiceValuesType, FormValues, IForm, KeyParams, MethodOptions, ValidationType, ValueType } from './types';
export declare class FormService<T extends FormServiceValuesType> implements IForm<T> {
fields: T;
validationSchema?: unknown;
onSubmit?: () => Promise<unknown>;
constructor(fields: T, validationSchema?: unknown);
setOnSubmit: (onSubmit: () => Promise<unknown>) => void;
submit: () => Promise<unknown>;
/***
* Validate the form
*
* *Configure this method with configureForm from mobx-form
*/
readonly validate: (type?: ValidationType) => Promise<void>;
setValidationSchema: (validationSchema: unknown) => void;
/**
* Return field keys
*/
get keys(): string[];
/**
* Check each field if its isValid = true
*/
get isValid(): boolean;
/**
* Check each field if its isTouched = true
*/
get isTouched(): boolean;
/**
* Check if isTouched = true && isValid = true
*/
get canBeSubmitted(): boolean;
/**
* always true if the form service is empty
*/
get disabled(): boolean;
/**
*
* @returns Object of field values
*/
getValues: () => FormValues<ValueType<T>>;
private getValue;
/**
* Set fields by this
*/
setFieldsByThis: (obj: any) => void;
private bypassFields;
private setValidationToFields;
private getFieldsByKeys;
/**
* Set object to init values by form service keys
*/
setInitValues: (values: Partial<FormValues<T>>, { validate }?: {
validate?: boolean | undefined;
}) => void;
/**
* Set object to values by form service keys
*/
setValues: (values: Partial<FormValues<T>>, { validate }?: MethodOptions) => void;
private resetValidationErrors;
/**
* Set field errors to undefined
*/
resetErrors: () => void;
private setValidationError;
/**
* Set errors for fields
* @param errors object of string which provides errors for fields
*/
setErrors(error: Partial<FormErrors<T>>, validationType?: ValidationType): void;
/**
* Set field values to init values
*/
setAsInit: () => void;
/**
* Reset fields to their own initial values
*/
reset: (keyParams?: KeyParams<keyof T>, { validate }?: MethodOptions) => void;
/**
* Clear fields to their first/constructor initial values
*/
clear: (keyParams?: KeyParams<keyof T>) => void;
/**
* Pass true to the property 'disabled'
*/
disable: (keyParams?: KeyParams<keyof T>) => void;
/**
* Pass false to the property 'disabled'
*/
enable: (keyParams?: KeyParams<keyof T>) => void;
touch: () => void;
setDisabledFn: (disabledFn: FieldOptionsType<T>['disabledFn']) => void;
}