@korautils/forms
Version:

366 lines (354 loc) • 13 kB
TypeScript
import * as react_jsx_runtime from 'react/jsx-runtime';
import * as yupUtil from 'yup';
import { Control, FieldValues, UseFormWatch, UseFormSetValue, UseFormReset, FieldErrors } from 'react-hook-form';
import React$1, { ReactNode } from 'react';
import { AxiosResponse, AxiosError } from 'axios';
import { SxProps, Theme } from '@mui/material/index';
import { ButtonProps as ButtonProps$1 } from '@mui/material';
type YUP_TYPES = 'string' | 'number' | 'boolean' | 'date' | 'object' | 'array';
interface ValidationDetail<T> {
value: T;
message?: string;
}
interface YupRule {
name?: string;
type: YUP_TYPES;
required?: ValidationDetail<boolean>;
isUrl?: ValidationDetail<boolean>;
isEmail?: ValidationDetail<boolean>;
pattern?: ValidationDetail<string | RegExp | undefined>;
min?: ValidationDetail<number>;
max?: ValidationDetail<number>;
}
type YupSchema = yupUtil.AnySchema;
declare class YupBuilder {
schema: Record<string, yupUtil.AnySchema>;
constructor();
static newInstance(): YupBuilder;
static createRule({ type, required, isUrl, isEmail, pattern, min, max, }: YupRule): yupUtil.AnySchema | undefined;
addRule(params: YupRule): this;
pushRule(name: string, rule: yupUtil.AnySchema): void;
static string: typeof yupUtil.string;
static number: typeof yupUtil.number;
static date: typeof yupUtil.date;
static object: typeof yupUtil.object;
static boolean: typeof yupUtil.bool;
build(): yupUtil.ObjectSchema<{
[x: string]: never;
}, yupUtil.AnyObject, {
[x: string]: any;
}, "">;
}
type FORMAT_TYPES = 'FORM_DATA' | 'JSON' | 'PLAIN_TEXT' | 'NO_BODY';
type ChildrenElement = ((formHandler?: FormHandlerProps) => ReactNode) | string | React.ReactElement | React.ReactElement[] | ReactNode;
type HTTP_METHODS = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
type RESPONSE_TYPE_VALUES = 'json' | 'arraybuffer' | 'blob' | 'document' | 'text' | 'stream';
interface ObjectKeys {
[key: string]: string | number;
}
interface TableItem {
id: string;
name: string;
value: string;
}
interface ApiRequestConfig<T = any> {
url: string;
method: HTTP_METHODS;
headers?: ObjectKeys;
params?: ObjectKeys;
data?: any;
timeout?: number;
format?: FORMAT_TYPES;
responseType?: 'json' | 'arraybuffer' | 'blob' | 'document' | 'text' | 'stream';
auth?: {
username: string;
password: string;
};
onUploadProgress?: (progressEvent: ProgressEvent) => void;
onDownloadProgress?: (progressEvent: ProgressEvent) => void;
extraValues?: {
tabValue?: 'params' | 'body' | 'headers';
format?: RESPONSE_TYPE_VALUES;
};
dataTemplate?: DateTemplate;
showSucessAlert?: boolean;
statusMessages?: StatusMessages;
onSuccess?: (response: AxiosResponse<T>) => void;
onError?: (error: AxiosError) => void;
}
interface StatusMessages {
200?: string;
201?: string;
204?: string;
400?: string;
401?: string;
403?: string;
404?: string;
405?: string;
409?: string;
422?: string;
500?: string;
502?: string;
503?: string;
DEFAULT?: string;
}
interface ParserObject {
value: string;
parser?: (value: any) => any;
}
type TemplateValue<T = string> = T extends string ? string | number : T extends {
value: string;
} ? ParserObject : never;
interface DateTemplate {
[key: string]: DateTemplate | TemplateValue | TemplateValue<ParserObject> | DateTemplate[];
}
interface KoraHandleResponse<T = any> {
onSuccessHandle?: (response: AxiosResponse<T>) => void;
onErrorHandle?: (response: AxiosError) => void;
}
type ELEMENT_TYPE = 'INPUT' | 'TEXTFIELD' | 'NUMBER' | 'PRICE' | 'SELECT' | 'BASIC_SELECT' | 'RADIO_GROUP' | 'CHECKBOX' | 'EMAIL' | 'PASSWORD' | 'DATE_FIELD' | 'TIME_FIELD' | 'TIME_RANGE_FIELD' | 'TIME_DESKTOP' | 'TIME_MOBILE' | 'DATE_PICKER' | 'DATE_PICKER_MODAL' | 'RANGE_PICKER' | 'NORMAL_FILE' | 'FILE_DROP' | 'TABS' | 'GROUP_BUTTON' | 'BOX' | 'STEPPER' | 'BUTTON';
type INPUT_VARIANTS = 'LABELING' | 'UNLABELING';
interface ItemOptionProps {
id?: string;
value?: string;
label?: ChildrenElement;
icon?: ChildrenElement;
variant?: 'contained' | 'outlined';
type?: 'API' | 'DEFAULT';
children?: ChildrenElement;
visible?: boolean;
}
interface StepItem {
label?: ChildrenElement;
body?: any;
}
type CONDITIONAL_OPERATORS = '==' | '>=' | '<=' | '>' | '<' | '!=';
type GenerateStringUnion<T> = Extract<{
[Key in keyof T]: true extends T[Key] ? Key : never;
}[keyof T], string>;
type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
type Overwrite<T, U> = DistributiveOmit<T, keyof U> & U;
type OverridableStringUnion<T extends string | number, U = {}> = GenerateStringUnion<Overwrite<Record<T, true>, U>>;
interface TextFieldPropsColorOverrides {
}
interface ElementProps {
id?: string;
name?: string;
label?: string;
labelVariant?: INPUT_VARIANTS;
type?: React.InputHTMLAttributes<unknown>['type'];
size?: 'small' | 'medium';
value?: string | object;
options?: Array<ItemOptionProps>;
onChange?: any;
renderInput?: any;
placeholder?: string;
style?: React.CSSProperties;
children?: ChildrenElement;
error?: boolean;
helperText?: string;
showNone?: boolean;
steps?: Array<StepItem>;
api?: ApiRequestConfig;
renderProps?: RenderProps;
disabled?: boolean;
formHandler?: FormHandlerProps;
className?: string;
required?: boolean;
minLength?: number | string;
maxLength?: number | string;
pattern?: string;
inputProps?: {
maxLength: number | string;
};
InputProps?: any;
multiple?: boolean;
limitTags?: number;
renderGroup?: any;
groupBy?: any;
onInput?: any;
renderOption?: any;
disableCloseOnSelect?: boolean;
color?: OverridableStringUnion<'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning', TextFieldPropsColorOverrides>;
}
type OriginElement = 'DEMO' | 'DEFAULT';
interface BuildProps {
field?: any;
origin?: OriginElement;
formHandler?: FormHandlerProps;
}
interface FormHandlerProps {
control?: Control<FieldValues, any>;
watch?: UseFormWatch<FieldValues>;
setValue?: UseFormSetValue<FieldValues>;
reset?: UseFormReset<FieldValues>;
errors?: FieldErrors<FieldValues>;
}
interface RenderProps {
label: string;
value: string;
includeAll?: boolean;
}
interface BaseRule {
fieldName: string;
operator: CONDITIONAL_OPERATORS;
value: any;
}
interface ExtraRule {
visible?: boolean;
disabled?: boolean;
action?: any;
}
type CompleteRule = BaseRule & ExtraRule;
type PartialVisibleRule = {
visible: boolean;
} & Partial<Omit<BaseRule, 'fieldName' | 'operator' | 'value'>>;
type PartialActionRule = {
action: any;
} & Partial<Omit<BaseRule, 'fieldName' | 'operator' | 'value'>>;
type PartialDisabledRule = {
disabled: boolean;
} & Partial<Omit<BaseRule, 'fieldName' | 'operator' | 'value'>>;
type ElementRule = CompleteRule | PartialVisibleRule | PartialActionRule | PartialDisabledRule;
type CommonProps = Pick<ElementProps, 'id' | 'name' | 'label' | 'disabled' | 'error' | 'helperText' | 'labelVariant' | 'size' | 'style' | 'value' | 'onChange' | 'className' | 'required' | 'color'>;
type InputProps = CommonProps & Pick<ElementProps, 'placeholder' | 'type' | 'inputProps' | 'InputProps' | 'onInput' | 'color'>;
type PasswordProps = InputProps;
type EmailProps = InputProps;
type TabsProps = Pick<ElementProps, 'options' | 'name' | 'value' | 'onChange' | 'formHandler' | 'className' | 'disabled'>;
type BoxProps = Pick<ElementProps, 'children' | 'style' | 'formHandler'> & {
withHandler?: boolean;
};
type SelectProps = CommonProps & Pick<ElementProps, 'api' | 'options' | 'renderProps'>;
type BasicSelectProps = CommonProps & Pick<ElementProps, 'options' | 'showNone'>;
interface ButtonProps extends Omit<ButtonProps$1, 'variant'> {
label?: string;
variant?: 'text' | 'outlined' | 'contained';
icon?: React.ReactElement;
circular?: boolean;
unshadow?: boolean;
tooltipTitle?: string;
}
type KeyProp = keyof ElementProps;
type VISIBILITY = 'VISIBLE' | 'GONE' | 'INVISIBLE';
declare class ElementBuilder {
id: string;
name?: string;
private gridColumn?;
private gridRow?;
private visibility;
private component;
private fullWidth?;
private elementType;
private _withControl?;
private props;
private rules;
yupRules: Array<YupSchema | undefined>;
protected formBuilderInstance?: FormBuilder;
constructor(props?: ElementProps);
static newElement(): ElementBuilder;
withControl(): this;
setFormBuilder(formBuilder: FormBuilder): this;
hasControl(): boolean | undefined;
getGridColumn(): number | undefined;
getGridRow(): number | undefined;
setGridColumn(gridColumn: number): this;
setGridRow(gridRow: number): this;
getRules(): ElementRule[][];
private instance;
private getBasicProps;
setStyles(styles: React$1.CSSProperties): this;
setVisibility(visibility: VISIBILITY): this;
getVisibility(): VISIBILITY;
setFullWidth(): this;
getColumns(): number | undefined;
getRows(): number | undefined;
setColumns(columns: number): this;
setRows(rows: number): void;
textField(props?: InputProps): this;
email(props?: EmailProps): this;
autocomplete(props?: SelectProps): this;
selectBasic(props?: BasicSelectProps): this;
groupButton(props?: ElementProps): this;
password(props?: PasswordProps): this;
inputPrice(props?: ElementProps): this;
select(props?: ElementProps): this;
checkbox(props?: ElementProps): this;
radioGroup(props?: ElementProps): this;
dateField(props?: ElementProps): this;
timeField(props?: ElementProps): this;
timeRangeField(props?: ElementProps): this;
timeDesktop(props?: ElementProps): this;
timeMobile(props?: ElementProps): this;
datePicker(props?: ElementProps): this;
datePickerModal(props?: ElementProps): this;
rangePicker(props?: ElementProps): this;
tabs(props?: TabsProps): this;
stepper(props?: ElementProps): this;
box(props?: BoxProps): this;
button(props: ButtonProps): this;
getType(): ELEMENT_TYPE;
getName(): string;
getProps(): ElementProps;
getProp(key: KeyProp): any;
withProps(props?: ElementProps): this;
private checkOptions;
getClassNames(): Array<string>;
getStyles(): SxProps<Theme>;
addRule(rule: ElementRule[]): this;
addValidation(params: YupRule): this;
addProps(props: ElementProps): this;
build({ field, origin, formHandler }?: BuildProps): react_jsx_runtime.JSX.Element;
}
type FormHandlerType = FormHandlerProps | (() => FormHandlerType) | undefined;
declare class FormBuilder {
private id?;
private title;
private cols?;
private gridGap?;
private colWidth?;
private items;
private api?;
private schema;
private initialData?;
private handleOnChange?;
formHandler?: FormHandlerType;
ssr?: boolean;
showControls?: boolean;
static baseUrl?: string;
private currentElement?;
constructor(initialData?: any);
/** Queda pendiente sincronizar el initial data de los subformuarios con el del formulario principal */
static newForm(initialData?: any): FormBuilder;
getApi(): ApiRequestConfig<any> | undefined;
setApi<D = any>(config: ApiRequestConfig<D>): this;
setFormHandler(formHandler: FormHandlerType): this;
getInitialData(): any;
setInitialData(data: any): this;
getFormHandler(): FormHandlerType;
setHandleOnChange(handle: any): this;
getHandleOnChange(): any;
getId(): string | undefined;
getCols(): number | undefined;
getColWidth(): number | undefined;
setCols(cols: number): this;
setColWidth(colWidth: number): this;
addItem(item: ElementBuilder): this;
getItems(): ElementBuilder[];
newElement(): ElementBuilder;
private endElement;
private buildSchema;
private processYupRules;
getSchema(): YupBuilder;
setGridGap(gap: number | string): this;
getGridGap(): string | number | undefined;
setSsr(ssr: boolean): this;
build(): react_jsx_runtime.JSX.Element;
}
declare class ElementFactory {
static renderElement(type: ELEMENT_TYPE, props: ElementProps, origin?: OriginElement): react_jsx_runtime.JSX.Element;
private static parseNumValidationValue;
private static processValidations;
static composeComponent(type: ELEMENT_TYPE, props: ElementProps): ElementBuilder;
static createComponent(type: ELEMENT_TYPE, props: ElementProps): ElementBuilder;
}
export { type ApiRequestConfig, ElementBuilder, ElementFactory, FormBuilder, type FormHandlerProps, type HTTP_METHODS, type KoraHandleResponse, type ObjectKeys, type TableItem };