简体   繁体   English

单击图标时的下拉菜单 - ReactJS

[英]Drop down menu when click on the icon- ReactJS

I have a Navbar and i am using the condition that if userstatus is true(ie user is signedin) then it will show the user-icon otherwise signin , i am looking to achieve that when i click in the user-icon or singin , it will show the drop down menu where i will later use it for logout button.我有一个导航Navbar ,我使用的条件是如果用户userstatus为真(即用户已登录),那么它将显示user-icon ,否则signin ,我希望在单击user-iconsingin时实现这一点,它将显示下拉菜单,稍后我将在其中将其用于注销按钮。 i am struggling to find how will i achieve this, i can see at https://react-bootstrap.github.io/components/dropdowns/ that i can implement but here i want to use my own icon(glyphicon glyphicon-user) rather the rectangle box .我正在努力寻找我将如何实现这一点,我可以在https://react-bootstrap.github.io/components/dropdowns/看到我可以实现但在这里我想使用我own icon(glyphicon glyphicon-user)而是rectangle box

在此处输入图像描述

  • I am looking to achieve the below but instead of Dropdown button i am looking to use the user-icon我希望实现以下目标,但我希望使用user-icon而不是Dropdown button

下拉按钮

any suggestion/pointers please.请有任何建议/指点。

-Snippet of code - 代码片段

 <li>
             <a className="active" href="/signin">
               {userstatus ? (
                 <a
                   className=" signin-icon glyphicon glyphicon-user    
   "
                 ></a>
               ) : (
                 <a>SIGNIN</a>
               )}
             </a>
             
</li>


First solution which i don't recommend is to over ride the bootstrap css which will be alot and hustle you don't need starting from resting all the css and !important alot of css我不推荐的第一个解决方案是超越引导程序 css ,这将是你不需要从休息所有 css 和!important的很多 ZC7A628CBA22E28EB17B5F5C7A628CBA22E28EB17B5F5

second choice is to use your own dropdown which i find it lot easier than doing all this previous solution hustle.第二个选择是使用你自己的下拉菜单,我发现它比做所有这些以前的解决方案更容易。

incase you are using react hooks for ex:如果您正在使用反应钩子,例如:

const [isDropdownOpend , setDropdownOpen ] = useState(false);
const [list , setList] = useState([1,2,3])
const toggleDropDown = () => setDropdownOpend = !isDropdownOpend
const DropDownlist = () =>  useMemo(() =>list.map(el=><div>el</div>)
return (
<>
<button class="signin-icon glyphicon glyphicon-user  " onClick={toggleDropDown}>
{isDropdownOpend  ?DropDownlist():false}
</>
)

Add these class here在此处添加这些 class

<Dropdown.Toggle variant="light" className="bg-white border-0 p-0" variant="success" id="dropdown-basic">
    <ADD YOUR ICON HERE>
  </Dropdown.Toggle>

css css

.dropdown-toggle:after {
   border: 0;
}

based on the above link you mentioned you can change here:根据您提到的上述链接,您可以在此处更改:

<Dropdown>
  <Dropdown.Toggle variant="success" id="dropdown-basic">
    {* you icon code here *} (//in my case I used <FaBeer /> )
  </Dropdown.Toggle>

  <Dropdown.Menu>
    <Dropdown.Item href="#/action-1">Action</Dropdown.Item>
    <Dropdown.Item href="#/action-2">Another action</Dropdown.Item>
    <Dropdown.Item href="#/action-3">Something else</Dropdown.Item>
  </Dropdown.Menu>
</Dropdown>

and change the actions like you wanted inside Dropdown.Menu tag并在 Dropdown.Menu 标记中更改您想要的操作

There are various ways to achieve this.有多种方法可以实现这一目标。 A little bit of googling will reveal a lot of those ways.一点点谷歌搜索将揭示很多这些方式。 However, you can do this with pure css by using a div and having menu items and menu button inside it.但是,您可以通过使用 div 并在其中包含菜单项和菜单按钮来使用纯 css 来执行此操作。 Menu items will remain hidden as long as the parent div isn't hovered.只要父 div 没有悬停,菜单项就会保持隐藏状态。

 .dropdown { width: 100px; background: purple; }.dropdown.item { display: none; }.dropdown:hover.item{ display: block; }
 <div class="dropdown"> <button> Button </button> <div class="item"> <p> item 1 </p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM