import { Component, Prop, Vue, Watch, Ref } from "vue-property-decorator"; import Icon from "@/components/Icon/index.vue"; import Button from "@/components/Button/index.vue"; import { IDropdownOptions } from "@/types"; import toInt from "@/utils/converts"; @Component({ components: { Icon, Button }, }) export default class Dropdown extends Vue { @Prop({ type: Array }) readonly list!: IDropdownOptions[]; @Prop({ type: String }) readonly width?: string; @Prop({ type: String }) readonly maxHeight?: string; @Prop({ type: String }) readonly value?: string; @Prop({ type: Boolean, default: true }) readonly autoClose?: boolean; @Prop({ type: Boolean, default: false }) readonly isClose?: boolean; @Prop({ type: Boolean, default: false }) readonly selected?: boolean; @Prop({ type: [String, Object, Array, Number] }) readonly customPayload?: any; @Prop({ type: Function }) readonly onDropDonwClk?: (arg) => void; @Ref("button") readonly buttonRef!: HTMLElement; @Ref("menu") readonly menuRef!: any; toggle = false; menuTop = ""; style = { top: "", left: "", right: "", }; windowWidth = window.innerWidth; finalColor(color: string): string { return `!text-${color}`; } updateStyle(): void { // get the position of button and set it to menu if (this.buttonRef && this.menuRef) { const btnElm = this.buttonRef.children[0]; const { height, top, left, width, bottom } = btnElm?.getBoundingClientRect(); const mne = this.menuRef.children[0]; if (mne) { const menuWidth = mne.getBoundingClientRect().width; const menuHeight = mne.getBoundingClientRect().height; let right = window.innerWidth - (left + width); const cssObj = window.getComputedStyle(btnElm, null); let addi = 0; addi -= toInt(cssObj.getPropertyValue("padding-right"))[0]; addi -= toInt(cssObj.getPropertyValue("margin-right"))[0]; right -= (width * (width < 50 ? 50 : Math.max(10 - addi, 15))) / 100 + addi; this.$set(this.style, "top", `${top + height + 2}px`); if (menuWidth > left + width) { if (menuWidth > right + width) { this.$set(this.style, "right", ""); const resLeft = left - (menuWidth - (width + right)); if (resLeft > 0) { this.$set(this.style, "left", `${resLeft}px`); } else { // const [wd] = toInt(mne.style.width); // this.$set(this.style, 'width', `${wd - resLeft}px`); this.$set(this.style, "left", 0); } } else { this.$set(this.style, "right", ""); this.$set(this.style, "left", `${left}px`); } } else { this.$set(this.style, "left", ""); this.$set(this.style, "right", `${right}px`); } if (menuHeight > window.innerHeight - bottom) { this.$set(this.style, 'top', `${top - menuHeight}px`); } } } } outsideClick(e: Event): void { const menu = document.getElementById("select-dropdown"); const event = e.target as HTMLInputElement; // close menu if (this.autoClose) { if (!this.buttonRef?.children[0].contains(event)) { this.$nextTick(() => { this.toggle = false; }); } } else if (!menu?.contains(event) && !this.buttonRef?.children[0].contains(event)) { if (!this.menuRef?.children[0]?.contains(event)) { this.$nextTick(() => { this.toggle = false; }); } } } onButtonClick(): void { this.toggle = !this.toggle; } @Watch("toggle") watchToggle(): void { this.$emit("closer", this.toggle); this.$nextTick(() => { if (this.toggle) { this.updateStyle(); } if (this.onDropDonwClk) { this.onDropDonwClk(this.toggle); } }); if (this.toggle) { // append menu to body document.body.appendChild(this.menuRef); } } onResize(): void { this.$nextTick(() => { // update position of menue when window is resizing this.windowWidth = window.innerWidth; if (this.toggle) { this.updateStyle(); } }); } mounted(): void { document.addEventListener("scroll", this.updateStyle); document.documentElement.addEventListener("click", this.outsideClick, true); this.buttonRef.children[0].addEventListener("click", this.onButtonClick, true); window.addEventListener("resize", this.onResize); } beforeDestroy(): void { window.removeEventListener("resize", this.onResize); if (this.menuRef) { this.menuRef.parentNode?.removeChild(this.menuRef); } this.toggle = false; } @Watch("isClose") updateToggle() { if (this.isClose) { this.$emit("close"); if (this.menuRef) { this.menuRef.parentNode?.removeChild(this.menuRef); } this.toggle = false; } } }