Hi,
I am trying to add a right click context menu to the header of a column in the React Grid. Note: The grid is about 500 pixels down the page so it needs to be scrolled to be within view (I think this may be relevant).
I am adding the headerCell:
headerCell={GridColumnHeader}
With this header:
const GridColumnHeader = (props) => {
return (
<div className="k-link" onClick={props.onClick}
onContextMenu={(e) => {
e.preventDefault()
handleContext(e, props.field)
}}
>
{props.title}
{props.children}
</div>
);
};
The problem is in the handleContext menu, the clientX and clientY coordinates seem off, it looks initially like they aren't taking into account the position the page is in when scrolled.
function handleContext(e, field) {
offSet.current = {
// left: e.clientX,
// top: e.clientY,
left: e.pageX,
top: e.pageY,
};
setShow(true);
}
As you can see, here I use pageX and pageY which seems more reliable. But then I get strange behavior when the menu is displayed and I click another column header (it can jump around a little but I think I might need to universally hide the menu when anything other than the menu is clicked).
Am I doing something wrong, or am I right to use this?
The popup code is below:
<Popup show={show} offset={offSet.current}>
<Menu
onSelect={() => setShow(false)}
vertical={true}
style={{
display: "inline-block",
}}
>
<MenuItem text="Item1">
<MenuItem text="Item1.1"/>
<MenuItem text="Item1.2">
<MenuItem text="Item1.2.1"/>
<MenuItem text="Item1.2.2"/>
</MenuItem>
</MenuItem>
<MenuItem text="Item2">
<MenuItem text="Item2.1"/>
<MenuItem text="Item2.2"/>
</MenuItem>
<MenuItem text="Item3"/>
</Menu>
</Popup>
I also have this to hide the menu when clicked outside:
React.useEffect(() => {
document.addEventListener("click", () => {
show ? setShow(false) : null;
});
}, [show]);