admin管理员组文章数量:1330434
Let's say I have a simple code like:
<ListItem
button={true}
>
<Typography variant='caption' color='primary'>
{value}
</Typography>
<Button
onClick={foo}
>
Button
</Button>
</ListItem>
When I click anything inside the ListItem the ripple effect is trigger, which is ok, but when I click the button I don't want the ripple effect of the parent ponent to be triggered. How do I do that?
Let's say I have a simple code like:
<ListItem
button={true}
>
<Typography variant='caption' color='primary'>
{value}
</Typography>
<Button
onClick={foo}
>
Button
</Button>
</ListItem>
When I click anything inside the ListItem the ripple effect is trigger, which is ok, but when I click the button I don't want the ripple effect of the parent ponent to be triggered. How do I do that?
Share edited Jun 5, 2019 at 8:42 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 4, 2019 at 16:23 LekLek 13712 bronze badges3 Answers
Reset to default 6You can try to use disableRipple
property of ListItem
. Set it to true
when clicking on button and set it to false
when clicking on list item, something like:
const foo = () => this.setState(prevState => ({
...prevState,
parentDisableRipple: true
}));
const enableRipple = () => this.state.parentDisableRipple && this.setState(prevState => ({
...prevState,
parentDisableRipple: false
}));
return (
<div>
<Hello name={this.state.name} />
<p>
Start editing to see some magic happen :)
</p>
<ListItem button={true}
disableRipple={this.state.parentDisableRipple}
onClick={enableRipple()}>
<Typography variant='caption' color='primary'>
{value}
</Typography>
<Button onClick={foo} >Button</Button>
</ListItem>
</div>
);
I created a STACKBLITZ to play with
UPDATE
There is a better solution by @Domino987 using onMouseDown
and event.stopPropagation()
(already mentioned here by @vasanthcullen) and <ListItemSecondaryAction>
wrapper.
I updated my STACKBLITZ with both these solutions
Use event.stopPropagation() inside the click handler of the button. In your case, inside the foo() function
<ListItem button={true} matRipple #parent>
<Typography variant='caption' color='primary' matRipple [matRippleTrigger]="parent"
[matRippleDisabled]="true">
{value}
</Typography>
<Button onClick={foo}>
Button
</Button>
</ListItem>
You need something like this: Add Template variable that element which should be animated. And trigger parent ripple by children with [matRippleTrigger] option.
本文标签: javascriptStop parent component ripple being triggered from child componentStack Overflow
版权声明:本文标题:javascript - Stop parent component ripple being triggered from child component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742261796a2442650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论