forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn3Dropdown.vue
executable file
·66 lines (64 loc) · 1.56 KB
/
n3Dropdown.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<template>
<div class="{{prefixCls}}-dropdown-con">
<span v-el:trigger>
<slot name="trigger" ></slot>
</span>
<ul class="{{prefixCls}}-dropdown-menu" v-show="show" :transition="effect">
<slot></slot>
</ul>
</div>
</template>
<script>
import EventListener from './utils/EventListener'
export default {
props: {
show: {
type: Boolean,
twoWay: true
},
trigger: {
type: String,
default: 'click'
},
clickClose: {
type: Boolean,
default: false
},
effect: {
type: String,
default: 'fadeDown'
},
prefixCls: {
type: String,
default: 'n3'
}
},
methods: {
toggleDropdown () {
this.show = !this.show
}
},
ready () {
let el = this.$el
let triger = this.$els.trigger.children[0]
if (this.trigger === 'click') {
this._clickEvent = EventListener.listen(triger, 'click', this.toggleDropdown)
this._closeEvent = EventListener.listen(window, 'click', (e) => {
if (!this.clickClose && !el.contains(e.target)) this.show = false
})
} else if (this.trigger === 'hover') {
this._mouseenterEvent = EventListener.listen(triger, 'mouseenter', () => {
this.show = true
})
this._closeEvent = EventListener.listen(this.$el, 'mouseleave', () => {
this.show = false
})
}
},
beforeDestroy () {
if (this._closeEvent) this._closeEvent.remove()
if (this._clickEvent) this._clickEvent.remove()
if (this._mouseenterEvent) this._mouseenterEvent.remove()
}
}
</script>