f7e1cc7e111fa9b92c40197f0e5913b29dca2558ec9bbdf2ab9c61d925c5b59ada8e6986e6b5a1e758c7836297007959d1701c6b8de58e725d946e34e97e8b 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type { SetupContext, UnwrapRef } from 'vue';
  2. import type { RuleItem, ValidateError, ValidateFieldsError } from 'async-validator';
  3. import type { ComponentSize } from 'element-plus/es/constants';
  4. import type { Arrayable, FieldPath } from 'element-plus/es/utils';
  5. import type { MaybeRef } from '@vueuse/core';
  6. import type { FormItemProp, FormItemProps, FormItemValidateState } from './form-item';
  7. import type { FormEmits, FormProps } from './form';
  8. import type { useFormLabelWidth } from './utils';
  9. export type FormLabelWidthContext = ReturnType<typeof useFormLabelWidth>;
  10. export interface FormItemRule extends RuleItem {
  11. trigger?: Arrayable<string>;
  12. }
  13. export type FormRules<T extends MaybeRef<Record<string, any> | string> = string> = Partial<Record<UnwrapRef<T> extends string ? UnwrapRef<T> : FieldPath<UnwrapRef<T>>, Arrayable<FormItemRule>>>;
  14. export type FormValidationResult = Promise<boolean>;
  15. export type FormValidateCallback = (isValid: boolean, invalidFields?: ValidateFieldsError) => Promise<void> | void;
  16. export interface FormValidateFailure {
  17. errors: ValidateError[] | null;
  18. fields: ValidateFieldsError;
  19. }
  20. export type FormContext = FormProps & UnwrapRef<FormLabelWidthContext> & {
  21. emit: SetupContext<FormEmits>['emit'];
  22. getField: (prop: FormItemProp) => FormItemContext | undefined;
  23. addField: (field: FormItemContext) => void;
  24. removeField: (field: FormItemContext) => void;
  25. resetFields: (props?: Arrayable<FormItemProp>) => void;
  26. clearValidate: (props?: Arrayable<FormItemProp>) => void;
  27. validateField: (props?: Arrayable<FormItemProp>, callback?: FormValidateCallback) => FormValidationResult;
  28. };
  29. export interface FormItemContext extends FormItemProps {
  30. $el: HTMLDivElement | undefined;
  31. size: ComponentSize;
  32. validateMessage: string;
  33. validateState: FormItemValidateState;
  34. isGroup: boolean;
  35. labelId: string;
  36. inputIds: string[];
  37. hasLabel: boolean;
  38. fieldValue: any;
  39. propString: string;
  40. addInputId: (id: string) => void;
  41. removeInputId: (id: string) => void;
  42. validate: (trigger: string, callback?: FormValidateCallback) => FormValidationResult;
  43. resetField(): void;
  44. clearValidate(): void;
  45. }