@manojadams/metaforms-core
Version:
React Library for rendering dynamic forms from json schema
87 lines (86 loc) • 5.26 kB
TypeScript
import EventEmitter from "eventemitter3";
import React from "react";
import { IMetaForm, IForm, IFormField, IError, IDepdendencyItem, IElementTypes, IFnTypes, TCondition, TErrorCallback, TFieldRef, IFieldConfig, IEventPayload, IControlProps, IRequestBody, IFormData, IFooterProps } from "../constants/common-interface";
import { IField, IFormConfig, IOption, IRest, ISchema, TParam, TParamType } from "../constants/model-interfaces";
import { Rest } from "./Rest";
import { Page } from "./Page";
import { TValue } from "../constants/types";
import InitialData from "./InitialData";
/**
* This class is responsible for handling all the heavy lifting work in the forms
* @category Form handler
*/
export default class MetaForm implements IMetaForm {
private schema;
private eventEmitter;
formConfig: IFormConfig;
form: IForm;
rest: Rest;
page: Page;
footer: React.FunctionComponent<IFooterProps>;
initialData?: InitialData;
icons?: IElementTypes;
fns?: IFnTypes;
controls: IElementTypes;
controlElements: Record<string, React.FunctionComponent<IControlProps>> | undefined;
errorHandler?: TErrorCallback;
constructor(schema: ISchema, eventEmitter: EventEmitter, formConfig: IFormConfig, restConfig?: IRest);
init(initialData?: IFormData): void;
/** page functions */
getPage(): Page;
setPage(page: Page): void;
updatePage(pageNumber: number): void;
setEndOfPage(pageNumber: number | undefined): void;
resetEndOfPage(): void;
/** event emitter functions */
emit(eventType: string, payload?: IEventPayload): void;
listener(eventType: string, fn: (data: IEventPayload) => void): void;
removeListener(event: string, fn?: (data: IEventPayload) => void): void;
destroy(): void;
/** icons */
getIcon(type: string): JSX.Element | "";
setIcons(icons: IElementTypes): void;
/** rest functions */
getRestConfig(): import("../constants/model-interfaces").IConfig;
api(type: string, url: string, queryParams?: Array<TParam>, requestBodyParams?: Array<TParamType>, requestBody?: IRequestBody, requestHeaders?: Record<string, string>, currentValue?: TValue, sectionName?: string, isRemote?: boolean): Promise<any>;
getData(config: IFieldConfig, val: TValue, section: string, eventType?: string): Promise<Array<IOption>>;
/** Theme functions */
getSection(pageNumber: number): import("../constants/common-interface").IFormSection | null;
getSectionName(pageNumber: number): IField | null;
setSection(section: string): void;
initField(section: string, field: IField, value: TValue): void;
getField(section: string, field: string): IFormField;
setField(section: string, field: string, value: TValue): void;
updateField(section: string, field: string, value: TValue): void;
getFieldProp(section: string, field: string, prop: string): any;
setFieldProp(section: string, field: string, prop: string, propVal: TValue | Array<TFieldRef> | IError | FileList): void;
getFieldDisplay(section: string, field: string): boolean;
setFieldDisplay(section: string, field: string, display: boolean): void;
getFieldOptions(section: string, field: string): IOption[];
setFieldOptions(section: string, field: string, options: Array<IOption> | undefined): void;
setFieldDisabled(section: string, field: string, disabled: boolean): void;
getDependencies(section: string, field: string): any;
getChangeEvents(section: string, field: string): import("../constants/model-interfaces").IChangeEvent | import("../constants/model-interfaces").IChangeEvent[] | undefined;
applyDependencies(section: string, fields: Array<IField>): void;
handleDependencies(section: string, fieldName: string, value: TValue, fieldDisplayed: boolean): Promise<boolean>;
handleChangeEvents(gSection: string, gField: string, fieldValue?: TValue, fieldRef?: IOption): void;
setDisplayTypeFieldProp(displayType: string, section: string, field: string, resultOptions: Array<IOption>, dependency: IDepdendencyItem): void;
performDeepUpdate(section: string, field: string, value: TValue, ref: IOption): void;
/** validation functions */
setError(section: string, field: string, error: IError): void;
validate(): boolean;
parseCondition(condition: Array<TCondition>, section: string): any;
/** Function mapper */
getFn(fn: string): ((arg: TValue, ref?: IOption | undefined, formField?: IFormField | undefined) => string | number | boolean | Date | IOption[] | null | undefined) | null;
setFns(fns: IFnTypes): void;
/** Error handler */
handleError(error: Error, section: string, field: string): void;
setErrorHandler(errorHandler: TErrorCallback): void;
getControl(displayType: string): React.JSX.Element;
setControls(controls: IElementTypes): void;
getControlElements(displayType: string): React.FunctionComponent<IControlProps> | null;
setControlElements(controlElements: Record<string, React.FunctionComponent>): void;
hasFooter(): boolean;
getFooter(): React.FunctionComponent<IFooterProps>;
setFooter(footer: React.FunctionComponent<IFooterProps>): void;
}