element-plus/packages/dropdown/src/dropdown.vue

242 lines
5.5 KiB
Vue
Raw Normal View History

2020-11-04 11:24:36 +08:00
<template>
<el-popper
ref="triggerVnode"
v-model:visible="visible"
:placement="placement"
:effect="effect"
:manual-mode="true"
:trigger="[trigger]"
popper-class="el-dropdown__popper"
2020-11-04 11:24:36 +08:00
>
<template #default>
<slot name="dropdown"></slot>
</template>
<template #trigger>
<div class="el-dropdown">
<slot v-if="!splitButton" name="default">
</slot>
<template v-else>
<el-button-group>
<el-button
:size="dropdownSize"
:type="type"
@click="handlerMainButtonClick"
>
<slot name="default"></slot>
</el-button>
<el-button
:size="dropdownSize"
:type="type"
class="el-dropdown__caret-button"
>
<i class="el-dropdown__icon el-icon-arrow-down"></i>
</el-button>
</el-button-group>
</template>
</div>
</template>
</el-popper>
</template>
2020-08-28 11:44:04 +08:00
<script lang='ts'>
import {
defineComponent,
provide,
getCurrentInstance,
ref,
computed,
watch,
onMounted,
2020-09-21 15:39:53 +08:00
ComponentPublicInstance,
2020-08-28 11:44:04 +08:00
} from 'vue'
import { on, addClass, removeClass } from '@element-plus/utils/dom'
import { Button as ElButton, ButtonGroup as ElButtonGroup } from '@element-plus/button'
2020-11-04 11:24:36 +08:00
import { Popper as ElPopper } from '@element-plus/popper'
2020-08-28 11:44:04 +08:00
import { useDropdown } from './useDropdown'
export default defineComponent({
name: 'ElDropdown',
components: {
ElButton,
ElButtonGroup,
2020-11-04 11:24:36 +08:00
ElPopper,
2020-08-28 11:44:04 +08:00
},
props: {
trigger: {
type: String,
default: 'hover',
},
type: String,
size: {
type: String,
default: '',
},
splitButton: Boolean,
hideOnClick: {
type: Boolean,
default: true,
},
placement: {
type: String,
default: 'bottom',
},
showTimeout: {
type: Number,
2020-09-21 15:39:53 +08:00
default: 150,
2020-08-28 11:44:04 +08:00
},
hideTimeout: {
type: Number,
default: 150,
},
tabindex: {
type: Number,
default: 0,
},
effect: {
type: String,
default: 'light',
},
},
emits: ['visible-change', 'click', 'command'],
2020-11-04 11:24:36 +08:00
setup(props, { emit }) {
2020-08-28 11:44:04 +08:00
const _instance = getCurrentInstance()
const { ELEMENT } = useDropdown()
const timeout = ref<Nullable<number>>(null)
const visible = ref(false)
watch(
() => visible.value,
val => {
if(val) triggerElmFocus()
if(!val) triggerElmBlur()
emit('visible-change', val)
},
)
const focusing = ref(false)
watch(
() => focusing.value,
val => {
const selfDefine = triggerElm.value
if (selfDefine) {
if (val) {
addClass(selfDefine, 'focusing')
} else {
removeClass(selfDefine, 'focusing')
}
}
},
)
2020-11-04 11:24:36 +08:00
const triggerVnode = ref<Nullable<ComponentPublicInstance>>(null)
const triggerElm = computed<Nullable<HTMLButtonElement>>(() =>{
const _: any = (triggerVnode.value?.$refs.triggerRef as HTMLElement)?.children[0] ?? {}
return !props.splitButton
? _
: _.children?.[1]
})
2020-08-28 11:44:04 +08:00
function handleClick() {
if (triggerElm.value?.disabled) return
if (visible.value) {
hide()
} else {
show()
}
}
function show() {
if (triggerElm.value?.disabled) return
timeout.value && clearTimeout(timeout.value)
timeout.value = window.setTimeout(() => {
visible.value = true
}, props.trigger === 'click' ? 0 : props.showTimeout)
}
function hide() {
if (triggerElm.value?.disabled) return
removeTabindex()
if (props.tabindex >=0) {
resetTabindex(triggerElm.value)
}
clearTimeout(timeout.value)
timeout.value = window.setTimeout(() => {
visible.value = false
}, props.trigger === 'click' ? 0 : props.hideTimeout)
}
function removeTabindex() {
triggerElm.value?.setAttribute('tabindex', '-1')
}
function resetTabindex(ele) {
removeTabindex()
ele?.setAttribute('tabindex', '0')
}
function triggerElmFocus() {
triggerElm.value?.focus?.()
}
function triggerElmBlur() {
triggerElm.value?.blur?.()
}
const dropdownSize = computed(() => props.size || ELEMENT.size)
2020-08-28 11:44:04 +08:00
function commandHandler (...args) {
emit('command', ...args)
}
provide('elDropdown', {
instance: _instance,
dropdownSize,
visible,
handleClick,
commandHandler,
show,
hide,
trigger: computed(() => props.trigger),
hideOnClick: computed(() => props.hideOnClick),
triggerElm,
})
onMounted(() => {
if (!props.splitButton) {
on(triggerElm.value, 'focus', () => {
focusing.value = true
})
on(triggerElm.value, 'blur', () => {
focusing.value = false
})
on(triggerElm.value, 'click', () => {
focusing.value = false
})
}
if (props.trigger === 'hover') {
on(triggerElm.value, 'mouseenter', show)
on(triggerElm.value, 'mouseleave', hide)
} else if (props.trigger === 'click') {
on(triggerElm.value, 'click', handleClick)
}
2020-11-04 11:24:36 +08:00
Object.assign(_instance, {
handleClick,
hide,
resetTabindex,
})
2020-08-28 11:44:04 +08:00
})
const handlerMainButtonClick = event => {
emit('click', event)
hide()
}
2020-11-04 11:24:36 +08:00
return {
visible,
dropdownSize,
handlerMainButtonClick,
triggerVnode,
}
2020-08-28 11:44:04 +08:00
},
})
</script>