Commit 91e3428459042b54a225956aca7e3252b2d28c0f

Authored by ww
1 parent 2349e0d6

fix: 修复枚举项可以重复

1 <script setup lang="ts"> 1 <script setup lang="ts">
2 import { Button, Tooltip } from 'ant-design-vue'; 2 import { Button, Tooltip } from 'ant-design-vue';
3 - import { nextTick, ref, unref, watch } from 'vue'; 3 + import { computed, nextTick, ref, unref, watch } from 'vue';
4 import { useForm, BasicForm } from '/@/components/Form'; 4 import { useForm, BasicForm } from '/@/components/Form';
5 import { Specs } from '/@/api/device/model/modelOfMatterModel'; 5 import { Specs } from '/@/api/device/model/modelOfMatterModel';
6 import { Icon } from '/@/components/Icon'; 6 import { Icon } from '/@/components/Icon';
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 import { FormActionType } from '../../../types/form'; 8 import { FormActionType } from '../../../types/form';
9 import { buildUUID } from '/@/utils/uuid'; 9 import { buildUUID } from '/@/utils/uuid';
10 import { DataTypeEnum } from '/@/enums/objectModelEnum'; 10 import { DataTypeEnum } from '/@/enums/objectModelEnum';
  11 + import { isNullOrUnDef } from '/@/utils/is';
11 12
12 const props = defineProps<{ disabled?: boolean; value?: Specs[] }>(); 13 const props = defineProps<{ disabled?: boolean; value?: Specs[] }>();
13 14
@@ -29,10 +30,27 @@ @@ -29,10 +30,27 @@
29 item.formActionType = el as unknown as FormActionType; 30 item.formActionType = el as unknown as FormActionType;
30 }; 31 };
31 32
  33 + const getEnumsLimit = computed(() => unref(enumsListElRef).length >= 100);
  34 +
  35 + const hasSameEnum = ref(false);
  36 +
  37 + const validateSameEnum = () => {
  38 + const value = getFieldsValue();
  39 + hasSameEnum.value = false;
  40 + const values = value.map((item) => item.value).filter((value) => !isNullOrUnDef(value));
  41 + const names = value.map((item) => item.name).filter((value) => !isNullOrUnDef(value));
  42 +
  43 + if (values.length !== new Set(values).size || names.length !== new Set(names).size) {
  44 + hasSameEnum.value = true;
  45 + }
  46 + };
  47 +
32 const validate = async () => { 48 const validate = async () => {
  49 + if (unref(hasSameEnum)) throw Error('存在相同的枚举');
33 for (const enumElItem of unref(enumsListElRef)) { 50 for (const enumElItem of unref(enumsListElRef)) {
34 await enumElItem.formActionType?.validate?.(); 51 await enumElItem.formActionType?.validate?.();
35 } 52 }
  53 + validateSameEnum();
36 }; 54 };
37 55
38 const getFieldsValue = () => { 56 const getFieldsValue = () => {
@@ -107,6 +125,7 @@ @@ -107,6 +125,7 @@
107 @register="registerForm" 125 @register="registerForm"
108 class="enums-form" 126 class="enums-form"
109 :disabled="disabled" 127 :disabled="disabled"
  128 + @field-value-change="validateSameEnum"
110 > 129 >
111 <template #division> 130 <template #division>
112 <div>~</div> 131 <div>~</div>
@@ -122,7 +141,12 @@ @@ -122,7 +141,12 @@
122 </Button> 141 </Button>
123 </section> 142 </section>
124 </main> 143 </main>
125 - <Button type="link" @click="handleAddEnums" :disabled="disabled">+添加枚举项</Button> 144 + <div v-if="hasSameEnum" class="text-red-400">枚举项中存在相同的参数值或参数描述</div>
  145 + <Tooltip title="枚举项最多创建 100 个">
  146 + <Button type="link" @click="handleAddEnums" :disabled="disabled || getEnumsLimit">
  147 + +添加枚举项
  148 + </Button>
  149 + </Tooltip>
126 </section> 150 </section>
127 </template> 151 </template>
128 152