element-plus/packages/components/select/src/option.vue
白雾三语 5f041f47a8
fix: [select] label can be displayed correctly after selecting operation (#8589)
* fix: [select] label can be displayed correctly after selecting operation

* fix: [select] label can be displayed correctly after selecting operation

* fix: [select] modify unit test

* fix: [select] modify unit test
2022-07-20 20:47:34 +08:00

107 lines
2.3 KiB
Vue

<template>
<li
v-show="visible"
:class="[
ns.be('dropdown', 'item'),
ns.is('disabled', isDisabled),
{
selected: itemSelected,
hover,
},
]"
@mouseenter="hoverItem"
@click.stop="selectOptionClick"
>
<slot>
<span>{{ currentLabel }}</span>
</slot>
</li>
</template>
<script lang="ts">
// @ts-nocheck
import {
defineComponent,
getCurrentInstance,
nextTick,
onBeforeUnmount,
reactive,
toRefs,
} from 'vue'
import { useNamespace } from '@element-plus/hooks'
import { useOption } from './useOption'
import type { SelectOptionProxy } from './token'
export default defineComponent({
name: 'ElOption',
componentName: 'ElOption',
props: {
value: {
required: true,
type: [String, Number, Boolean, Object],
},
label: [String, Number],
created: Boolean,
disabled: {
type: Boolean,
default: false,
},
},
setup(props) {
const ns = useNamespace('select')
const states = reactive({
index: -1,
groupDisabled: false,
visible: true,
hitState: false,
hover: false,
})
const { currentLabel, itemSelected, isDisabled, select, hoverItem } =
useOption(props, states)
const { visible, hover } = toRefs(states)
const vm = getCurrentInstance().proxy
const key = (vm as unknown as SelectOptionProxy).value
select.onOptionCreate(vm as unknown as SelectOptionProxy)
onBeforeUnmount(() => {
const { selected } = select
const selectedOptions = select.props.multiple ? selected : [selected]
const doesSelected = selectedOptions.some((item) => {
return item.value === (vm as unknown as SelectOptionProxy).value
})
// if option is not selected, remove it from cache
nextTick(() => {
if (select.cachedOptions.get(key) === vm && !doesSelected) {
select.cachedOptions.delete(key)
}
})
select.onOptionDestroy(key, vm)
})
function selectOptionClick() {
if (props.disabled !== true && states.groupDisabled !== true) {
select.handleOptionSelect(vm, true)
}
}
return {
ns,
currentLabel,
itemSelected,
isDisabled,
select,
hoverItem,
visible,
hover,
selectOptionClick,
states,
}
},
})
</script>