element/packages/dropdown/src/dropdown.vue

126 lines
3.0 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<script>
2016-10-13 03:12:24 +00:00
import Clickoutside from 'element-ui/src/utils/clickoutside';
2016-10-27 09:31:22 +00:00
import Emitter from 'element-ui/src/mixins/emitter';
import ElButton from 'element-ui/packages/button';
import ElButtonGroup from 'element-ui/packages/button-group';
2016-07-27 06:15:02 +00:00
export default {
name: 'ElDropdown',
2016-10-28 02:54:01 +00:00
componentName: 'ElDropdown',
2016-10-27 09:31:22 +00:00
mixins: [Emitter],
2016-08-22 04:02:09 +00:00
directives: { Clickoutside },
components: {
ElButton,
ElButtonGroup
},
2016-07-27 06:15:02 +00:00
props: {
trigger: {
type: String,
default: 'hover'
},
menuAlign: {
type: String,
default: 'end'
2016-10-05 03:03:44 +00:00
},
2016-10-12 12:27:51 +00:00
type: String,
size: String,
2016-12-20 10:42:44 +00:00
splitButton: Boolean,
hideOnClick: {
type: Boolean,
default: true
}
2016-07-27 06:15:02 +00:00
},
data() {
return {
timeout: null,
visible: false
};
},
2016-10-05 03:03:44 +00:00
mounted() {
2016-10-31 04:40:30 +00:00
this.$on('menu-item-click', this.handleMenuItemClick);
2016-10-05 03:03:44 +00:00
this.initEvent();
},
watch: {
visible(val) {
this.broadcast('ElDropdownMenu', 'visible', val);
}
},
2016-07-27 06:15:02 +00:00
methods: {
show() {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.visible = true;
}, 250);
},
hide() {
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.visible = false;
}, 150);
},
handleClick() {
2016-10-05 03:03:44 +00:00
this.visible = !this.visible;
},
initEvent() {
let { trigger, show, hide, handleClick, splitButton } = this;
let triggerElm = splitButton
2016-10-11 15:47:24 +00:00
? this.$refs.trigger.$el
2016-10-05 03:03:44 +00:00
: this.$slots.default[0].elm;
if (trigger === 'hover') {
triggerElm.addEventListener('mouseenter', show);
triggerElm.addEventListener('mouseleave', hide);
2016-10-13 05:54:02 +00:00
let dropdownElm = this.$slots.dropdown[0].elm;
dropdownElm.addEventListener('mouseenter', show);
dropdownElm.addEventListener('mouseleave', hide);
2016-10-05 03:03:44 +00:00
} else if (trigger === 'click') {
triggerElm.addEventListener('click', handleClick);
2016-07-27 06:15:02 +00:00
}
2016-10-31 04:40:30 +00:00
},
handleMenuItemClick(command, instance) {
2016-12-20 10:42:44 +00:00
if (this.hideOnClick) {
this.visible = false;
}
2016-10-31 04:40:30 +00:00
this.$emit('command', command, instance);
2016-07-27 06:15:02 +00:00
}
2016-10-05 03:03:44 +00:00
},
render(h) {
2016-10-12 12:27:51 +00:00
let { hide, splitButton, type, size } = this;
2016-10-05 03:03:44 +00:00
var handleClick = _ => {
this.$emit('click');
};
let triggerElm = !splitButton
? this.$slots.default
: (<el-button-group>
2016-10-12 12:27:51 +00:00
<el-button type={type} size={size} nativeOn-click={handleClick}>
2016-10-05 03:03:44 +00:00
{this.$slots.default}
</el-button>
2016-10-13 05:33:19 +00:00
<el-button ref="trigger" type={type} size={size} class="el-dropdown__caret-button">
2016-10-05 03:03:44 +00:00
<i class="el-dropdown__icon el-icon-caret-bottom"></i>
</el-button>
</el-button-group>);
return (
<div class="el-dropdown" v-clickoutside={hide}>
{triggerElm}
{this.$slots.dropdown}
2016-10-05 03:03:44 +00:00
</div>
);
2016-07-27 06:15:02 +00:00
}
};
</script>