useForm.d.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { Ref } from 'vue';
  2. import type { Callbacks, RuleError, ValidateMessages } from './interface';
  3. import type { ValidateStatus } from './FormItem';
  4. interface DebounceSettings {
  5. leading?: boolean;
  6. wait?: number;
  7. trailing?: boolean;
  8. }
  9. export interface Props {
  10. [key: string]: any;
  11. }
  12. export interface validateOptions {
  13. validateFirst?: boolean;
  14. validateMessages?: ValidateMessages;
  15. trigger?: 'change' | 'blur' | string | string[];
  16. }
  17. declare type namesType = string | string[];
  18. export interface ValidateInfo {
  19. autoLink?: boolean;
  20. required?: boolean;
  21. validateStatus?: ValidateStatus;
  22. help?: any;
  23. }
  24. export interface validateInfos {
  25. [key: string]: ValidateInfo;
  26. }
  27. declare function useForm(modelRef: Props | Ref<Props>, rulesRef?: Props | Ref<Props>, options?: {
  28. immediate?: boolean;
  29. deep?: boolean;
  30. validateOnRuleChange?: boolean;
  31. debounce?: DebounceSettings;
  32. onValidate?: Callbacks['onValidate'];
  33. }): {
  34. modelRef: Props | Ref<Props>;
  35. rulesRef: Props | Ref<Props>;
  36. initialModel: Props;
  37. validateInfos: validateInfos;
  38. resetFields: (newValues?: Props) => void;
  39. validate: <T = any>(names?: namesType, option?: validateOptions) => Promise<T>;
  40. /** This is an internal usage. Do not use in your prod */
  41. validateField: (name: string, value: any, rules: Record<string, unknown>[], option?: validateOptions) => Promise<RuleError[]>;
  42. mergeValidateInfo: (items: ValidateInfo | ValidateInfo[]) => ValidateInfo;
  43. clearValidate: (names?: namesType) => void;
  44. };
  45. export default useForm;