mobx-easy-form
Version:
Simple and performant form library built with MobX
104 lines (103 loc) • 3.75 kB
text/typescript
import { StandardSchemaV1 } from "@standard-schema/spec";
//#region src/createField.d.ts
type ValidationFn<ValueType, ParsedType = ValueType> = (value: ValueType) => {
error?: undefined;
parsed: ParsedType;
} | {
error: Error | string;
parsed?: undefined;
};
type YupLikeSchema<ParsedType> = {
validateSync(value: unknown, options?: {
abortEarly?: boolean;
}): ParsedType;
};
type ValidationSchema<ParsedType = unknown> = StandardSchemaV1<unknown, ParsedType> | YupLikeSchema<ParsedType>;
type InferParsed<Schema, Fallback> = Schema extends StandardSchemaV1<unknown, infer Out> ? Out : Schema extends YupLikeSchema<infer P> ? P : Fallback;
type CreateFieldArgs<ValueType, ParsedType = ValueType, Schema extends ValidationSchema<ParsedType> | undefined = ValidationSchema<ParsedType> | undefined> = {
id: string;
initialValue: ValueType;
initialError?: string | undefined;
form: {
actions: {
add(field: Field<any, any>): void;
submit(): unknown;
};
};
} & ({
validate?: undefined;
validationSchema?: undefined;
} | {
validate: ValidationFn<ValueType, ParsedType>;
validationSchema?: undefined;
} | {
validate?: undefined;
validationSchema: Schema;
});
declare function createField<ValueType, Schema extends ValidationSchema<unknown> | undefined = undefined, ParsedType = InferParsed<Schema, ValueType>>(args: CreateFieldArgs<ValueType, ParsedType, Schema extends ValidationSchema<ParsedType> ? Schema : undefined>): Field<ValueType, ParsedType>;
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;
};
}
//#endregion
//#region src/createForm.d.ts
type OnSubmitArg = {
fields: Record<string, Field<unknown>>;
rawValues: Record<string, unknown>;
values: Record<string, unknown>;
};
type OnSubmitFn = (props: OnSubmitArg) => unknown;
type CreateFormArgs = {
onSubmit: OnSubmitFn;
};
type Form = ReturnType<typeof createForm>;
declare function createForm({
onSubmit
}: CreateFormArgs): {
fields: Record<string, Field<unknown, unknown>>;
state: {
isSubmitting: boolean;
valuesAtLastSubmit: undefined | string;
submitCount: number;
};
computed: {
readonly isDirty: boolean;
readonly errorList: string[];
readonly isError: boolean;
readonly isValid: boolean;
readonly valueList: string;
readonly isChangedSinceLastSubmit: boolean;
};
actions: {
add(field: Field<unknown>): void;
submit: () => unknown;
};
};
//#endregion
//#region src/useForm.d.ts
declare function useForm(args: CreateFormArgs, deps?: ReadonlyArray<unknown>): Form;
//#endregion
//#region src/useField.d.ts
declare function useField<ValueType, Schema extends ValidationSchema<unknown> | undefined = undefined, ParsedType = InferParsed<Schema, ValueType>>(args: CreateFieldArgs<ValueType, ParsedType, Schema extends ValidationSchema<ParsedType> ? Schema : undefined>, deps?: ReadonlyArray<unknown>): Field<ValueType, ParsedType>;
//#endregion
export { type CreateFieldArgs, type CreateFormArgs, type Field, type Form, type InferParsed, type OnSubmitArg, type OnSubmitFn, type ValidationFn, type ValidationSchema, createField, createForm, useField, useForm };
//# sourceMappingURL=index.d.mts.map