fix(components): [select-v2] label display error when re-rendering (#18128)

* fix(components): [select-v2] label display error when re-rendering

* fix: update
This commit is contained in:
btea 2024-09-10 15:53:11 +08:00 committed by GitHub
parent 1d26af3066
commit c4f7e27bdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -716,7 +716,7 @@ const useSelect = (props: ISelectV2Props, emit) => {
menuRef.value.scrollToItem(index)
}
const getOption = (value) => {
const getOption = (value: any, cachedOptions?: Option[]) => {
// match the option with the given value, if not found, create a new option
const selectValue = getValueKey(value)
@ -725,6 +725,15 @@ const useSelect = (props: ISelectV2Props, emit) => {
return option
}
if (cachedOptions && cachedOptions.length) {
const option = cachedOptions.find(
(option) => getValueKey(getValue(option)) === selectValue
)
if (option) {
return option
}
}
return {
[aliasProps.value.value]: value,
[aliasProps.value.label]: value,
@ -734,11 +743,12 @@ const useSelect = (props: ISelectV2Props, emit) => {
const initStates = () => {
if (props.multiple) {
if ((props.modelValue as Array<any>).length > 0) {
const cachedOptions = states.cachedOptions.slice()
states.cachedOptions.length = 0
states.previousValue = props.modelValue.toString()
for (const value of props.modelValue) {
const option = getOption(value)
const option = getOption(value, cachedOptions)
states.cachedOptions.push(option)
}
} else {