index.tsx 4.4 KB
Newer Older
陈文彬 已提交
1 2 3 4
import type { TabContentProps } from './tab.data';
import type { TabItem } from '/@/store/modules/tab';
import type { AppRouteRecordRaw } from '/@/router/types';

5 6 7 8 9 10 11
import {
  defineComponent,
  watch,
  computed,
  // ref,
  unref,
  onMounted,
V
vben 已提交
12
  toRaw,
13
} from 'vue';
陈文彬 已提交
14 15 16 17 18 19 20 21 22 23 24 25
import { Tabs } from 'ant-design-vue';
import TabContent from './TabContent';

import { useGo } from '/@/hooks/web/usePage';

import { TabContentEnum } from './tab.data';

import { useRouter } from 'vue-router';

import { tabStore } from '/@/store/modules/tab';
import { closeTab } from './useTabDropdown';
import router from '/@/router';
26 27 28
import { useTabs } from '/@/hooks/web/useTabs';
import { PageEnum } from '/@/enums/pageEnum';

29
import './index.less';
陈文彬 已提交
30 31 32 33 34 35
export default defineComponent({
  name: 'MultiTabs',
  setup() {
    let isAddAffix = false;
    const go = useGo();
    const { currentRoute } = useRouter();
36
    const { addTab, activeKeyRef } = useTabs();
37
    onMounted(() => {
V
vben 已提交
38 39 40 41 42
      const route = unref(currentRoute);
      addTab(unref(currentRoute).path as PageEnum, false, {
        query: route.query,
        params: route.params,
      });
43 44
    });

陈文彬 已提交
45
    // 当前激活tab
46 47
    // const activeKeyRef = ref<string>('');

陈文彬 已提交
48 49 50 51 52
    // 当前tab列表
    const getTabsState = computed(() => {
      return tabStore.getTabsState;
    });

53 54 55 56 57
    if (!isAddAffix) {
      addAffixTabs();
      isAddAffix = true;
    }

陈文彬 已提交
58 59 60
    watch(
      () => unref(currentRoute).path,
      (path) => {
61 62
        if (activeKeyRef.value !== path) {
          activeKeyRef.value = path;
陈文彬 已提交
63
        }
64 65 66
        // 监听路由的话虽然可以,但是路由切换的时间会造成卡顿现象?
        // 使用useTab的addTab的话,当用户手动调转,需要自行调用addTab
        // tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
V
vben 已提交
67 68 69 70 71 72 73 74
        // const { affix } = currentRoute.value.meta || {};
        // if (affix) return;
        // const hasInTab = tabStore.getTabsState.some(
        //   (item) => item.fullPath === currentRoute.value.fullPath
        // );
        // if (!hasInTab) {
        //   tabStore.commitAddTab((unref(currentRoute) as unknown) as AppRouteRecordRaw);
        // }
陈文彬 已提交
75 76 77 78 79
      },
      {
        immediate: true,
      }
    );
80

陈文彬 已提交
81 82 83 84 85 86 87 88
    /**
     * @description: 过滤所有固定路由
     */
    function filterAffixTabs(routes: AppRouteRecordRaw[]) {
      const tabs: TabItem[] = [];
      routes &&
        routes.forEach((route) => {
          if (route.meta && route.meta.affix) {
V
vben 已提交
89
            tabs.push(toRaw(route) as TabItem);
陈文彬 已提交
90 91 92 93
          }
        });
      return tabs;
    }
94

陈文彬 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    /**
     * @description: 设置固定tabs
     */
    function addAffixTabs(): void {
      const affixTabs = filterAffixTabs((router.getRoutes() as unknown) as AppRouteRecordRaw[]);
      for (const tab of affixTabs) {
        tabStore.commitAddTab(tab);
      }
    }

    // tab切换
    function handleChange(activeKey: any) {
      activeKeyRef.value = activeKey;
      go(activeKey, false);
    }
110

陈文彬 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    // 关闭当前ab
    function handleEdit(targetKey: string) {
      // 新增操作隐藏,目前只使用删除操作
      const index = unref(getTabsState).findIndex((item) => item.path === targetKey);
      index !== -1 && closeTab(unref(getTabsState)[index]);
    }

    function renderQuick() {
      const tabContentProps: TabContentProps = {
        tabItem: (currentRoute as unknown) as AppRouteRecordRaw,
        type: TabContentEnum.EXTRA_TYPE,
        trigger: ['click', 'contextmenu'],
      };
      return (
        <span>
V
vben 已提交
126
          <TabContent {...(tabContentProps as any)} />
陈文彬 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        </span>
      );
    }
    function renderTabs() {
      return unref(getTabsState).map((item: TabItem) => {
        return (
          <Tabs.TabPane key={item.path} closable={!(item && item.meta && item.meta.affix)}>
            {{
              tab: () => <TabContent tabItem={item} />,
            }}
          </Tabs.TabPane>
        );
      });
    }

    return () => {
      return (
        <div class="multiple-tabs">
          <Tabs
            type="editable-card"
            size="small"
            hideAdd={true}
            tabBarGutter={2}
            activeKey={unref(activeKeyRef)}
            onChange={handleChange}
            onEdit={handleEdit}
          >
            {{
              default: () => renderTabs(),
              tabBarExtraContent: () => renderQuick(),
            }}
          </Tabs>
        </div>
      );
    };
  },
});