@innoways/hooks
Version:
drip-form通用hooks
273 lines (272 loc) • 9.55 kB
TypeScript
/// <reference types="react" />
import { Dispatch } from "react";
import { Action, Map, Get, ClosestEdge, UiSchema, DataSchema, GetKey, TitleUi, OnChange, GetTypeKey } from "@innoways/utils";
import Ajv from "ajv/dist/2019";
import { ErrorObject } from "ajv/dist/2019";
/*
* Field表单onChange生成 hooks
* 该hooks会自动根据
* @Author: jiangxiaowei
* @Date: 2020-05-20 20:31:51
* @Last Modified by: jiangxiaowei
* @Last Modified time: yyyy-05-dd 15:10:43
*/
import { Moment } from "moment";
type SetType = "data" | "uiSchema" | "dataSchema";
type SetFn = (value: unknown) => void;
type StaticValue = undefined | null | string | number | boolean | Record<string, unknown> | Array<unknown>;
type Set = (fieldKey: string, type: SetType, value: SetFn | StaticValue) => void;
type DeleteField = (fieldKey: string, cb?: () => void) => void;
type AddField = (params: {
fieldKey: string;
closestEdge: ClosestEdge;
unitedSchema: Map;
overFieldKey: string;
shouldDelete: boolean;
cb: () => void;
}) => void;
type Merge = (fieldKey: string, type: SetType, value: StaticValue) => void;
declare const useSchema: ({ uiSchema, dataSchema, typeMap, formData, dispatch, prevFormData, prevUiSchema, prevDataSchema, prevTypeMap }: {
dispatch: Dispatch<Action>;
uiSchema: UiSchema;
dataSchema: DataSchema;
typeMap: Map;
formData: Map;
prevFormData: Map;
prevUiSchema: UiSchema;
prevDataSchema: DataSchema;
prevTypeMap: Map;
}) => {
get: Get;
set: Set;
deleteField: DeleteField;
addField: AddField;
merge: Merge;
};
type Errors = ErrorObject[];
type ErrorsMap = Record<string, string>;
type ValidateReturn = {
pass: boolean;
errors: Errors;
errorsMap: ErrorsMap;
formData: Record<string, unknown>;
};
type Validate = (param: {
schema: Record<string, unknown> | any;
formData: Record<string, unknown>;
ajv: Ajv;
visibleFieldKey?: string[];
customProps?: string[];
}) => ValidateReturn;
type Params = {
dataSchema: {
[propName: string]: any;
};
formData: {
[propName: string]: any;
};
ajv: any;
dispatch: Dispatch<Action>;
visibleFieldKey: string[];
};
/**
*
* @param {json} schema 校验数据的json schema
* @param {object} formData 校验数据
* @param {Ajv} ajv 校验器
* @param visibleFieldKey
* return {
* pass {bol} 是否校验通过
* error {} 校验错误信息
* }
*/
declare const validate: Validate;
/**
*
* @param delay 校验防抖,延迟多少ms校验
* @returns
*/
declare const useValidate: (delay?: number) => (arg0: Params) => void;
interface MarginType {
style: string;
stringArr: string[];
}
type TitleWidth = string | number;
/**
*
* @param titleUi ui中的title字段
* @param type ui的type类型
* @returns
*/
declare const useTitle: (titleUi: TitleUi, type?: string | undefined) => TitleUi;
type UndefinedComponentFn = (param: {
theme?: string;
type?: string;
fieldKey: string;
}) => string;
type GlobalOptions = Partial<{
// (一般情况无需设置)true: formData、unitedSchema变化会触发reducer的reload false:formData和unitedSchema变化不会触发重新渲染
reload: boolean;
// 使用未定义的组件(主题中查找不到该组件) tip:提示无法找到主题xxx中的xx组件,请确认是否倒入 hidden:直接隐藏该组件,控制台提示
undefinedComponent: {
type: "tips";
value: string | UndefinedComponentFn;
} | {
type: "console";
consoleType: "log" | "warn" | "error";
value: string | UndefinedComponentFn;
};
// ajv校验防抖延迟时间 单位:ms(避免数据频繁变化多次调用ajv校验函数) 默认不延迟 注意:大部分情况无需设置延迟
ajvValidateDelay: number;
fieldTitleEditable: boolean | undefined;
selectedFieldKey: string | null | undefined;
setFieldTitleChange: (title: string, fieldKey: string) => void | undefined;
showTitleWithIcon: Function | undefined | null;
getDeletedColumnsIndex?: Function | undefined | null;
}>;
// 默认全局配置
declare const defaultGlobalOptions: GlobalOptions;
declare const globalOptionsContext: import("react").Context<Partial<{
reload: boolean;
undefinedComponent: {
type: "tips";
value: string | UndefinedComponentFn;
} | {
type: "console";
consoleType: "log" | "warn" | "error";
value: string | UndefinedComponentFn;
};
ajvValidateDelay: number;
fieldTitleEditable: boolean | undefined;
selectedFieldKey: string | null | undefined;
setFieldTitleChange: (title: string, fieldKey: string) => void | undefined;
showTitleWithIcon: Function | undefined | null;
getDeletedColumnsIndex?: Function | undefined | null;
}>>;
// 返回上一次的value值
declare const useGlobalOptions: () => GlobalOptions;
type TGlobalState = Partial<{
// 暂存的错误信息
stageErrors: Record<string, string>;
apiJson: Record<string, any>;
webModalForPlaceAutoCompleteField: string | null;
setGlobalState: (state: TGlobalState) => void;
}>;
// 默认全局状态
declare const defaultGlobalState: TGlobalState;
declare const globalStateContext: import("react").Context<Partial<{
stageErrors: Record<string, string>;
apiJson: Record<string, any>;
webModalForPlaceAutoCompleteField: string | null;
setGlobalState: (state: TGlobalState) => void;
}>>;
// 返回上一次的value值
declare const useGlobalState: () => TGlobalState;
type FormatMomentDataProps = {
value: string | [
Moment,
Moment
];
format: string;
};
declare const formatMap: {
[propName: string]: (...args: any[]) => any;
};
/* 例子
import { useField } from 'hooks'
const myComponent = ({fieldKey,onChange})=>{
const onchange = useField(fieldKey,onChange)
<Input
onChange={_onChange}
/>
}
*/
type SpecialFormatKeys = keyof typeof formatMap | "isDelete";
type UseFieldR = (...args: any[]) => void;
declare const useField: ({ fieldKey, onChange, options, asyncValidate, prevFieldData, fieldData, getKey }: {
fieldKey: string;
getKey: GetKey;
onChange?: OnChange | undefined;
options?: Partial<Record<SpecialFormatKeys, boolean> & {
[index: number]: any;
[propName: string]: any;
format: FormatMomentDataProps["format"];
}> | undefined;
asyncValidate?: {
type: "change" | "click";
fn: (arg0: any) => any;
} | undefined;
prevFieldData?: any;
fieldData?: any;
}, dispatch: Dispatch<Action>) => UseFieldR;
declare const useModal: (defaultStatus: boolean) => [
boolean,
() => void,
() => void
];
declare const useRefProp: (props: any[]) => {
current: null | any[];
};
// 返回上一次的value值
declare const usePrevious: (value: any) => any;
/**
* 重写useCallback(保证函数即使dependencies依赖改变。也不会重新生成)
* 解决因为useCallback的依赖频繁变化导致useCallback缓存效果很差甚至影响性能的问题
* fn 函数
* dependencies 依赖数组
*/
declare const useEventCallback: (fn: (...args: any[]) => void, dependencies: any[]) => (...args: any[]) => void;
type ClickFuc = () => void;
type ReturnType<T> = [
boolean,
T,
T
];
declare const useClickOne: () => ReturnType<ClickFuc>;
// import useCusDispatch from './useCusDispatch'
/**
* 查询表单数据的hook
* @param {object} param {
* options 表单数据
* queryFunc 表单query函数
* requestCache 是否每次都调查询接口
* fieldKey 表单的key
* }
*/
type Arg0 = {
options: any[];
queryFunc?: (...args: any[]) => any;
requestCache?: boolean;
fieldKey: string;
getKey: GetKey;
};
declare const useQuery: ({ options, queryFunc, requestCache, fieldKey, getKey }: Arg0, dispatch: Dispatch<Action>) => (...args: any[]) => void;
type AddItem = (order: number, item: unknown) => void;
type DeltItem = (order: number) => void;
type ArrayMove = (oldIndex: number, newIndex: number) => void;
declare const useArray: ({ fieldKey, dispatch, fieldData }: {
fieldKey: string;
dispatch: Dispatch<Action>;
fieldData: Array<unknown>;
}) => {
addItem: AddItem;
deltItem: DeltItem;
arrayMove: ArrayMove;
};
declare const useContainer: ({ fieldKey, dispatch }: {
fieldKey: string;
dispatch: Dispatch<Action>;
}) => void;
declare const useGetKey: (typeMap: Map) => {
getTypeKey: GetTypeKey;
getKey: GetKey;
};
// 必填校验模式 default:必填同JSON schema reuqired关键字校验模式(会清空当前表单项) empty: 使用@jdfed/utils isEmpty判断(不会清空当前表单项)
type RequiredMode = "default" | "empty";
declare const RequiredModeContext: import("react").Context<RequiredMode>;
// 返回上一次的value值
declare const useRequiredModeContext: () => RequiredMode;
declare const useCountryStateCityOptions: any;
declare const useScreenSize: () => number;
export { useModal, useRefProp, usePrevious, useEventCallback, useClickOne, useValidate, validate, useField, useQuery, useArray, useContainer, useSchema, useGetKey, useTitle, useRequiredModeContext, RequiredModeContext, useGlobalOptions, globalOptionsContext, defaultGlobalOptions, useGlobalState, globalStateContext, defaultGlobalState, useCountryStateCityOptions, useScreenSize };
export type { Get, Set, Merge, DeleteField, AddField, GetKey, SetType, Validate, ErrorsMap, MarginType, TitleWidth, GlobalOptions, UndefinedComponentFn, TGlobalState, UseFieldR };