formular
Version:
Build forms in React. Easy-Peasy!
62 lines • 2.06 kB
TypeScript
import Field, { FieldOpts, Validator } from './Field';
export declare const eventNames: {
readonly stateChange: "state change";
readonly attachFields: "attach fields";
readonly detachFields: "detach fields";
readonly forceUpdate: "force update";
readonly change: "change";
readonly focus: "focus";
readonly blur: "blur";
readonly submit: "submit";
};
export declare type FormEventName = typeof eventNames[keyof typeof eventNames];
declare type FormFieldOpts<T> = {
[K in keyof T]: FieldOpts<T[K]> | Validator[];
};
export declare type FormOpts<T extends {}> = {
name?: string;
fields: FormFieldOpts<T>;
initialValues?: Partial<{
[K in keyof T]: T[K];
}>;
};
declare type FormFields<T extends {}> = {
[K in keyof T]: Field<T[K]>;
};
export declare type FormErrors<T extends {}> = {
[K in keyof T]: any;
};
declare type State = {
isValid: boolean;
isChanged: boolean;
isValidating: boolean;
isSubmitting: boolean;
isSubmitted: boolean;
};
declare class Form<FieldValues extends {}> {
private _events;
name?: string;
opts: FormOpts<FieldValues>;
fields: FormFields<FieldValues>;
state: State;
constructor(opts: FormOpts<FieldValues>);
private _attachFields;
attachFields(fieldOpts: Partial<FormFieldOpts<FieldValues>>): void;
detachFields(fieldNames: Array<keyof FieldValues>): void;
forceUpdate(): void;
setState(values: Partial<State>): void;
setValues(values: Partial<FieldValues>): void;
getValues(): FieldValues;
unsetValues(): void;
setErrors(errors: FormErrors<FieldValues>): void;
getErrors(): FormErrors<FieldValues> | null;
validate(): Promise<boolean>;
submit(): Promise<{
values: FieldValues;
errors: FormErrors<FieldValues> | null;
}>;
on(eventName: FormEventName, handler: Function): void;
off(eventName: FormEventName, handler: Function): void;
}
export default Form;
//# sourceMappingURL=Form.d.ts.map