huangyinfeng
4 天以前 db42d08c39ae6129e2b95cd24c0d57c6769282e5
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<template>
  <div>
    <a-sub-menu :key="item.key" :open="isOpen">
      <template #title>
        <div>
          <div v-if="!isSearch" class="my-display">
            <span>{{ item.title }}</span>
            <div class="handle-icon">
              <SearchOutlined v-if="item.key == 'moduleBelowA'" @click.stop="handleSearch(item)" />
              <MoreOutlined />
            </div>
          </div>
          <a-select
            v-if="isSearch"
            ref="inputRef"
            v-model:value="value"
            style="width: 100%"
            show-search
            placeholder="请输入文件夹关键字搜索"
            :options="options"
            :field-names="{ label: 'title', value: 'key' }"
            @change="handleChange"
            @blur="blurSearch"
          ></a-select>
        </div>
      </template>
      <template v-for="child in item.children" :key="child.key">
        <template v-if="!child.children || child.children.length === 0">
          <a-menu-item :key="child.key" @click="$emit('click', child)">
            <span>{{ child.title }}</span>
            <span v-if="child.total > 0" class="my-left">{{ child.total }}</span>
          </a-menu-item>
        </template>
        <template v-else>
          <!-- 递归调用自身 -->
          <MyMenu :item="child" @click="$emit('click', child)" />
        </template>
      </template>
    </a-sub-menu>
  </div>
</template>
 
<script lang="ts" setup>
  import { defineProps, defineEmits, ref, computed } from 'vue';
  import { SearchOutlined, MoreOutlined } from '@ant-design/icons-vue';
 
  interface MenuItem {
    title: string;
    key: string;
    total: number;
    children?: MenuItem[];
  }
 
  const props = defineProps<{ item: MenuItem }>();
  const emit = defineEmits();
  const options = ref([]);
  const isSearch = ref(false);
  const inputRef = ref();
  const value = ref();
 
  // 计算属性,判断当前菜单项是否应展开
  const isOpen = computed(() => {
    return true; // 默认为展开状态
  });
 
  function handleSearch(item: MenuItem) {
    console.log('handleSearch', item);
    options.value = flattenMenuItems(item.children);
    console.log('options', options.value);
    isSearch.value = true;
    setTimeout(() => {
      inputRef.value.focus();
    }, 100);
    emit('handleSearch', item);
  }
 
  function blurSearch() {
    isSearch.value = false;
  }
 
  const handleChange = (value: string) => {
    console.log(`selected ${value}`, isSearch.value);
    isSearch.value = false;
    emit('updateSelectedKeys', value);
  };
 
  const flattenMenuItems = (items: MenuItem[]): MenuItem[] => {
    let result: MenuItem[] = [];
    const recursiveFlatten = (items: MenuItem[]) => {
      for (const item of items) {
        result.push({ ...item, children: undefined }); // 不保留 children
        if (item.children && item.children.length > 0) {
          recursiveFlatten(item.children);
        }
      }
    };
    recursiveFlatten(items);
    return result;
  };
</script>
 
<style scoped>
  .my-display {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
 
  .my-left {
    margin-left: 5px;
  }
 
  .handle-icon {
    display: none;
    font-size: 16px;
  }
 
  .my-display:hover .handle-icon {
    display: block;
  }
</style>