scinote-web/app/javascript/vue/shared/general_dropdown.vue

71 lines
1.9 KiB
Vue
Raw Normal View History

2023-12-12 19:17:38 +08:00
<template>
<div class="relative" v-click-outside="closeMenu" >
2024-01-29 22:19:25 +08:00
<div ref="field" class="cursor-pointer" @click.stop="isOpen = (!isOpen || fieldOnlyOpen)">
2023-12-12 19:17:38 +08:00
<slot name="field"></slot>
</div>
<template v-if="isOpen">
<teleport to="body">
<div ref="flyout"
class="sn-dropdown fixed z-[3000] bg-sn-white inline-block
rounded p-2.5 sn-shadow-menu-sm"
:class="{
'right-0': position === 'right',
'left-0': position === 'left',
}"
>
<slot name="flyout"></slot>
</div>
</teleport>
</template>
2023-12-12 19:17:38 +08:00
</div>
</template>
<script>
import { vOnClickOutside } from '@vueuse/components';
import FixedFlyoutMixin from './mixins/fixed_flyout.js';
export default {
name: 'DropdownMenu',
props: {
listItems: { type: Array, default: () => [] },
position: { type: String, default: 'left' },
btnClasses: { type: String, default: 'btn btn-light' },
btnText: { type: String, required: false },
btnIcon: { type: String, required: false },
caret: { type: Boolean, default: false },
alwaysShow: { type: Boolean, default: false },
2024-01-29 22:19:25 +08:00
closeDropdown: { type: Boolean, default: false },
fieldOnlyOpen: { type: Boolean, default: false },
fixedWidth: { type: Boolean, default: false }
2023-12-12 19:17:38 +08:00
},
data() {
return {
isOpen: false
};
},
directives: {
'click-outside': vOnClickOutside
},
mixins: [FixedFlyoutMixin],
watch: {
isOpen() {
if (this.isOpen) {
this.$emit('open');
this.$nextTick(() => {
this.setPosition();
});
} else {
this.$emit('close');
}
}
},
methods: {
closeMenu(e) {
2024-01-29 22:19:25 +08:00
if (e && e.target.closest('.sn-dropdown, .sn-select-dropdown, .sn-menu-dropdown, .dp__instance_calendar')) return;
2023-12-12 19:17:38 +08:00
this.isOpen = false;
}
}
};
</script>