I think this is just an architecture problem. The click event happened, so no reason to filter it out. Double click is actually another immediate click under specific short time period.
So I would to react on click event with delay if double click didn't happened.
In case you want to prevent some internal action. In my case it was the drilling down when clicking on segments in the sunburst chart. I found the solution in their documentation (it is really neat and helpful).
It was just one property set to false
value :)
https://echarts.apache.org/en/option.html#series-sunburst.nodeClick
In another words I would to expect that the click will trigger twice as that is what double click means.
The (legacy react) code deals with it like this:
canvasWrapperRef.current.ondblclick = e => {
triggerOnDoubleClickEvent();
};
let dblclick_timeout = 0;
canvasWrapperRef.current.onclick = e => {
if (dblclick_timeout === 0) {
dblclick_timeout = setTimeout(() => {
dblclick_timeout = 0;
triggerOnClickEvent();
}, 400);
} else {
clearTimeout(dblclick_timeout);
dblclick_timeout = 0;
}
};
Refactored version for React
I've done the hook what make the code more tidy and increase readability.
The hook:
export default () => {
let doubleClickTimeout;
return (callbackDoubleClick, callbackSingleClick, doubleClickTimeoutDelay) => {
if (!doubleClickTimeout) {
doubleClickTimeout = setTimeout(() => {
doubleClickTimeout = 0;
callbackSingleClick();
}, doubleClickTimeoutDelay);
} else {
clearTimeout(doubleClickTimeout);
doubleClickTimeout = 0;
callbackDoubleClick();
}
};
};
And the code to use it is here:
import React from "react";
import useDoubleClickChecker from "./useDoubleClickChecker";
function Component(props) {
...
const doubleClickChecker = useDoubleClickChecker();
...
const [echartsEvetns] = React.useState({
click: event => {
doubleClickChecker(() => console.log("double click"), () => console.log("single click"), 400)
},
dblclick: event => {
console.log("double click");
},
});
...
return (
<ReactECharts
option={option}
onEvents={echartsEvetns}
/>
);
}
export default Component;
Now, you'll see the "double click" string in the browser's console twice. Is it not fun?
Just ignore one from the double click reaction.