iview/src/components/select/dropdown.vue

80 lines
2.6 KiB
Vue
Raw Normal View History

<template>
2017-01-13 17:10:57 +08:00
<div class="ivu-select-dropdown" :style="styles"><slot></slot></div>
</template>
<script>
2017-01-13 17:10:57 +08:00
import { getStyle } from '../../utils/assist';
import Popper from 'popper.js';
export default {
props: {
placement: {
type: String,
default: 'bottom-start'
}
},
data () {
return {
2017-01-13 17:10:57 +08:00
popper: null,
width: '',
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 () {
if (this.popper) {
this.$nextTick(() => {
this.popper.update();
});
} else {
this.$nextTick(() => {
this.popper = new Popper(this.$parent.$els.reference, this.$el, {
gpuAcceleration: false,
placement: this.placement,
boundariesPadding: 0,
2016-11-01 12:42:57 +08:00
forceAbsolute: true,
boundariesElement: 'body'
});
this.popper.onCreate(popper => {
this.resetTransformOrigin(popper);
});
});
}
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) {
this.resetTransformOrigin(this.popper);
setTimeout(() => {
this.popper.destroy();
this.popper = null;
}, 300);
}
},
resetTransformOrigin(popper) {
let placementMap = {top: 'bottom', bottom: 'top'};
let placement = popper._popper.getAttribute('x-placement').split('-')[0];
let origin = placementMap[placement];
popper._popper.style.transformOrigin = `center ${ origin }`;
}
},
2017-01-16 12:29:03 +08:00
compiled () {
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>