tabs.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { computed, ref } from "vue";
  2. export function useTabs({ config, Form }: { config: ClForm.Config; Form: Vue.Ref<any> }) {
  3. // 选中
  4. const active = ref<string | undefined>();
  5. // 列表
  6. const list = computed(() => {
  7. return get()?.props?.labels || [];
  8. });
  9. // 获取选项
  10. function getItem(value: any) {
  11. return list.value.find((e) => e.value == value);
  12. }
  13. // 是否已加载
  14. function isLoaded(value: any) {
  15. const d = getItem(value);
  16. return d?.lazy ? d.loaded : true;
  17. }
  18. // 加载后
  19. function onLoad(value: any) {
  20. const d = getItem(value);
  21. d!.loaded = true;
  22. }
  23. // 查找分组
  24. function toGroup(opts: { config: ClForm.Config; prop: string; refs: any }) {
  25. if (active.value) {
  26. let name;
  27. // 查找标签上绑定的数据
  28. const el = opts.refs.form.querySelector(`[data-prop="${opts.prop}"]`);
  29. // 各自判断
  30. if (el) {
  31. name = el?.getAttribute("data-group");
  32. } else {
  33. function deep(d: ClForm.Item) {
  34. if (d.prop == opts.prop) {
  35. name = d.group;
  36. } else {
  37. if (d.children) {
  38. d.children.forEach(deep);
  39. }
  40. }
  41. }
  42. config.items.forEach(deep);
  43. }
  44. set(name);
  45. }
  46. }
  47. // 获取参数
  48. function get() {
  49. return config.items.find((e) => e.type === "tabs");
  50. }
  51. // 设置参数
  52. function set(data: any) {
  53. active.value = data;
  54. }
  55. // 清空
  56. function clear() {
  57. // 清空选中
  58. active.value = undefined;
  59. // 清空加载状态
  60. list.value.forEach((e) => {
  61. if (e.lazy && e.loaded) {
  62. e.loaded = undefined;
  63. }
  64. });
  65. }
  66. // 切换
  67. function change(value: any, isValid = true) {
  68. return new Promise((resolve: Function, reject: Function) => {
  69. function next() {
  70. active.value = value;
  71. resolve();
  72. }
  73. if (isValid) {
  74. let isError = false;
  75. const arr = config.items
  76. .filter((e) => e.group == active.value && !e._hidden && e.prop)
  77. .map((e) => {
  78. return new Promise((r: Function) => {
  79. // 验证表单
  80. Form.value.validateField(e.prop, (valid: string) => {
  81. if (valid) {
  82. isError = true;
  83. }
  84. r(valid);
  85. });
  86. });
  87. });
  88. Promise.all(arr).then((msg) => {
  89. if (isError) {
  90. reject(msg.filter(Boolean));
  91. } else {
  92. next();
  93. }
  94. });
  95. } else {
  96. next();
  97. }
  98. });
  99. }
  100. // 合并
  101. function mergeProp(item: ClForm.Item) {
  102. const d = get();
  103. if (d && d.props) {
  104. const { mergeProp, labels = [] } = d.props;
  105. if (mergeProp) {
  106. const t = labels.find((e) => e.value == item.group);
  107. if (t && t.name) {
  108. item.prop = `${t.name}-${item.prop}`;
  109. }
  110. }
  111. }
  112. }
  113. return {
  114. active,
  115. list,
  116. isLoaded,
  117. onLoad,
  118. get,
  119. set,
  120. change,
  121. clear,
  122. mergeProp,
  123. toGroup
  124. };
  125. }