22
schangxiang@126.com
2025-05-21 40c6e31e722088fb5577552271eb0af7f695131e
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
<script lang="ts">
import { defineComponent, h, Teleport, toRefs } from 'vue'
import type { PropType, VNode } from 'vue'
import type { MenuOptions } from './ContextMenuDefine'
import { genContainer } from "./ContextMenuUtils";
import ContextSubMenuWrapperConstructor from './ContextSubMenuWrapper.vue'
 
export type GlobalHasSlot = (name: string) => boolean;
export type GlobalRenderSlot = (name: string, params: Record<string, unknown>) => VNode;
 
/**
 * Context menu component
 */
export default defineComponent({
  name: 'ContextMenu',
  emits: [ 'update:show', 'close' ],
  props: {
    /**
     * Menu options
     */
    options: {
      type: Object as PropType<MenuOptions>,
      default: null
    },
    /**
     * Show menu?
     */
    show: {
      type: Boolean,
      default: false
    },
  },
  setup(props, ctx) {
 
    const {
      options,
      show,
    } = toRefs(props);
 
    ctx.expose({
      closeMenu: () => ctx.emit('update:show', false),
      isClosed: () => !show.value,
    });
 
    return () => {  
      const { isNew, container, eleId } = genContainer(options.value);
      
      return [
        h(
          Teleport,
          { to: `#${eleId}` },
          [
            h(ContextSubMenuWrapperConstructor as unknown as string, { 
              options: options,
              show: show,
              container: container,
              isFullScreenContainer: !isNew,
              onClose: (fromItem: undefined) => {
                ctx.emit('update:show', false);
                ctx.emit('close');
                options.value.onClose?.(fromItem);
              },
            }, ctx.slots)
          ]
        )
      ];
    }
  },
})
</script>