2

Created sample application with ReactJS and install reactstrap for boostrap. I use dropdown components in application that is working fine. code is

<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
    <DropdownToggle caret>
      Dropdown
    </DropdownToggle>
    <DropdownMenu>
      <DropdownItem>Another Action</DropdownItem>
      <DropdownItem>Another Action</DropdownItem>
    </DropdownMenu>
</Dropdown> 

but i have to change .

Like currently they passing text only

. But

I have to pass 3 things like icon,heading and sub heading

so can I change DropdownItem.js code for this type of customization ?

I have to make dropdown similar to this image http://prntscr.com/f34zoo

Thanks in advance

2 Answers 2

8

It is possible to have custom menu items:

As it is specified on the official website:

<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
   <span
      onClick={this.toggle}
      data-toggle="dropdown"
      aria-haspopup="true"
      aria-expanded={this.state.dropdownOpen}
      >
   Custom Dropdown Content
   </span>
   <DropdownMenu>
      <div onClick={this.toggle}>
          <i class="some-icon"/>
          <h3>Some heading</h3>
          <p>Some sub heading</p>
      </div>
      <div onClick={this.toggle}>
          <i class="some-icon"/>
          <h3>Some heading</h3>
          <p>Some sub heading</p>
      </div>
      <div onClick={this.toggle}>
          <i class="some-icon"/>
          <h3>Some heading</h3>
          <p>Some sub heading</p>
      </div>
   </DropdownMenu>
</Dropdown>
Sign up to request clarification or add additional context in comments.

3 Comments

Ok understand so we dont need to change DropdownItem.js for custom styled dropdown right?
yes you can pass down your HTML elements instead of just text, and then customize it with CSS.
Please check "Custom Dropdown" section.
5

There is a better way to do it. If you want the dropdown toggle to be like:

<div>
  <span>{selectedShop ? selectedShop.shopCity : ''}</span>
  <FaChevronDown />
</div>

Then your DropdownToggle should be like (see the tag attribute):

<DropdownToggle tag="div">
  <span>{selectedShop ? selectedShop.shopCity : ''}</span>
  <FaChevronDown />
</DropdownToggle>

The tag props takes the html tag for wrapping the dropdown toggle content. Now you can style ur div any way you want.

Note: By default the tag is a button.

Now, for your dropdown items. You can simply do something like this:

<DropdownItem onClick={() => onShopChange(shopToMap)}>
  <i class="some-icon" />
  <h3>Some heading</h3>
  <p>Some sub heading</p>{' '}
</DropdownItem>

Note: Even for DropdownItem you can specify tag prop.

2 Comments

very simple and clean solution
this should be the accepted answer, gives you total control on how the toggle is rendered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.