😎优化字典组件

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 * @computed
* @returns {DictItem|DictItem[]|null} - 当前选中的字典项或字典项数组 * @returns {DictItem|DictItem[]|null} - 当前选中的字典项或字典项数组
*/ */
const currentDictItems = computed(() => { 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)) { if (Array.isArray(state.value)) {
// // /
const uniqueValues = [...new Set(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;
}); });
/** /**
@ -467,17 +477,21 @@ const handleMutex = (newValue: any, mutexConfigs: MutexConfig[]): any => {
* @function * @function
* @param {any} newValue - 新值 * @param {any} newValue - 新值
*/ */
const updateValue = (newValue: any) => { 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) { if (props.mutexConfigs && props.mutexConfigs.length > 0) {
processedValue = handleMutex(newValue, props.mutexConfigs); processedValue = handleMutex(processedValue, props.mutexConfigs);
} }
let emitValue = processedValue; let emitValue = processedValue;
if (props.multipleModel === MultipleModel.Comma) { if (props.multipleModel === MultipleModel.Comma) {
if (Array.isArray(processedValue)) { if (Array.isArray(processedValue)) {
emitValue = processedValue.length > 0 ? processedValue.sort().join(',') : []; emitValue = processedValue.length > 0 ? processedValue.sort().join(',') : '';
} else if (processedValue === null || processedValue === undefined) { } else if (processedValue === null || processedValue === undefined) {
emitValue = undefined; emitValue = undefined;
} }
@ -488,7 +502,7 @@ const updateValue = (newValue: any) => {
emitValue = undefined; emitValue = undefined;
} }
} }
console.log('更新值:', { newValue, processedValue, emitValue }); console.log('[g-sys-dict] 更新值:', { newValue, processedValue, emitValue });
state.value = processedValue; state.value = processedValue;
emit('update:modelValue', emitValue === '' || emitValue?.length === 0 ? undefined : emitValue); emit('update:modelValue', emitValue === '' || emitValue?.length === 0 ? undefined : emitValue);