iview/src/components/select/dropdown.vue

94 lines
3.2 KiB
Vue
Raw Normal View History

<template>
2017-08-18 16:49:42 +08:00
<div class="ivu-select-dropdown" :class="className" :style="styles"><slot></slot></div>
</template>
<script>
2017-07-10 15:36:09 +08:00
import Vue from 'vue';
const isServer = Vue.prototype.$isServer;
2017-01-13 17:10:57 +08:00
import { getStyle } from '../../utils/assist';
2018-03-15 10:43:25 +08:00
const Popper = isServer ? function() {} : require('popper.js/dist/umd/popper.js'); // eslint-disable-line
export default {
name: 'Drop',
props: {
placement: {
type: String,
default: 'bottom-start'
2017-08-18 16:49:42 +08:00
},
className: {
type: String
}
},
data () {
return {
2017-01-13 17:10:57 +08:00
popper: null,
2018-03-29 17:35:56 +08:00
width: '',
popperStatus: false
2016-12-25 22:49:42 +08:00
};
},
2017-01-13 17:10:57 +08:00
computed: {
styles () {
let style = {};
if (this.width) style.width = `${this.width}px`;
return style;
}
},
methods: {
update () {
2017-07-10 15:36:09 +08:00
if (isServer) return;
if (this.popper) {
this.$nextTick(() => {
this.popper.update();
2018-03-29 17:35:56 +08:00
this.popperStatus = true;
});
} else {
this.$nextTick(() => {
2017-03-03 17:46:09 +08:00
this.popper = new Popper(this.$parent.$refs.reference, this.$el, {
placement: this.placement,
2018-03-15 00:07:11 +08:00
modifiers: {
computeStyle:{
gpuAcceleration: false,
}
2018-03-16 00:43:11 +08:00
},
onCreate:()=>{
this.resetTransformOrigin();
2018-03-16 16:58:20 +08:00
this.$nextTick(this.popper.update());
},
onUpdate:()=>{
this.resetTransformOrigin();
2018-03-15 00:07:11 +08:00
}
});
});
}
2017-01-13 17:10:57 +08:00
// set a height for parent is Modal and Select's width is 100%
if (this.$parent.$options.name === 'iSelect') {
this.width = parseInt(getStyle(this.$parent.$el, 'width'));
}
},
destroy () {
if (this.popper) {
setTimeout(() => {
2018-03-29 17:35:56 +08:00
if (this.popper && !this.popperStatus) {
2018-01-12 09:51:33 +08:00
this.popper.destroy();
this.popper = null;
}
2018-03-29 17:35:56 +08:00
this.popperStatus = false;
}, 300);
}
2018-03-16 00:43:11 +08:00
},
resetTransformOrigin() {
let placement = this.popper.popper.getAttribute('x-placement').split('-')[0];
this.popper.popper.style.transformOrigin = placement==='bottom'?'center top':'center bottom';
}
},
2017-03-03 17:46:09 +08:00
created () {
this.$on('on-update-popper', this.update);
this.$on('on-destroy-popper', this.destroy);
},
beforeDestroy () {
if (this.popper) {
this.popper.destroy();
}
}
2016-12-25 22:49:42 +08:00
};
</script>