😎优化字典组件

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

View File

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