Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

antdpro的prolayout鼠标移入一级菜单,显示二级菜单,如何实现鼠标移入一级菜单,显示一级与二级菜单 #10873

Closed
wangfeiyu2020 opened this issue Aug 4, 2023 · 2 comments
Labels

Comments

@wangfeiyu2020
Copy link

wangfeiyu2020 commented Aug 4, 2023

🧐 问题描述 | Problem description

antdpro的prolayout鼠标移入一级菜单,显示二级菜单,如何实现鼠标移入一级菜单,显示一级与二级菜单

💻 示例代码 | Sample code

<ProLayout
{...Settings}
{...SettingRoutes}
collapsed={collapsed}
onCollapse={setCollapsed}
location={{
pathname
}}
actionsRender={() => {
return [];
}}
menuItemRender={(menuItemProps, defaultDom) => (
<div
style={{
display: "flex",
alignItems: "center"
}}
onClick={() => {
setCollapsed(true);
setPathname(menuItemProps.path || "/organizationaManage/companyInformation");
}}
>
{menuItemProps.pro_layout_parentKeys &&
menuItemProps.pro_layout_parentKeys.length > 0 &&
menuItemProps.icon}
{defaultDom}

)}
>

{pathname === "/systemTools/panel" && }

🚑 其他信息 | Other information

图片
图片

OS:

Node:

浏览器 | browser:firefox

@wangfeiyu2020 wangfeiyu2020 changed the title antdpro的prolayout鼠标移入一级菜单,显示二级菜单,如何实现鼠标移入一级菜单,显示以及与二级菜单 antdpro的prolayout鼠标移入一级菜单,显示二级菜单,如何实现鼠标移入一级菜单,显示一级与二级菜单 Aug 4, 2023
@chenshuai2144
Copy link
Collaborator

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns.
此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

✅ 解决方案 | Solution

要实现鼠标移入一级菜单时显示一级和二级菜单的效果,你可以使用antdpro中Menu组件的SubMenu子菜单。

首先,确保你已经引入了antdpro的Menu组件。

然后,你可以使用CSS的伪类:hover来实现鼠标移入一级菜单时显示二级菜单的效果。以下是示例代码:

import React from 'react';
import { ProLayout, Menu } from '@ant-design/pro-layout';

const SubMenu = Menu.SubMenu;

const MyLayout = () => {
  return (
    <ProLayout
      menuRender={(props) => (
        <Menu
          mode="inline"
          theme="dark"
          selectedKeys={[props.location.pathname]}
          defaultSelectedKeys={[props.location.pathname]}
        >
          <SubMenu
            key="sub1"
            title={
              <span>
                <span>一级菜单</span>
              </span>
            }
          >
            <Menu.Item key="/page1">二级菜单1</Menu.Item>
            <Menu.Item key="/page2">二级菜单2</Menu.Item>
          </SubMenu>
        </Menu>
      )}
    >
      {/* 页面内容 */}
    </ProLayout>
  );
};

export default MyLayout;

在上述示例代码中,我们使用Menu组件的SubMenu来创建一级菜单,并在SubMenu中添加了二级菜单项。当鼠标移入一级菜单时,二级菜单项会显示出来。

你可以根据你的实际需求来修改菜单和页面内容。

关于antdpro中的Menu组件,你可以参考官方文档:https://ant.design/components/menu-cn/

希望以上解决方案对你有所帮助!

@chenshuai2144
Copy link
Collaborator

如果你想实现鼠标移入一级菜单时显示二级菜单的效果,你可以借助Ant Design ProLayout组件的onMouseEnteronMouseLeave事件来实现。

首先,在<ProLayout>组件上添加onMouseEnteronMouseLeave事件处理函数。

<ProLayout
  onMouseEnter={handleMouseEnter}
  onMouseLeave={handleMouseLeave}
>
  // 其他内容
</ProLayout>

然后,定义handleMouseEnterhandleMouseLeave函数来处理鼠标进入和离开一级菜单的事件。

const handleMouseEnter = () => {
  // 显示二级菜单的逻辑
};

const handleMouseLeave = () => {
  // 隐藏二级菜单的逻辑
};

handleMouseEnter函数中,你可以使用React的状态钩子来管理二级菜单是否显示的状态。

const [showSubMenu, setShowSubMenu] = useState(false);

const handleMouseEnter = () => {
  setShowSubMenu(true);
};

const handleMouseLeave = () => {
  setShowSubMenu(false);
};

最后,你可以根据showSubMenu状态来决定是否显示二级菜单。

{showSubMenu && (
  // 二级菜单的内容
)}

通过这种方式,当鼠标移入一级菜单时,二级菜单会显示出来,鼠标移出时,二级菜单会隐藏起来。希望对你有帮助!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants