shunjizhan / react-folder-tree

A versatile react treeview library that supports custom icons and event handlers
https://www.npmjs.com/package/react-folder-tree
119 stars 46 forks source link

Is there a way to get/add an onClick(event) for a node? (with the js event to get the mouse position) #72

Closed DespuesLeeWallace closed 2 years ago

DespuesLeeWallace commented 2 years ago

I need it to get the mouse coordinates when a node is clicked so I can show a menu.

shunjizhan commented 2 years ago

hey, please reference to this part of the doc: https://github.com/shunjizhan/react-folder-tree#-custom-onclick-for-node-names

DespuesLeeWallace commented 2 years ago

Hi, yeah sorry, I meant the event javascript object. The one that is used in mouse callbacks (ev,...), i.e. to get the mouse location on the screen, etc

shunjizhan commented 2 years ago

ummm I think the mouse location is not a concern of folder tree itself, so maybe it's better to use another lib to track it.

For example use this https://github.com/streamich/react-use/blob/master/docs/useMouse.md to track mouse location, and when the node is clicked, read the location from it

shunjizhan commented 2 years ago

something like

const {docX, docY, posX, posY, elX, elY, elW, elH} = useMouse(ref);

const onNameClick = ({ defaultOnClick, nodeData }) => {
  // use the mouse location from above
};
DespuesLeeWallace commented 2 years ago

mmm, I see.

I was thinking more of something like this:

const onNameClick = ({ defaultOnClick, nodeData, event }) => {
  // We can get all the usual properties (i.e. button, clientX, metaKey, movementX, region, target, etc)
}

To be able to access the event object like any other onClick callback lets you. Anyway, I added it in TreeNode:

const handleNameClick = (event) => {
    const defaultOnClick = selectMe;
    if (onNameClick && typeof onNameClick === "function") {
      !isEditing && onNameClick({ defaultOnClick, nodeData, event });
    } else {
      defaultOnClick();
    }
  };

But thanks for the tip, I didn't know about useMouse(). I am new to React and js