😎优化字典组件

This commit is contained in:
zuohuaijun 2025-09-24 14:27:49 +08:00
parent 801a31dfba
commit b39592fdcd

View File

@ -291,15 +291,25 @@ const formattedDictData = computed(() => {
* @returns {DictItem|DictItem[]|null} - 当前选中的字典项或字典项数组
*/
const currentDictItems = computed(() => {
if (!state.value) return null;
// 0
const isEmpty = (val: any) =>
val === null ||
val === undefined ||
(Array.isArray(val) && val.length === 0) ||
(typeof val === 'string' && val.trim() === '');
if (isEmpty(state.value)) return null;
if (Array.isArray(state.value)) {
//
// /
const uniqueValues = [...new Set(state.value)];
return formattedDictData.value.filter((item) => uniqueValues.includes(item.value));
return formattedDictData.value.filter(item =>
uniqueValues.some(val => val == item.value)
);
}
return formattedDictData.value.find((item) => item.value == state.value) || null;
//
return formattedDictData.value.find(item => item.value == state.value) || null;
});
/**
@ -468,16 +478,20 @@ const handleMutex = (newValue: any, mutexConfigs: MutexConfig[]): any => {
* @param {any} newValue - 新值
*/
const updateValue = (newValue: any) => {
//
let processedValue = newValue;
//
let processedValue = Array.isArray(newValue) ? newValue : (typeof newValue === 'string' && props.multipleModel === MultipleModel.Comma)
? newValue.split(',').filter(Boolean)
: newValue;
//
if (props.mutexConfigs && props.mutexConfigs.length > 0) {
processedValue = handleMutex(newValue, props.mutexConfigs);
processedValue = handleMutex(processedValue, props.mutexConfigs);
}
let emitValue = processedValue;
if (props.multipleModel === MultipleModel.Comma) {
if (Array.isArray(processedValue)) {
emitValue = processedValue.length > 0 ? processedValue.sort().join(',') : [];
emitValue = processedValue.length > 0 ? processedValue.sort().join(',') : '';
} else if (processedValue === null || processedValue === undefined) {
emitValue = undefined;
}
@ -488,7 +502,7 @@ const updateValue = (newValue: any) => {
emitValue = undefined;
}
}
console.log('更新值:', { newValue, processedValue, emitValue });
console.log('[g-sys-dict] 更新值:', { newValue, processedValue, emitValue });
state.value = processedValue;
emit('update:modelValue', emitValue === '' || emitValue?.length === 0 ? undefined : emitValue);