mirror of
https://github.com/element-plus/element-plus.git
synced 2024-12-27 03:01:14 +08:00
106 lines
2.3 KiB
Vue
106 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">
|
|
import {
|
|
toRefs,
|
|
defineComponent,
|
|
getCurrentInstance,
|
|
onBeforeUnmount,
|
|
reactive,
|
|
nextTick,
|
|
} 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
|
|
if (select.cachedOptions.get(key) === vm && !doesSelected) {
|
|
nextTick(() => {
|
|
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>
|