Skip to content

Commit

Permalink
优化代码,处理多选情况下忽略默认选择第一项
Browse files Browse the repository at this point in the history
  • Loading branch information
think-gem committed Dec 4, 2024
1 parent 875e7b3 commit 09a8772
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/components/Form/src/components/JeeSiteSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
return {
showSearch: true,
optionFilterProp: 'label',
fieldNames: {
value: 'value',
label: 'label',
},
...unref(attrs),
...(props as Recordable),
};
Expand Down Expand Up @@ -116,6 +120,30 @@
},
);

watch(
() => optionsRef.value,
() => {
// 如果没有给初始值,并不允许清空选择项和非多选的时候,默认选择第一个选项
if (
!state.value &&
!getAttrs.value.allowClear &&
getAttrs.value.mode != 'multiple' &&
optionsRef.value.length > 0
) {
const fieldNames = getAttrs.value.fieldNames;
const firstValue = optionsRef.value[0];
if (props.labelInValue) {
state.value = {
label: firstValue[fieldNames.label],
value: firstValue[fieldNames.value],
};
} else {
state.value = firstValue[fieldNames.value];
}
}
},
);

onMounted(async () => {
if (props.options && props.options.length > 0) {
optionsRef.value = props.options;
Expand All @@ -124,23 +152,6 @@
await fetch();
isFirstLoad.value = true;
}
// 如果没有给初始值,并不允许清空选择项的时候,默认选择第一个选项
if (!state.value && !getAttrs.value.allowClear && optionsRef.value.length > 0) {
const fieldNames = getAttrs.value.fieldNames || {
label: 'label',
value: 'value',
options: 'options',
};
const firstValue = optionsRef.value[0];
if (props.labelInValue) {
state.value = {
label: firstValue[fieldNames.label],
value: firstValue[fieldNames.value],
};
} else {
state.value = firstValue[fieldNames.value];
}
}
});

async function fetch() {
Expand Down

0 comments on commit 09a8772

Please sign in to comment.