form.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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): void => {
  25. this.formRef.value!.addField(prop);
  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. removeError = (prop: string): void => {
  45. this.formRef.value!.removeError(prop);
  46. };
  47. // 清除所有错误信息
  48. clearErrors = (): void => {
  49. this.formRef.value!.clearErrors();
  50. };
  51. // 获取字段规则
  52. getRule = (prop: string): ClFormRule[] => {
  53. return this.formRef.value!.getRule(prop);
  54. };
  55. // 验证单个规则
  56. validateRule = (value: any | null, rule: ClFormRule): string | null => {
  57. return this.formRef.value!.validateRule(value, rule);
  58. };
  59. // 清除所有验证
  60. clearValidate = (): void => {
  61. this.formRef.value!.clearValidate();
  62. };
  63. // 验证单个字段
  64. validateField = (prop: string): string | null => {
  65. return this.formRef.value!.validateField(prop);
  66. };
  67. // 验证整个表单
  68. validate = (callback: (valid: boolean, errors: ClFormValidateError[]) => void): void => {
  69. this.formRef.value!.validate(callback);
  70. };
  71. // 检查字段是否存在错误
  72. isError = (prop: string): boolean => {
  73. return this.formRef.value!.getError(prop) != "";
  74. };
  75. }
  76. class FormItem {
  77. public formItemRef = ref<ClFormItemComponentPublicInstance | null>(null);
  78. public isError: ComputedRef<boolean>;
  79. constructor() {
  80. const { isError } = new Form();
  81. if (this.formItemRef.value == null) {
  82. const ClFormItem = useParent<ClFormItemComponentPublicInstance>("cl-form-item");
  83. if (ClFormItem != null) {
  84. this.formItemRef.value = ClFormItem;
  85. }
  86. }
  87. // 监听表单字段是否验证错误
  88. this.isError = computed<boolean>(() => {
  89. if (this.formItemRef.value == null) {
  90. return false;
  91. }
  92. return isError(this.formItemRef.value.prop);
  93. });
  94. }
  95. }
  96. export const useForm = (): Form => {
  97. return new Form();
  98. };
  99. export const useFormItem = (): FormItem => {
  100. return new FormItem();
  101. };