【问题标题】:Nesting buttons in Material UI: how to disable ripple effect of container button while clicking on a child button?Material UI 中的嵌套按钮:如何在单击子按钮时禁用容器按钮的波纹效果?
【发布时间】:2019-08-06 10:44:32
【问题描述】:

我正在尝试将一个按钮嵌套到另一个按钮中(IconButtonListItem 内,button 道具)。问题是即使我单击IconButton,也会触发ListItem 涟漪动画。我希望它仅在我直接单击 ListItem 元素时触发

我试过IconButton的绝对定位,但没有帮助

example

【问题讨论】:

标签: javascript reactjs material-ui


【解决方案1】:

你可以这样做:

function App() {
  const mouseDown = e => {
    e.stopPropagation ();
  }
  return (
    <ListItem button>
      Some text
      <IconButton onMouseDown={mouseDown}>
        <Favorite />
      </IconButton>
    </ListItem>
  );
}

或者通过将 Button 包裹在 &lt;ListItemSecondaryAction&gt; 中,这也会禁用波纹效果,但会将图标移动到最终项目,这可以通过一些 css 来修复。

希望这会有所帮助。快乐编码

【讨论】:

  • 有可能 - 请在此处查看我的答案和 STACKBLITZ 示例 - stackoverflow.com/questions/56447828/…
  • 我还使用 onMouseDown 添加了一个更简单的解决方案。
  • @Andriy,不幸的是,您的 STACKBLITZ 对我不起作用。但我添加的 onMouseDown 解决方案有效。
  • 我同意,您的解决方案更好@Domino987,我更新了指向您解决方案的原始答案。感谢分享!
猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 2021-06-26
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2015-07-21
相关资源
最近更新 更多