1
0

index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import './index.less';
  2. import type { TabContentProps } from './types';
  3. import { defineComponent, watch, computed, unref, ref } from 'vue';
  4. import { useRouter } from 'vue-router';
  5. import { Tabs } from 'ant-design-vue';
  6. import TabContent from './TabContent';
  7. import { useGo } from '/@/hooks/web/usePage';
  8. import { TabContentEnum } from './types';
  9. import { tabStore } from '/@/store/modules/tab';
  10. import { userStore } from '/@/store/modules/user';
  11. import { initAffixTabs, useTabsDrag } from './useMultipleTabs';
  12. import { REDIRECT_NAME } from '/@/router/constant';
  13. export default defineComponent({
  14. name: 'MultipleTabs',
  15. setup() {
  16. const activeKeyRef = ref('');
  17. const affixTextList = initAffixTabs();
  18. useTabsDrag(affixTextList);
  19. const go = useGo();
  20. const { currentRoute } = useRouter();
  21. const getTabsState = computed(() => tabStore.getTabsState);
  22. watch(
  23. () => tabStore.getLastChangeRouteState?.path,
  24. () => {
  25. if (tabStore.getLastChangeRouteState?.name === REDIRECT_NAME) {
  26. return;
  27. }
  28. const lastChangeRoute = unref(tabStore.getLastChangeRouteState);
  29. if (!lastChangeRoute || !userStore.getTokenState) return;
  30. const { path, fullPath } = lastChangeRoute;
  31. const p = fullPath || path;
  32. if (activeKeyRef.value !== p) {
  33. activeKeyRef.value = p;
  34. }
  35. tabStore.addTabAction(lastChangeRoute);
  36. },
  37. {
  38. immediate: true,
  39. }
  40. );
  41. function handleChange(activeKey: any) {
  42. activeKeyRef.value = activeKey;
  43. go(activeKey, false);
  44. }
  45. // Close the current tab
  46. function handleEdit(targetKey: string) {
  47. // Added operation to hide, currently only use delete operation
  48. tabStore.closeTabByKeyAction(targetKey);
  49. }
  50. function renderQuick() {
  51. const tabContentProps: TabContentProps = {
  52. tabItem: currentRoute.value,
  53. type: TabContentEnum.EXTRA_TYPE,
  54. };
  55. return <TabContent {...tabContentProps} />;
  56. }
  57. function renderTabs() {
  58. return unref(getTabsState).map((item) => {
  59. const key = item.query ? item.fullPath : item.path;
  60. const closable = !(item && item.meta && item.meta.affix);
  61. const slots = {
  62. tab: () => <TabContent tabItem={item} />,
  63. };
  64. return (
  65. <Tabs.TabPane key={key} closable={closable}>
  66. {slots}
  67. </Tabs.TabPane>
  68. );
  69. });
  70. }
  71. return () => {
  72. const slots = {
  73. default: () => renderTabs(),
  74. tabBarExtraContent: () => renderQuick(),
  75. };
  76. return (
  77. <div class="multiple-tabs">
  78. <Tabs
  79. type="editable-card"
  80. size="small"
  81. animated={false}
  82. hideAdd={true}
  83. tabBarGutter={3}
  84. activeKey={unref(activeKeyRef)}
  85. onChange={handleChange}
  86. onEdit={handleEdit}
  87. >
  88. {slots}
  89. </Tabs>
  90. </div>
  91. );
  92. };
  93. },
  94. });