iview/src/components/dropdown/dropdown.vue

142 lines
4.8 KiB
Vue
Raw Normal View History

<template>
<div
:class="[prefixCls]"
2016-12-04 17:04:48 +08:00
v-clickoutside="handleClose"
@mouseenter="handleMouseenter"
2016-12-04 17:04:48 +08:00
@mouseleave="handleMouseleave">
2017-03-03 17:46:09 +08:00
<div :class="[prefixCls + '-rel']" ref="reference" @click="handleClick"><slot></slot></div>
<transition :name="transition">
<Drop v-show="currentVisible" :placement="placement" ref="drop"><slot name="list"></slot></Drop>
</transition>
</div>
</template>
<script>
import Drop from '../select/dropdown.vue';
import clickoutside from '../../directives/clickoutside';
2017-04-27 16:55:48 +08:00
import { oneOf, findComponentUpward } from '../../utils/assist';
const prefixCls = 'ivu-dropdown';
export default {
2016-12-04 13:35:09 +08:00
name: 'Dropdown',
directives: { clickoutside },
components: { Drop },
props: {
trigger: {
validator (value) {
return oneOf(value, ['click', 'hover', 'custom']);
},
default: 'hover'
},
2016-12-04 13:35:09 +08:00
placement: {
validator (value) {
2016-12-04 13:35:09 +08:00
return oneOf(value, ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end']);
},
2016-12-04 13:35:09 +08:00
default: 'bottom'
},
visible: {
type: Boolean,
default: false
}
},
2016-12-04 17:04:48 +08:00
computed: {
transition () {
return ['bottom-start', 'bottom', 'bottom-end'].indexOf(this.placement) > -1 ? 'slide-up' : 'fade';
}
},
data () {
return {
2017-03-03 17:46:09 +08:00
prefixCls: prefixCls,
currentVisible: this.visible
2016-12-25 22:49:42 +08:00
};
},
2017-03-03 17:46:09 +08:00
watch: {
visible (val) {
this.currentVisible = val;
},
currentVisible (val) {
if (val) {
this.$refs.drop.update();
} else {
this.$refs.drop.destroy();
}
this.$emit('on-visible-change', val);
}
},
methods: {
handleClick () {
if (this.trigger === 'custom') return false;
if (this.trigger !== 'click') {
return false;
}
2017-03-03 17:46:09 +08:00
this.currentVisible = !this.currentVisible;
},
handleMouseenter () {
if (this.trigger === 'custom') return false;
if (this.trigger !== 'hover') {
return false;
}
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
2017-03-03 17:46:09 +08:00
this.currentVisible = true;
}, 250);
},
handleMouseleave () {
if (this.trigger === 'custom') return false;
if (this.trigger !== 'hover') {
return false;
}
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
2017-03-03 17:46:09 +08:00
this.currentVisible = false;
}, 150);
},
handleClose () {
if (this.trigger === 'custom') return false;
if (this.trigger !== 'click') {
return false;
}
2017-03-03 17:46:09 +08:00
this.currentVisible = false;
2016-12-04 14:33:06 +08:00
},
hasParent () {
2017-04-27 16:55:48 +08:00
// const $parent = this.$parent.$parent.$parent;
const $parent = findComponentUpward(this, 'Dropdown');
if ($parent) {
2016-12-04 14:33:06 +08:00
return $parent;
} else {
return false;
}
}
},
2017-03-03 17:46:09 +08:00
mounted () {
this.$on('on-click', (key) => {
2016-12-04 14:33:06 +08:00
const $parent = this.hasParent();
2017-03-03 17:46:09 +08:00
if ($parent) $parent.$emit('on-click', key);
});
this.$on('on-hover-click', () => {
2016-12-04 14:33:06 +08:00
const $parent = this.hasParent();
2016-12-04 17:04:48 +08:00
if ($parent) {
this.$nextTick(() => {
if (this.trigger === 'custom') return false;
2017-03-03 17:46:09 +08:00
this.currentVisible = false;
2016-12-04 17:04:48 +08:00
});
$parent.$emit('on-hover-click');
} else {
this.$nextTick(() => {
if (this.trigger === 'custom') return false;
2017-03-03 17:46:09 +08:00
this.currentVisible = false;
2016-12-04 17:04:48 +08:00
});
}
2017-03-03 17:46:09 +08:00
});
this.$on('on-haschild-click', () => {
2016-12-04 14:33:06 +08:00
this.$nextTick(() => {
if (this.trigger === 'custom') return false;
2017-03-03 17:46:09 +08:00
this.currentVisible = true;
2016-12-04 14:33:06 +08:00
});
const $parent = this.hasParent();
if ($parent) $parent.$emit('on-haschild-click');
2017-03-03 17:46:09 +08:00
});
2017-03-03 18:08:10 +08:00
}
2016-12-25 22:49:42 +08:00
};
</script>