ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<script lang="ts" setup>
import type { PropType } from 'vue';
 
import type { DropMenu } from './typing';
 
import { computed } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { isFunction } from '@jnpf/utils';
 
import { $t } from '@vben/locales';
 
import { Dropdown, Menu, Popconfirm } from 'ant-design-vue';
import { omit } from 'lodash-es';
 
const props = defineProps({
  dropMenuList: {
    default: () => [],
    type: Array as PropType<(DropMenu & Recordable)[]>,
  },
  popconfirm: Boolean,
  selectedKeys: {
    default: () => [],
    type: Array as PropType<string[]>,
  },
  /**
   * the trigger mode which executes the drop-down action
   * @default ['hover']
   * @type string[]
   */
  trigger: {
    default: () => {
      return ['contextmenu'];
    },
    type: Array as PropType<('click' | 'contextmenu' | 'hover')[]>,
  },
});
const emit = defineEmits(['menuEvent']);
const ADropdown = Dropdown;
const AMenu = Menu;
const AMenuItem = Menu.Item;
const AMenuDivider = Menu.Divider;
const APopconfirm = Popconfirm;
const { createConfirm } = useMessage();
 
function handleClickMenu(item) {
  const { event } = item;
  const menu = props.dropMenuList.find((item) => `${item.event}` === `${event}`);
  emit('menuEvent', menu);
  if (item.modelConfirm) {
    createConfirm({
      content: item.modelConfirm?.content || $t('common.delTip'),
      iconType: item.modelConfirm?.iconType || 'warning',
      onOk: item.modelConfirm?.onOk,
      title: item.modelConfirm?.title || $t('common.tipTitle'),
    });
  } else {
    !props.popconfirm && item.onClick?.();
  }
}
 
const getPopConfirmAttrs = computed(() => {
  return (attrs) => {
    const originAttrs = omit(attrs, ['confirm', 'cancel', 'icon']);
    if (!attrs.onConfirm && attrs.confirm && isFunction(attrs.confirm)) originAttrs.onConfirm = attrs.confirm;
    if (!attrs.onCancel && attrs.cancel && isFunction(attrs.cancel)) originAttrs.onCancel = attrs.cancel;
    return originAttrs;
  };
});
 
const getAttr = (key: number | string) => ({ key });
</script>
 
<template>
  <ADropdown :trigger="trigger" v-bind="$attrs">
    <span>
      <slot></slot>
    </span>
    <template #overlay>
      <AMenu :selected-keys="selectedKeys">
        <template v-for="item in dropMenuList" :key="`${item.event}`">
          <AMenuItem v-bind="getAttr(item.event)" :disabled="item.disabled" @click="handleClickMenu(item)">
            <APopconfirm v-if="popconfirm && item.popConfirm" v-bind="getPopConfirmAttrs(item.popConfirm)">
              <template v-if="item.popConfirm.icon" #icon>
                <i :class="item.popConfirm.icon"></i>
              </template>
              <div>
                <i v-if="item.icon" :class="item.icon"></i>
                <span class="ml-1">{{ item.text }}</span>
              </div>
            </APopconfirm>
            <template v-else>
              <i v-if="item.icon" :class="item.icon"></i>
              <span class="ml-1">{{ item.text }}</span>
            </template>
          </AMenuItem>
          <AMenuDivider v-if="item.divider" :key="`d-${item.event}`" />
        </template>
      </AMenu>
    </template>
  </ADropdown>
</template>