@mobx-ecosystem/mobx-form
Version:
provides the ability to use forms with validation in MobX stores
100 lines (99 loc) • 3.2 kB
TypeScript
import { FieldOptionsType, FormErrors, FormServiceValuesType, FormValues, IForm, KeyParams, MethodOptions, ResetType, ValidationType, ValueType } from './types';
export declare class FormService<T extends FormServiceValuesType> implements IForm<T> {
fields: T;
validationSchema?: unknown;
onSubmit?: () => Promise<unknown>;
private onValidate?;
private onClear?;
private onReset?;
constructor(fields: T, validationSchema?: unknown);
setOnSubmit: (onSubmit: () => Promise<unknown>) => void;
setOnValidate: (onValidate: (type: ValidationType) => unknown) => void;
setOnClear: (onClear: () => void) => void;
setOnReset: (onReset: (params?: ResetType) => void) => 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: (params?: KeyParams<keyof T> & ResetType & MethodOptions) => void;
/**
* Clear fields to their first/constructor initial values
*/
clear: (params?: KeyParams<keyof T> & MethodOptions) => void;
/**
* Pass true to the property 'disabled'
* Useful for editing / readonly mode when the fields has their own business logic
*/
disable: (params?: KeyParams<keyof T>) => void;
/**
* Pass false to the property 'disabled'
*/
enable: (params?: KeyParams<keyof T>) => void;
touch: () => void;
setDisabledFn: (disabledFn: FieldOptionsType<T>['disabledFn']) => void;
}