react-hooks-dynamic-form
Version:
Dynamic form with react hooks
54 lines (53 loc) • 1.44 kB
TypeScript
import { Field, FieldValueType, FieldSettings } from "./field";
/**
* Form data with fields map
*/
export interface FormData {
[fieldName: string]: Field;
}
/**
* Field values map { [name]: value}
*/
export interface FormValues {
[fieldName: string]: FieldValueType;
}
/**
* Form API client interface
*/
export interface FormApi {
/**
* indicating whether form data is initialized
*/
isInit: boolean;
/**
* current field values { key: value }
*/
values: FormValues;
/**
* update form field value
*/
setFieldValue: (name: string, value: FieldValueType) => void;
/**
* validate a field
*/
validateField: (name: string) => void;
/**
* validate all form fields
*/
validateForm: () => boolean;
/**
* return field input error if there is any
*/
getFieldInputError: (fieldName: string) => string | null;
/**
* reset form to initial settings
*/
resetForm: () => void;
}
/**
* Form API library entry point to generate and reuse all Form API function
* @param fields field definitions array
* @param defaultSettings common settings for all fields
* @param remoteValues in case fields values are remote (server, parents components ...)
*/
export declare const useFormApi: (fields: FieldSettings[], defaultSettings?: Partial<FieldSettings> | undefined, remoteValues?: FormValues | undefined) => FormApi;