UNPKG

@altiore/form

Version:

Form helper for building powerful forms

134 lines (133 loc) 6.16 kB
import { ButtonHTMLAttributes, MouseEventHandler, MutableRefObject } from 'react'; /** * "button|checkbox|file|hidden|image|password|radio|reset|submit|text|password */ export declare enum FieldType { ARRAY = "array", BOOLEAN = "boolean", CHECKBOX = "checkbox", ENUM = "enum", NUMBER = "number", FLOAT = "float", PHONE = "phone", PASSWORD = "password", TEXT = "text", EMAIL = "email", SELECT = "select", SELECT_MULTIPLE = "select-multiple", DATE = "date", DATETIME = "datetime-local", SECRET_CURRENCY = "secret-currency-field-multiple-by-10000" } export declare type InputType = 'button' | 'checkbox' | 'color' | 'date' | 'datetime' | 'datetime-local' | 'email' | 'file' | 'hidden' | 'image' | 'month' | 'number' | 'password' | 'radio' | 'range' | 'reset' | 'search' | 'submit' | 'text' | 'tel' | 'time' | 'url' | 'week'; export declare type NamedFieldProps = { fieldArrayState: FieldArrayState; formState: FormContextState; }; export declare enum InsertPosition { BEFORE = 0, AFTER = 1 } export interface ListItem<Item extends Record<string, any> = Record<string, any>> { key: string; itemKey: number; append: (initialValue?: Item) => void; prepend: (initialValue?: Item) => void; remove: () => void; } export declare type ListInterface<Item extends Record<string, any> = Record<string, any>> = { map: (arg: (el: ListItem, index: number) => JSX.Element) => JSX.Element[]; add: ((initialValue?: Item) => void) & MouseEventHandler<unknown>; remove: (index: number) => void; }; export declare type RegisterField = (fieldName: string, fieldType: FieldType, defaultValue?: any, validators?: Array<ValidateFunc>) => () => void; export declare type SetErrors = (name: string, errors: string[] | undefined) => void; export declare enum FieldMetaName { NAME = "name", DEFAULT_VALUE = "defaultValue", ERRORS = "errors", ITEMS = "items", ITEMS_PREV = "itemsPrev", SET_ERRORS = "setErrors", FIELD_TYPE = "fieldType", IS_UNTOUCHED = "isUntouched", IS_INVALID = "isInvalid", ERROR = "error", VALIDATORS = "validators", WARNING = "warning", WARNINGS = "warnings" } export declare enum IgnoredProp { INPUT_PROPS = "inputProps", FIELD_PROPS = "fieldProps" } export declare type FieldMeta<ValueType = any> = { [FieldMetaName.NAME]: string; [FieldMetaName.DEFAULT_VALUE]?: ValueType; [FieldMetaName.ERRORS]: string[]; [FieldMetaName.WARNINGS]: string[]; [FieldMetaName.ITEMS]?: number[]; [FieldMetaName.ITEMS_PREV]?: number[]; [FieldMetaName.SET_ERRORS]: (errors: string[], force?: boolean, isWarnings?: boolean) => void; [FieldMetaName.FIELD_TYPE]?: FieldType; [FieldMetaName.IS_UNTOUCHED]?: boolean; [FieldMetaName.IS_INVALID]: boolean; [FieldMetaName.ERROR]?: string; [FieldMetaName.WARNING]?: string; [FieldMetaName.VALIDATORS]: Array<ValidateFunc>; }; export declare type FormContextState = { fields: Record<string, FieldMeta>; formRef: MutableRefObject<HTMLFormElement>; isSubmitting: boolean; registerField: RegisterField; setItems: (fieldName: string, setItems: (i: number[]) => number[], getErrors: (i: number[]) => string[], defValue?: any) => void; onSubmit: (evt?: any) => void; hideErrorsInXSeconds?: false | number; }; export interface FieldArrayState { name: string; } export declare type ValidateFunc<ValueType = any> = (value: ValueType, fieldName?: string, getFieldValueByName?: (name: string) => any) => string | undefined; export declare type ReusableValidator<CheckType, ValueType = any> = (getMessage?: null | undefined | string | ((value: ValueType, forCheck: CheckType) => string), forCheck?: CheckType) => ValidateFunc<ValueType>; export declare type FieldOuterProps<FormState extends Record<string, any> = Record<string, any>> = { name: keyof FormState; defaultValue?: any; validate?: ValidateFunc<FormState[keyof FormState]> | Array<ValidateFunc<FormState[keyof FormState]>>; }; export declare type FieldResProps<FormState extends Record<string, any> = Record<string, any>, FieldCustomProps extends Record<string, any> = Record<string, any>> = FieldCustomProps & FieldOuterProps<FormState>; export declare type FieldInputProps<ValueType, El extends HTMLElement = HTMLElement> = { defaultChecked?: ValueType extends boolean ? ValueType : undefined; defaultValue?: ValueType extends boolean ? undefined : ValueType; multiple?: true; name: string; ref: MutableRefObject<El | undefined> | any; type: InputType; value?: 'on' | undefined; }; export declare type FieldInnerProps<Input extends HTMLElement = HTMLInputElement, ValueType = any> = { [IgnoredProp.FIELD_PROPS]: FieldMeta<ValueType>; [IgnoredProp.INPUT_PROPS]: FieldInputProps<ValueType, Input>; }; export declare type FieldProps<FieldCustomProps extends Record<string, any> = { name: string; }, Input extends HTMLElement = HTMLInputElement, ValueType = any> = FieldCustomProps & FieldInnerProps<Input, ValueType>; export declare type FieldOptions = { fieldType?: FieldType; }; export declare type FieldArrayHiddenProps<Item extends Record<string, any> = Record<string, any>> = { listRef: MutableRefObject<HTMLElement>; list: ListInterface<Item>; }; export declare type FieldArrayInnerProps<Item extends Record<string, any> = Record<string, any>> = FieldArrayHiddenProps<Item> & FieldMeta<Item>; export declare type FieldArrayProps<FieldCustomProps extends Record<string, any> = Record<string, unknown>, ArrayItemProps extends Record<string, any> = Record<string, any>> = FieldCustomProps & FieldArrayInnerProps<ArrayItemProps>; export interface SubmitInnerProps<El = HTMLButtonElement> extends ButtonHTMLAttributes<El> { isInvalid: boolean; isSubmitting: boolean; isUntouched: boolean; } export interface SubmitOuterProps { children?: any; onSubmit?: (values: any) => Promise<any>; } export declare type SubmitProps<CustomSubmitProps extends Record<string, any> = Record<string, any>, El = HTMLButtonElement> = SubmitInnerProps<El> & CustomSubmitProps;