Blame view

src/layouts/default/multitabs/index.tsx 2.78 KB
1
2
import './index.less';
vben authored
3
import type { TabContentProps } from './types';
4
vben authored
5
import { defineComponent, watch, computed, unref, ref } from 'vue';
6
7
import { useRouter } from 'vue-router';
陈文彬 authored
8
9
10
11
12
import { Tabs } from 'ant-design-vue';
import TabContent from './TabContent';

import { useGo } from '/@/hooks/web/usePage';
vben authored
13
import { TabContentEnum } from './types';
陈文彬 authored
14
15

import { tabStore } from '/@/store/modules/tab';
16
17
import { userStore } from '/@/store/modules/user';
vben authored
18
import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
19
陈文彬 authored
20
export default defineComponent({
21
  name: 'MultipleTabs',
陈文彬 authored
22
  setup() {
23
24
25
    const activeKeyRef = ref('');

    const affixTextList = initAffixTabs();
vben authored
26
vben authored
27
    useTabsDrag(affixTextList);
vben authored
28
vben authored
29
    const go = useGo();
30
陈文彬 authored
31
    const { currentRoute } = useRouter();
32
vben authored
33
    const getTabsState = computed(() => tabStore.getTabsState);
陈文彬 authored
34
35

    watch(
vben authored
36
      () => tabStore.getLastChangeRouteState?.path,
37
      () => {
38
39
        const lastChangeRoute = unref(tabStore.getLastChangeRouteState);
        if (!lastChangeRoute || !userStore.getTokenState) return;
vben authored
40
        const { path, fullPath } = lastChangeRoute;
41
42
43
        const p = fullPath || path;
        if (activeKeyRef.value !== p) {
          activeKeyRef.value = p;
陈文彬 authored
44
        }
vben authored
45
        tabStore.addTabAction(lastChangeRoute);
陈文彬 authored
46
47
48
49
50
      },
      {
        immediate: true,
      }
    );
51
陈文彬 authored
52
53
54
55
    function handleChange(activeKey: any) {
      activeKeyRef.value = activeKey;
      go(activeKey, false);
    }
56
57
    // Close the current tab
陈文彬 authored
58
    function handleEdit(targetKey: string) {
59
      // Added operation to hide, currently only use delete operation
vben authored
60
      tabStore.closeTabByKeyAction(targetKey);
陈文彬 authored
61
62
63
64
    }

    function renderQuick() {
      const tabContentProps: TabContentProps = {
vben authored
65
        tabItem: currentRoute.value,
陈文彬 authored
66
67
        type: TabContentEnum.EXTRA_TYPE,
      };
vben authored
68
      return <TabContent {...tabContentProps} />;
陈文彬 authored
69
    }
70
陈文彬 authored
71
    function renderTabs() {
vben authored
72
      return unref(getTabsState).map((item) => {
73
        const key = item.query ? item.fullPath : item.path;
74
        const closable = !(item && item.meta && item.meta.affix);
75
76
77
78

        const slots = {
          tab: () => <TabContent tabItem={item} />,
        };
陈文彬 authored
79
        return (
80
          <Tabs.TabPane key={key} closable={closable}>
81
            {slots}
陈文彬 authored
82
83
84
85
86
87
          </Tabs.TabPane>
        );
      });
    }

    return () => {
88
89
90
91
      const slots = {
        default: () => renderTabs(),
        tabBarExtraContent: () => renderQuick(),
      };
陈文彬 authored
92
93
94
95
96
      return (
        <div class="multiple-tabs">
          <Tabs
            type="editable-card"
            size="small"
vben authored
97
            animated={false}
陈文彬 authored
98
            hideAdd={true}
99
            tabBarGutter={3}
陈文彬 authored
100
101
102
103
            activeKey={unref(activeKeyRef)}
            onChange={handleChange}
            onEdit={handleEdit}
          >
104
            {slots}
陈文彬 authored
105
106
107
108
109
110
          </Tabs>
        </div>
      );
    };
  },
});