😎优化字典组件
This commit is contained in:
parent
801a31dfba
commit
b39592fdcd
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Loading…
Reference in New Issue
Block a user