1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from 'react';
  2. import { Menu } from 'antd';
  3. import { useNavigate } from 'react-router-dom';
  4. import useRoute from '@/utils/hooks/useRoute';
  5. const menuStyle = { height: '100%', borderRight: 0 };
  6. export default (props) => {
  7. const { theme, items, location } = props;
  8. const navigate = useNavigate();
  9. const { meta } = useRoute() || {};
  10. const selectedKeys = React.useMemo(() => {
  11. const parts = location.pathname.split('/').filter(Boolean);
  12. const keys = parts.reduce((acc, it) => {
  13. const parent = acc.pop();
  14. const path = !parent ? `/${it}` : `${parent}/${it}`
  15. return acc.concat([parent, path].filter(Boolean));
  16. }, []);
  17. return keys;
  18. }, [location.pathname]);
  19. // const selectedKeys = [location.pathname];
  20. const onClick = ({key}) => {
  21. const path = key
  22. if (path.indexOf('http') > -1) {
  23. document.createElement('a');
  24. a.href = path;
  25. a.target = '_blank';
  26. a.click();
  27. } else {
  28. if (meta?.target === '_blank') {
  29. document.createElement('a');
  30. a.href = `${window.location.pathname}#${path}`
  31. a.target = '_blank';
  32. a.click();
  33. } else {
  34. navigate(path);
  35. }
  36. }
  37. }
  38. return (
  39. <Menu
  40. mode="inline"
  41. style={menuStyle}
  42. theme={theme}
  43. items={items}
  44. selectedKeys={selectedKeys}
  45. onClick={onClick}
  46. />
  47. )
  48. }