[release-2.19] fix: resolve incorrect display of options in formkit selector with async data (#6631)

This is an automated cherry-pick of #6629

/assign LIlGG

```release-note
解决当 formkit select 组件中的 options 延迟设置时无法正常回显的问题。
```
pull/6633/head
Takagi 2024-09-10 16:42:10 +08:00 committed by GitHub
parent 527a49e3b8
commit a5ff816af5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 8 deletions

View File

@ -43,9 +43,12 @@ const filterOptions = computed(() => {
}
const options = props.options.filter((option) => {
return option.label
.toLocaleLowerCase()
.includes(keyword.toLocaleLowerCase());
if (option.label) {
return option.label
.toLocaleLowerCase()
.includes(keyword.toLocaleLowerCase());
}
return false;
});
if (props.allowCreate) {

View File

@ -555,6 +555,18 @@ watch(
}
);
watch(
() => options.value,
async (newOptions) => {
if (newOptions && newOptions.length > 0) {
const selectedOption = await fetchSelectedOptions();
if (selectedOption) {
selectOptions.value = selectedOption;
}
}
}
);
const enableAutoSelect = () => {
if (!selectProps.autoSelect) {
return false;
@ -575,11 +587,6 @@ watch(
() => options.value,
async (newOptions) => {
if (newOptions && newOptions.length > 0) {
const selectedOption = await fetchSelectedOptions();
if (selectedOption) {
selectOptions.value = selectedOption;
}
if (enableAutoSelect()) {
// Automatically select the first option when the selected value is empty.
const autoSelectedOption = getAutoSelectedOption();