如何在悬停时交换 MUI 图标上的 IconButton

huangapple go评论66阅读模式
英文:

How to swap MUI icon on IconButton when hovering

问题

我有这些标签,它们上面有一个关闭按钮,如果其中的内容已经被编辑过,那么关闭图标会变成一个圆圈,类似于Visual Studio Code。

我遇到的问题是如何在鼠标悬停在按钮上时将图标从圆圈更改为关闭图标。请问有人能帮我找到一个实现这个的好方法吗?

英文:

I have these tabs that have a close button on them, if the content in them has edits then the close icon turns to a circle, similar to visual code.

  {tabs.map((tab, index) => {
    const child = (
      <StyledTab
        label={
          <span>
            {tab.label + ':' + tab.hasEdit}
            <IconButton size="small" component="span" onClick={() => closeClickHandler(tab.value)}>
              {tab.hasEdit ? (
                <CircleIcon style={{ fontSize: "12px" }} />
              ) : (
                <CloseIcon style={{ fontSize: "18px" }} />
              )}
            </IconButton>
          </span>
        }
        value={tab.value}
        key={index}
      />
    );

    return (
      <DraggableTab
        label={
          <span>
            {tab.label}
            <IconButton size="small" component="span" onClick={() => {
              closeClickHandler(tab.value);
            }}>
              {tab.hasEdit ? (
                <CircleIcon style={{ fontSize: "12px" }} />
              ) : (
                <CloseIcon style={{ fontSize: "18px" }} />
              )}
            </IconButton>
          </span>
        }
        value={tab.value}
        index={index}
        key={index}
        child={child}
      />
    );
  })}

What I'm having trouble with is getting the icon to change from a circle to a close icon while hovering over the button.

Could someone give me a hand on a good way to implement this please?!

答案1

得分: 1

你可以通过为这些项目添加一个状态来实现这一点。然后在 IconButton 上添加 onMouseEnteronMouseLeave 事件。当鼠标悬停时,我们可以将索引添加到数组中,最后在离开时移除。要确定是否需要更改图标,我们可以检查索引是否在 hoveringItems 中。

const [hoveringItems, setHoveringItems] = useState([]);

function handleHover(index, isLeaving) {
  setHoveringItems((prevItems) => {
    if (isLeaving) return prevItems.filter((item) => item !== index);
    return [...prevItems, index];
  });
}

return (
  <IconButton
    size="small"
    component="span"
    onClick={() => {
      closeClickHandler(tab.value);
    }}
    onMouseEnter={() => handleHover(index, false)}
    onMouseLeave={() => handleHover(index, true)}
  >
    {tab.hasEdit || hoveringItems.includes(index) ? (
      <CircleIcon style={{ fontSize: "12px" }} />
    ) : (
      <CloseIcon style={{ fontSize: "18px" }} />
    )}
  </IconButton>
);
英文:

You could do this by adding a state for the items. Then add a onMouseEnter and onMouseLeave events on the IconButton. When hovering we can add the index to the array and finally remove when we're leaving. To determine if a icon needs to change we can check if the index in in the hoveringItems.

const [hoveringItems, setHoveringItems] = useState([]);

function handleHover(index, isLeaving) {
  setHoveringItems((prevItems) =&gt; {
    if (isLeaving) return prevItems.filter((item) =&gt; item !== index);
    return [...prevItems, index];
  });
}

return (
  &lt;IconButton
    size=&quot;small&quot;
    component=&quot;span&quot;
    onClick={() =&gt; {
      closeClickHandler(tab.value);
    }}
    onMouseEnter={() =&gt; handleHover(index, false)}
    onMouseLeave={() =&gt; handleHover(index, true)}
  &gt;
    {tab.hasEdit || hoveringItems.includes(index) ? (
      &lt;CircleIcon style={{ fontSize: &quot;12px&quot; }} /&gt;
    ) : (
      &lt;CloseIcon style={{ fontSize: &quot;18px&quot; }} /&gt;
    )}
  &lt;/IconButton&gt;
);

huangapple
  • 本文由 发表于 2023年2月16日 11:47:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定