Search code examples
reactjsantd

Open antd submenu only on click in antd


With the below code I'm trying to open submenu on-click, but it opens automatically every time I hover on it. I need help in finding it.

codesandbox: https://codesandbox.io/s/falling-field-10ilwd?file=/src/index.js

code:

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Menu, Icon } from "antd";

const { SubMenu } = Menu;

class Sider extends React.Component {
  state = {
    mode: "vertical",
    theme: "light"
  };

  render() {
    return (
      <div>
        <br />
        <br />
        <Menu
          style={{ width: 256 }}
          mode={this.state.mode}
          theme={this.state.theme}
        >
          <SubMenu
            key="sub1"
            title={
              <span>
                <Icon type="appstore" />
                <span>Navigation Three</span>
              </span>
            }
          >
            <Menu.Item key="3">Option 3</Menu.Item>
            <Menu.Item key="4">Option 4</Menu.Item>
            <SubMenu key="sub1-2" title="Submenu">
              <Menu.Item key="5">Option 5</Menu.Item>
              <Menu.Item key="6">Option 6</Menu.Item>
            </SubMenu>
          </SubMenu>
          <SubMenu
            key="sub2"
            title={
              <span>
                <Icon type="setting" />
                <span>Navigation Four</span>
              </span>
            }
          >
            <Menu.Item key="7">Option 7</Menu.Item>
            <Menu.Item key="8">Option 8</Menu.Item>
            <Menu.Item key="9">Option 9</Menu.Item>
            <Menu.Item key="10">Option 10</Menu.Item>
          </SubMenu>
        </Menu>
      </div>
    );
  }
}

ReactDOM.render(<Sider />, document.getElementById("container"));

The submenu should open only when I click on navigation three or four and should not open when I hover on it. Thanks.


Solution

  • You can change the sub menu trigger option using the "triggerSubMenuAction" param. Add this param in you Menu component.

    <Menu
        style={{ width: 256 }}
        mode={this.state.mode}
        theme={this.state.theme}
        triggerSubMenuAction={"click"}
    >
    
    .....
    

    https://codesandbox.io/s/awesome-waterfall-ml8vyj?file=/src/index.js