DropdownMenu.vue 7.6 KB
Newer Older
1 2
<template>
  <div
aaronchen2k2k's avatar
aaronchen2k2k 已提交
3 4 5 6 7
      class="dropdown-menu"
      :class="menuClass ?? 'layer rounded'"
      :style="menuFinalStyle"
      @click="_handleClickMenu"
      ref="menuRef"
8 9
  >
    <template v-if="state.show">
aaronchen2k2k's avatar
aaronchen2k2k 已提交
10 11 12 13 14 15
      <input type="text" class="keywords"
             v-if="filter"
             v-model="keywords"
             @input="onKeywordsChanged"
             @click.stop />

16
      <List
aaronchen2k2k's avatar
aaronchen2k2k 已提交
17 18 19 20 21 22 23 24 25
          :items="itemsToShow"
          :replaceFields="replaceFields"
          :checkedKey="checkedKey"
          :activeKey="activeKey"
          :keyName="keyName"
          :class="listClass"
          :compact="listCompact"
          :divider="listDivider"
          @click="emit('click', $event)"
26
      />
aaronchen2k2k's avatar
aaronchen2k2k 已提交
27
      <slot/>
28 29 30 31 32
    </template>
  </div>
</template>

<script setup lang="ts">
aaronchen2k2k's avatar
aaronchen2k2k 已提交
33 34
import {computed, defineEmits, defineProps, onMounted, onUnmounted, reactive, ref, withDefaults} from 'vue';
import {onClickOutside, useWindowSize} from '@vueuse/core'
35
import List from './List.vue';
H
Hao Sun 已提交
36
import {ListItemKey, ListItemProps} from './ListItem.vue';
37

aaronchen2k2k's avatar
aaronchen2k2k 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
export type DropdownMenuPosition =
    'bottom'
    | 'left'
    | 'right'
    | 'top'
    | 'bottom-left'
    | 'bottom-right'
    | 'top-left'
    | 'top-right'
    | 'left-top'
    | 'left-bottom'
    | 'right-top'
    | 'right-bottom'
    | { left: number, top: number }
    | ((toggleElement: HTMLElement) => { left: number, top: number });
53 54

const props = withDefaults(defineProps<{
aaronchen2k2k's avatar
aaronchen2k2k 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  toggle?: object | string,
  triggerEvent?: string,
  position?: DropdownMenuPosition,
  items?: ListItemProps[] | Record<string, any>[],
  keyName?: string,
  checkedKey?: ListItemKey,
  activeKey?: ListItemKey,
  replaceFields?: Record<string, string>,
  listClass?: string,
  listCompact?: boolean,
  listDivider?: boolean,
  defaultShow?: boolean,
  showOnHover?: boolean,
  hideOnClickAway?: boolean,
  hideOnClickMenu?: boolean,
  menuClass?: string,
  limitInWindow?: boolean,
  menuStyle?: object
  filter?: boolean
74 75 76 77 78 79 80
}>(), {hideOnClickAway: true, hideOnClickMenu: true});

const menuRef = ref<HTMLElement>();
const cleanUpRef = ref();
const state = reactive({show: !!props.defaultShow, showed: false});
const showTimerRef = ref<number>(0);

aaronchen2k2k's avatar
aaronchen2k2k 已提交
81 82
const keywords = ref('')

83
function _toggle(show?: boolean) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  if (show === undefined) {
    show = !state.show;
  }
  if (state.show === show) {
    return;
  }
  if (showTimerRef.value) {
    clearTimeout(showTimerRef.value);
    showTimerRef.value = 0;
  }
  state.show = show;
  state.showed = false;
  if (show) {
    showTimerRef.value = setTimeout(() => {
      state.showed = true;
      showTimerRef.value = 0;
    }, 100);
  }
102 103 104
}

function _handleClickMenu(event: MouseEvent) {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
105 106 107 108 109 110
  if (!props.hideOnClickMenu || event.target && event.target instanceof HTMLElement && event.target.closest('.not-hide-menu')) {
    return;
  }
  if (state.showed) {
    _toggle(false);
  }
111 112
}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
113 114 115 116 117 118 119 120 121 122 123
const itemsToShow = computed(() => {
  let arr = props.items?.filter((item) => {
    return !keywords.value || item.name.indexOf(keywords.value) > -1;
  })
  return arr
})
const onKeywordsChanged = () => {
  console.log('onKeywordsChanged')
}

const emit = defineEmits<{ (type: 'click', event: { originalEvent: Event, key: ListItemKey, item: ListItemProps | Record<string, any> }): void }>();
124

aaronchen2k2k's avatar
aaronchen2k2k 已提交
125
onClickOutside(menuRef, _event => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
126 127 128
  if (props.hideOnClickAway && state.showed) {
    _toggle(false);
  }
129 130 131
});

function getToggleElement() {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
132 133 134 135
  const {toggle} = props;
  if (!toggle) {
    if (menuRef.value) {
      return menuRef.value.closest('.dropdown')?.querySelector('.dropdown-toggle') as HTMLElement || undefined;
136 137
    }
    return undefined;
aaronchen2k2k's avatar
aaronchen2k2k 已提交
138 139 140 141 142 143 144 145
  }
  if (toggle instanceof HTMLElement) {
    return toggle;
  }
  if (typeof toggle === 'string') {
    return document.querySelector(toggle) as HTMLElement || undefined;
  }
  return undefined;
146 147 148 149 150
}

const windowSize = useWindowSize();

const menuFinalStyle = computed(() => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  if (!state.show) {
    return {display: 'none'};
  }
  const style: Record<string, any> = {
    display: 'block',
    opacity: 1,
    top: '0px',
    left: '0px',
  };
  const element = getToggleElement();
  if (!element || !menuRef.value) {
    return style;
  }
  if (!state.showed) {
    style.opacity = 0;
    return style;
  }
  const {position = 'bottom-left'} = props;
  const bounding = element.getBoundingClientRect();
  const menuBounding = menuRef.value.getBoundingClientRect();
  if (typeof position === 'function') {
    Object.assign(style, position(element));
  } else if (typeof position === 'object') {
    Object.assign(style, position);
  } else if (position === 'bottom') {
    style.top = bounding.bottom;
    style.left = Math.round(bounding.left + (bounding.width / 2) - (menuBounding.width / 2));
  } else if (position === 'bottom-left') {
    style.top = bounding.bottom;
    style.left = bounding.left;
  } else if (position === 'bottom-right') {
    style.top = bounding.bottom;
    style.left = bounding.right - menuBounding.width;
  } else if (position === 'top') {
    style.top = bounding.top - menuBounding.height;
    style.left = bounding.left + (bounding.width / 2) - (menuBounding.width / 2);
  } else if (position === 'top-left') {
    style.top = bounding.top - menuBounding.height;
    style.left = bounding.left;
  } else if (position === 'top-right') {
    style.top = bounding.top - menuBounding.height;
    style.left = bounding.right - menuBounding.width;
  } else if (position === 'left') {
    style.left = bounding.left - menuBounding.width;
    style.top = bounding.top + (bounding.height / 2) - (menuBounding.height / 2);
  } else if (position === 'left-top') {
    style.left = bounding.left - menuBounding.width;
    style.top = bounding.top;
  } else if (position === 'left-bottom') {
    style.left = bounding.left - menuBounding.width;
    style.top = bounding.bottom - menuBounding.height;
  } else if (position === 'right') {
    style.left = bounding.left - menuBounding.width;
  } else if (position === 'right-top') {
    style.left = bounding.right;
    style.top = bounding.top;
  } else if (position === 'right-bottom') {
    style.left = bounding.right;
    style.top = bounding.bottom - menuBounding.height;
  }
211

aaronchen2k2k's avatar
aaronchen2k2k 已提交
212 213 214 215 216 217 218 219 220
  if (props.limitInWindow !== false) {
    style.left = Math.min(windowSize.width.value - menuBounding.width, style.left);
    style.top = Math.min(windowSize.height.value - menuBounding.height, style.top);
  }
  style.maxHeight = `${Math.round(windowSize.height.value - style.top - 10)}px`;
  style.maxWidth = `${Math.round(windowSize.width.value - style.left - 10)}px`;
  style.left = `${Math.round(style.left)}px`;
  style.top = `${Math.round(style.top)}px`;
  style.overflow = 'auto';
221

aaronchen2k2k's avatar
aaronchen2k2k 已提交
222 223 224 225 226
  if (props.menuStyle) {
    Object.assign(style, props.menuStyle);
  }

  return style;
227 228 229
});

onMounted(() => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
230 231 232 233
  const toggleElement = getToggleElement();
  if (!toggleElement) {
    return;
  }
234

aaronchen2k2k's avatar
aaronchen2k2k 已提交
235 236 237 238 239 240 241 242 243 244 245 246
  const triggerEvent = props.triggerEvent ?? 'click';
  const handler = (_event) => {
    if (state.showed) {
      _toggle(false);
    } else if (!state.show) {
      _toggle(true);
    }
  };
  toggleElement.addEventListener(triggerEvent, handler, false);
  cleanUpRef.value = () => {
    toggleElement.removeEventListener(triggerEvent, handler, false);
  };
247 248 249
});

onUnmounted(() => {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
250 251 252
  if (cleanUpRef.value) {
    cleanUpRef.value();
  }
253 254 255 256 257 258 259 260 261 262 263
})
</script>

<style scoped>
.dropdown-menu {
  position: fixed;
  min-width: 100px;
  z-index: 100;
  padding-top: var(--space-sm);
  padding-bottom: var(--space-sm);
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
264

265 266 267 268
.dropdown-menu :deep(.list-item) {
  padding-left: var(--space-lg);
  padding-right: var(--space-lg);
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
269

270 271 272
.dropdown-menu :deep(.list-item.has-checkmark) {
  padding-right: var(--space-sm);
}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
273 274 275 276 277 278 279 280 281 282 283 284

.dropdown-menu .keywords {
  margin: 3px 10px;
  padding: 0 5px;
  height: 22px;
  outline: 0;
}

.dropdown-menu .keywords input {

}

285
</style>