index.js 2.6 KB
Newer Older
1
import React, { useState, useRef, useCallback, useEffect } from 'react';
邓晓峰's avatar
邓晓峰 committed
2
import { useHistory, useLocation } from 'react-router-dom';
3
import { getKeyName } from '../../utils/utils';
邓晓峰's avatar
邓晓峰 committed
4 5 6 7 8 9 10 11 12 13
const TabPanes = props => {
  const [activeKey, setActiveKey] = useState('');
  const [panes, setPanes] = useState([
    {
      title: '首页',
      key: 'home',
      closable: false,
      path: '/',
    },
  ]);
邓晓峰's avatar
邓晓峰 committed
14

邓晓峰's avatar
邓晓峰 committed
15 16 17
  const [isReload, setIsReload] = useState(false);
  const [selectedPanel, setSelectedPanel] = useState({});
  const pathRef = useRef(null);
邓晓峰's avatar
邓晓峰 committed
18

邓晓峰's avatar
邓晓峰 committed
19 20 21 22 23 24 25
  const {
    storeData: { curTab, reloadPath },
    setStoreData,
    defaultActiveKey,
    panesItem,
    tabActiveKey,
  } = props;
邓晓峰's avatar
邓晓峰 committed
26

邓晓峰's avatar
邓晓峰 committed
27
  const history = useHistory();
28
  const { pathname, search } = useLocation();
邓晓峰's avatar
邓晓峰 committed
29

30
  const fullPath = pathname + search;
邓晓峰's avatar
邓晓峰 committed
31 32 33 34 35 36 37 38 39 40

  const storeTabs = useCallback(
    ps => {
      const pathArr = ps.reduce((prev, next) => [...prev, next.path], []);
      // setStoreData('SET_CURTAB', pathArr)
    },
    [setStoreData],
  );

  const resetTabs = useCallback(() => {
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
    const initPanes = curTab.reduce((prev, next) => {
      const { title, tabKey, Content } = getKeyName(next);
      return [
        ...prev,
        {
          title,
          key: tabKey,
          content: Content,
          closable: tabKey !== 'home',
          path: next,
        },
      ];
    }, []);
    const { tabKey } = getKeyName(pathname);
    setPanes(initPanes);
    setActiveKey(tabKey);
  }, [curTab, pathname]);

  useEffect(() => {
    resetTabs();
  }, [resetTabs]);

  const onChange = tabKey => {
    setActiveKey(tabKey);
  };

  const remove = targetKey => {
    const delIndex = panes.findIndex(item => item.key === targetKey);
    panes.splice(delIndex, 1);

    if (targetKey !== activeKey) {
      const nextKey = activeKey;
      setPanes(panes);
      setActiveKey(nextKey);
      storeTabs(panes);
      return;
    }

    const nextPath = curTab[delIndex - 1];
    const { tabKey } = getKeyName(nextPath);
    if (nextPath !== '/') {
      remove(tabKey);
      history.push(curTab[delIndex - 2]);
    } else {
      history.push(nextPath);
    }
    setPanes(panes);
    storeTabs(panes);
  };

  const onEdit = (targetKey, action) =>
    action === 'remove' && remove(targetKey);

  const onTabClick = targetKey => {
    const { path } = panes.filter(item => item.key === targetKey)[0];
    history.push({ pathname: path });
  };

  const refreshTab = () => {
    setIsReload(true);
    setTimeout(() => {
      setIsReload(false);
    }, 1000);
    setStoreData('SET_RELOADPATH', pathname + search);
    setTimeout(() => {
      setStoreData('SET_RELOADPATH', 'null');
    }, 500);
  };
邓晓峰's avatar
邓晓峰 committed
109
};