简体   繁体   English

如何使用三元运算符使用反应?

[英]How to use ternary operator using react?

i want to display a popup like in picture below when hovering on buttons using react and typescript.当使用 react 和 typescript 将鼠标悬停在按钮上时,我想显示如下图所示的弹出窗口。

在此处输入图像描述

below is my code,下面是我的代码,

function Parent () {
    return (
        <ItemsList>
            {filtereditems.map(item => (
                <Item 
                    key={item.id}
                    item={item}
                />
            ))}
        </ItemsList>
    );
}


const ItemsList = styled.div`
    display: flex;
    flex-direction: row;  
`;


function Item ({item}: Props) { //this displays the button like components in picture
    return(
        <Wrapper>
            {getShortName(item)}
        </Wrapper>
    );
}


const Wrapper = styled(Icon)`
    z-index: 1;
    font-size: 14px;
`;


 const Icon = styled.div<Props>`
     width: ${p => (p.size ? `${p.size}px` : '20px')};
     height: ${p => (p.size ? `${p.size}px` : '20px')};
     flex-shrink: 0;
     text-align: center;
     display: flex;
     justify-content: center;
     align-items: center;
`;

Now when i hover on Item component i want to get the popup like in picture above.现在,当我在 Item 组件上使用 hover 时,我想获得如上图所示的弹出窗口。 could someone help me with this.有人可以帮我解决这个问题。 thanks.谢谢。

Not sure where the content of the popup should come (I assume getShortName() just shows the button content), however, with the solution below you can solve the problem whatever structure it has.不确定弹出窗口的内容应该来自哪里(我假设getShortName()只显示按钮内容),但是,使用下面的解决方案,您可以解决任何结构的问题。 If you have a span or whatever inside as the popup/tooltip text, it can be solved like:如果您有一个span或任何内部作为弹出/工具提示文本,它可以像这样解决:

function Item ({item}: Props) { //this displays the button like components in picture
    return(
        <Wrapper>
            {getShortName(item)}
            <span id='popupOrTooltip'>Whatever you like</span>
        </Wrapper>
    );
}


const Wrapper = styled(Icon)`
    z-index: 1;
    font-size: 14px;
`;


 const Icon = styled.div<Props>`
     width: ${p => (p.size ? `${p.size}px` : '20px')};
     height: ${p => (p.size ? `${p.size}px` : '20px')};
     flex-shrink: 0;
     text-align: center;
     display: flex;
     justify-content: center;
     align-items: center;
     & > span {
       opacity: 0;
       visibility: hidden;
     }
     &:hover > span {
       opacity: 1;
       visibility: visible;
     }

`;

The & operator is from SASS/SCSS, which means the parent selector, :hover is the psuedo for if the selector's element is hovered, > means direct child(ren). &运算符来自 SASS/SCSS,表示父选择器, :hover是选择器元素悬停时的伪, >表示直接子选择器。

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

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