fix: 修复多选排它异常

- 修复异常卡死的问题
This commit is contained in:
Huangzhe
2025-03-25 19:59:58 +08:00
parent 562fa7d478
commit 726c8c7f58

View File

@@ -112,7 +112,7 @@
</template>
<script setup>
import OptionAction from '@/views/Design/components/ActionCompoents/OptionAction.vue';
import { defineAsyncComponent } from 'vue';
import { defineAsyncComponent, nextTick } from 'vue';
// 是否是预览
const isPreview = defineModel('isPreview', { default: false, type: Boolean });
@@ -147,11 +147,6 @@ const element = defineModel('element', {
}
});
/**
* 控制选项,如果新增排它项, 那么就直接重新取值
* 按理说是不是 watch 更加合理
* @param names {number[]} 选中的项
*/
function handleChange(names) {
const newOption = names[names.length - 1];
// 如果names 长度小于1直接返回不处理
@@ -167,19 +162,57 @@ function handleChange(names) {
// 如果不存在 options 和 list 属性,直接返回
if (!options) return;
let newValues = [...value.value];
// 如果 names 里面有其他的排它项,就把排它项的值去除
for (const option of options) {
if (option.is_remove_other === 1 && newOption !== option.option_index) {
value.value = value.value.filter((item) => item !== option.option_index);
newValues = newValues.filter((item) => item !== option.option_index);
}
}
if (options[Number(newOption) - 1]?.is_remove_other === 1) {
value.value = [newOption];
newValues = [newOption];
}
// 使用 nextTick 确保在下一个 DOM 更新周期中修改 value
nextTick(() => {
if (JSON.stringify(value.value) !== JSON.stringify(newValues)) {
value.value = newValues;
}
});
}
/**
* 控制选项,如果新增排它项, 那么就直接重新取值
* 按理说是不是 watch 更加合理
* @param names {number[]} 选中的项
*/
// function handleChange(names) {
// const newOption = names[names.length - 1];
// // 如果names 长度小于1直接返回不处理
// if (names.length < 1) return;
//
// let options;
//
// try {
// options = element.value.options[0].options ?? element.value.list[0];
// } catch (e) {
// options = element.value.options[0];
// }
//
// // 如果不存在 options 和 list 属性,直接返回
// if (!options) return;
//
// // 如果 names 里面有其他的排它项,就把排它项的值去除
// for (const option of options) {
// if (option.is_remove_other === 1 && newOption !== option.option_index) {
// value.value = value.value.filter((item) => item !== option.option_index);
// }
// }
//
// if (options[Number(newOption) - 1]?.is_remove_other === 1) {
// value.value = [newOption];
// }
// }
const emit = defineEmits(['update:element']);
const emitValue = () => {
emit('update:element', element.value);