form-chain-effect-engine
Version:
A dependency-driven form effect engine for React + Ant Design
46 lines (45 loc) • 1.5 kB
TypeScript
import { FormInstance } from "antd";
export interface Chain {
source: string;
path: string[];
isStopped?: boolean;
isSkipped?: boolean;
redirectTarget?: string;
stop?: () => void;
stopPropagation?: () => void;
}
export type EffectFn<EA = Record<string, any>> = (changedValue: any, allValues: Record<string, any>, chain: Chain, effectActions?: EA) => void;
export type EffectConfig<EA> = {
dependents?: string[];
effect?: EffectFn<EA>;
} & Record<string, any>;
export interface FormChainEffectMap<EA = Record<string, any>> {
[field: string]: EffectConfig<EA>;
}
export interface UseFormChainEffectEngineOptions<EA = Record<string, any>> {
enableAdvancedControl?: boolean;
debugLog?: boolean;
effectActions?: EA;
}
export interface ChainControllerOptions {
enableAdvancedControl: boolean;
trigger: (field: string, chain: Chain, visited: Set<string>) => void;
}
export type TriggerFn = (field: string, chain: Chain, visited: Set<string>, overrideValue?: any) => void;
export interface UseFormChainEffectEngineConfig {
form: FormInstance;
config: FormChainEffectMap;
options?: UseFormChainEffectEngineOptions;
onEffectResult?: (options: onEffectResultOptions) => void;
}
export interface onEffectResultOptions {
fieldName: string;
field: {
dependents?: string[];
effect?: EffectFn<Record<string, any>>;
};
result: any;
chain: Chain;
currentVal: any;
allValues: Record<string, any>;
}