Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make react-bootstrap's Dropdown open on mouse hover?

Tags:

Ok the question is simple. I need to make the dropdown open when mouse hover rather than on click.

Currently my code is as follow.

    <Nav>
      <NavDropdown
        onMouseEnter = {()=> isOpen=true}
        open={isOpen}
        noCaret
        id="language-switcher-container"
      >
        <MenuItem>Only one Item</MenuItem>
      </NavDropdown>
    </Nav>

as you can see, I tried onMouseEnter but no effect. Can someone solve this issue? What attribute should I pass in to achive this.

The API page is here react-bootstrap API page

like image 766
W.Chen Avatar asked Mar 24 '17 23:03

W.Chen


People also ask

How can add hover effect in Bootstrap dropdown?

Answer: Use the jQuery hover() method By default, to open or display the dropdown menu in Bootstrap you have to click on the trigger element. However, if you want to show the dropdown on mouseover instead of click you can do it with little customization using the CSS and jQuery.

How do I make Bootstrap dropdown stay open?

Just wrap the contents of the menu in a form tag. That's it. So, instead of ul. dropdown-menu , use form.

How do you make a drop down hover?

Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

How do I create a dynamic drop down list with react Bootstrap?

To create a dynamic drop down list with React Bootstrap, we can call the option array's map method to return the option element for each option in the array. We call options. map with a callback that returns the option element by setting the value prop to id and the text content to name .


2 Answers

export class Nav extends React.Component {

  constructor(props) {
    super(props)
    this.state = { isOpen: false }
  }

  handleOpen = () => {
    this.setState({ isOpen: true })
  }

  handleClose = () => {
     this.setState({ isOpen: false })
  }

  render() {
    return (
       <Nav>
        <NavDropdown
          onMouseEnter = { this.handleOpen }
          onMouseLeave = { this.handleClose }
          open={ this.state.isOpen }
          noCaret
          id="language-switcher-container"
        >
          <MenuItem>Only one Item</MenuItem>
        </NavDropdown>
      </Nav>
    )
  }
}

Hope this solves your issue.

like image 116
Rei Dien Avatar answered Sep 17 '22 15:09

Rei Dien


A much cleaner pure CSS solution here:

.dropdown:hover .dropdown-menu { display: block; }

like image 24
Gary Vernon Grubb Avatar answered Sep 17 '22 15:09

Gary Vernon Grubb