mobx-easy-form
Version:
Simple and performant form library built with MobX
58 lines (57 loc) • 1.85 kB
TypeScript
export declare type ValidationFn<ValueType, ParsedType = ValueType> = (value: ValueType) => {
error?: undefined;
parsed: ParsedType;
} | {
error: Error | string;
parsed?: undefined;
};
declare type ValidationSchema<ParsedType> = {
validateSync(value: any, options: {
abortEarly: boolean;
}): ParsedType;
};
export declare type CreateFieldArgs<ValueType, ParsedType = ValueType> = {
id: string;
initialValue: ValueType;
initialError?: undefined | string;
form: {
actions: {
add(field: any): any;
submit(): any;
};
};
} & ({
validate?: undefined;
validationSchema?: undefined;
} | {
validate: ValidationFn<ValueType, ParsedType>;
validationSchema?: undefined;
} | {
validate?: undefined;
validationSchema: ValidationSchema<ParsedType> | undefined;
});
export declare function createField<ValueType = any, ParsedType = ValueType>({ id, initialValue, initialError, form, ...validationProps }: CreateFieldArgs<ValueType, ParsedType>): Field<ValueType, ParsedType>;
export interface Field<ValueType, ParsedType = ValueType> {
state: {
id: string;
errorOverride: undefined | string;
value: ValueType;
isFocused: boolean;
wasEverFocused: boolean;
wasEverBlurred: boolean;
};
computed: {
readonly parsed: ParsedType | undefined;
readonly isDirty: boolean;
readonly error: undefined | string;
readonly ifWasEverFocusedThenError: undefined | string;
readonly ifWasEverBlurredThenError: undefined | string;
};
actions: {
onFocus(): void;
onChange(value: ValueType): void;
onBlur(): void;
setError(value: string | undefined): void;
};
}
export {};