Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a icon to React Bootstrap Dropdown

I'm trying to add an icon to my react dropdown button as shown in the following button.

enter image description here

I couldn't find a way to implement this with the react bootstrap package i'm using.

https://react-bootstrap.github.io/

I tried using the normal bootstrap 4 to to this. But found out that it needs jquery to open the dropdown menu. How can i add a icon to my react bootstrap drop down?

My code

   <DropdownButton id="example-month-input-2" title={this.state.weekselectedType}>
         <Dropdown.Item onClick={() => this.changeWeekValue('a')}>A</Dropdown.Item>
         <Dropdown.Item onClick={() => this.changeWeekValue('b')}>B</Dropdown.Item>
         <Dropdown.Item onClick={() => this.changeWeekValue('c')}>C</Dropdown.Item>
   </DropdownButton>

I managed to remove the default dropdown icon using the following css

.dropdown-toggle::after {
    display:none !important;
}
like image 579
CraZyDroiD Avatar asked Oct 29 '19 05:10

CraZyDroiD


People also ask

How do I add a dropdown icon in react?

To place the icon on a DropDownButton, set the iconCss property to e-icons with the required icon CSS. By default, the icon is positioned to the left side of the DropDownButton. You can customize the icon's position by using the iconPosition property.

Can I use bootstrap icons in react?

Viewing and using a React Bootstrap iconYou need to import each icon from the installed react-bootstrap-icons package individually by using it's name. In VS Code, as you start typing the name within the curly brackets, you'll see an autocomplete list come up of all of the available icons.

How do I add a button to a drop down list?

Example Explained. Use 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 use bootstrap icons in react app?

The solution to How To Use Bootstrap Icons In React Components will be demonstrated using examples in this article. import React from 'react'; import * as Icon from 'react-bootstrap-icons'; export default function App() => { return <Icon. ArrowRight /> };


2 Answers

React Bootstrap allows you to customise Dropdown by passing in custom subcomponents. To customise the toggle button, you can use:

// The forwardRef is important!!
// Dropdown needs access to the DOM node in order to position the Menu
const CustomToggle = React.forwardRef(({ children, onClick }, ref) => (
  <a
    href=""
    ref={ref}
    onClick={e => {
      e.preventDefault();
      onClick(e);
    }}
  >
    {/* Render custom icon here */}
    &#x25bc;
    {children}
  </a>
));

render(
  <Dropdown>
    <Dropdown.Toggle as={CustomToggle} id="dropdown-custom-components">
      Custom toggle
    </Dropdown.Toggle>

    <Dropdown.Menu>
      <Dropdown.Item eventKey="1">Red</Dropdown.Item>
      <Dropdown.Item eventKey="2">Blue</Dropdown.Item>
      <Dropdown.Item eventKey="3" active>
        Orange
      </Dropdown.Item>
      <Dropdown.Item eventKey="1">Red-Orange</Dropdown.Item>
    </Dropdown.Menu>
  </Dropdown>,
);

Docs

like image 133
Agney Avatar answered Nov 10 '22 06:11

Agney


here's with the Icon

<DropdownButton id="example-month-input-2" title= 
{this.state.weekselectedType}>
     <Dropdown.Item onClick={() => this.changeWeekValue('a')}><i 
 class="fa fa-chevron-down"></i></Dropdown.Item>
     <Dropdown.Item onClick={() => 
this.changeWeekValue('b')}>B</Dropdown.Item>
     <Dropdown.Item onClick={() => 
this.changeWeekValue('c')}>C</Dropdown.Item>
</DropdownButton>

font-awesome

like image 2
CERFECTUS Avatar answered Nov 10 '22 08:11

CERFECTUS