For some reason, when I change material-ui <SpeedDial>
to remove the prop onMouseEnter={handleOpen}
so that the speed dial only opens upon clicking the FAB instead of on hover, the onClick
event in <SpeedDialAction>
does not get triggered when I click a speed dial menu item. I feel like I’m missing something fundamental here.
JavaScript
x
29
29
1
return (
2
<SpeedDial
3
ariaLabel="Add"
4
className={classes.root}
5
icon={<SpeedDialIcon />}
6
onClick={handleClick}
7
onClose={handleClose}
8
onBlur={handleClose}
9
// onMouseEnter={handleOpen}
10
// onMouseLeave={handleClose}
11
open={open}
12
direction={mobile ? 'up' : 'down'}
13
>
14
{actions.map(action => (
15
<SpeedDialAction
16
key={action.name}
17
icon={action.icon}
18
tooltipTitle={action.name}
19
tooltipOpen
20
classes={{ staticTooltipLabel: classes.staticTooltipLabel }}
21
onClick={e => {
22
e.preventDefault();
23
alert('x');
24
}}
25
/>
26
))}
27
</SpeedDial>
28
);
29
Advertisement
Answer
Using preventDefault
will not cause the click
event to not propagate to the parent element (which from what I understand is what you are trying to get).
You should use the stopPropagation
instead:
JavaScript
1
21
21
1
<SpeedDial
2
ariaLabel="Add"
3
className={classes.SpeedDial}
4
icon={<SpeedDialIcon />}
5
onClick={handleClick}
6
open={open}
7
>
8
{actions.map(action => (
9
<SpeedDialAction
10
key={action.name}
11
icon={action.icon}
12
tooltipTitle={action.name}
13
tooltipOpen
14
onClick={e => {
15
e.stopPropagation();
16
alert("x");
17
}}
18
/>
19
))}
20
</SpeedDial>
21
Check the following example: https://codesandbox.io/s/speeddial-open-on-click-rleg5?file=/demo.js