form.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { computed, ref, type ComputedRef } from "vue";
  2. import type { ClFormRule, ClFormValidateError } from "../types";
  3. import { useParent } from "@/cool";
  4. export class Form {
  5. public formRef = ref<ClFormComponentPublicInstance | null>(null);
  6. public disabled: ComputedRef<boolean>;
  7. constructor() {
  8. // 获取 cl-form 实例
  9. if (this.formRef.value == null) {
  10. const ClForm = useParent<ClFormComponentPublicInstance>("cl-form");
  11. if (ClForm != null) {
  12. this.formRef.value = ClForm;
  13. }
  14. }
  15. // 监听表单是否禁用
  16. this.disabled = computed<boolean>(() => {
  17. if (this.formRef.value == null) {
  18. return false;
  19. }
  20. return this.formRef.value.disabled;
  21. });
  22. }
  23. // 注册表单字段
  24. addField = (prop: string, rules: ClFormRule[]): void => {
  25. this.formRef.value!.addField(prop, rules);
  26. };
  27. // 注销表单字段
  28. removeField = (prop: string): void => {
  29. this.formRef.value!.removeField(prop);
  30. };
  31. // 获取字段值
  32. getValue = (prop: string): any | null => {
  33. return this.formRef.value!.getValue(prop);
  34. };
  35. // 设置字段错误信息
  36. setError = (prop: string, error: string): void => {
  37. this.formRef.value!.setError(prop, error);
  38. };
  39. // 获取字段错误信息
  40. getError = (prop: string): string => {
  41. return this.formRef.value!.getError(prop);
  42. };
  43. // 获取所有错误信息
  44. getErrors = async (): Promise<ClFormValidateError[]> => {
  45. return this.formRef.value!.getErrors();
  46. };
  47. // 移除字段错误信息
  48. removeError = (prop: string): void => {
  49. this.formRef.value!.removeError(prop);
  50. };
  51. // 清除所有错误信息
  52. clearErrors = (): void => {
  53. this.formRef.value!.clearErrors();
  54. };
  55. // 获取字段规则
  56. getRule = (prop: string): ClFormRule[] => {
  57. return this.formRef.value!.getRule(prop);
  58. };
  59. // 设置字段规则
  60. setRule = (prop: string, rules: ClFormRule[]): void => {
  61. this.formRef.value!.setRule(prop, rules);
  62. };
  63. // 移除字段规则
  64. removeRule = (prop: string): void => {
  65. this.formRef.value!.removeRule(prop);
  66. };
  67. // 验证单个规则
  68. validateRule = (value: any | null, rule: ClFormRule): string | null => {
  69. return this.formRef.value!.validateRule(value, rule);
  70. };
  71. // 清除所有验证
  72. clearValidate = (): void => {
  73. this.formRef.value!.clearValidate();
  74. };
  75. // 验证单个字段
  76. validateField = (prop: string): string | null => {
  77. return this.formRef.value!.validateField(prop);
  78. };
  79. // 验证整个表单
  80. validate = (
  81. callback: (valid: boolean, errors: ClFormValidateError[]) => void | Promise<void>
  82. ): void => {
  83. this.formRef.value!.validate(callback);
  84. };
  85. // 检查字段是否存在错误
  86. isError = (prop: string): boolean => {
  87. return this.formRef.value!.getError(prop) != "";
  88. };
  89. }
  90. class FormItem {
  91. public formItemRef = ref<ClFormItemComponentPublicInstance | null>(null);
  92. public isError: ComputedRef<boolean>;
  93. constructor() {
  94. const { isError } = new Form();
  95. if (this.formItemRef.value == null) {
  96. const ClFormItem = useParent<ClFormItemComponentPublicInstance>("cl-form-item");
  97. if (ClFormItem != null) {
  98. this.formItemRef.value = ClFormItem;
  99. }
  100. }
  101. // 监听表单字段是否验证错误
  102. this.isError = computed<boolean>(() => {
  103. if (this.formItemRef.value == null) {
  104. return false;
  105. }
  106. return isError(this.formItemRef.value.prop);
  107. });
  108. }
  109. }
  110. export const useForm = (): Form => {
  111. return new Form();
  112. };
  113. export const useFormItem = (): FormItem => {
  114. return new FormItem();
  115. };