react-arborist icon indicating copy to clipboard operation
react-arborist copied to clipboard

Tree.onSelect

Open jameskerr opened this issue 2 years ago • 5 comments

It might be cleaner to have an onSelect callback that can be placed on the root tree node. It only get's fired when an item gets selected. A group can only be selected during multi select.

New Api:

type TreeProps = {
  onSelect: () // only get's called when a single item is selected.
}

Then when the tree selects something programmatically, this code can run and does not need to be duplicated.

jameskerr avatar Mar 16 '22 23:03 jameskerr

This makes it easy for the click handlers too. We can have a default handlers.click function that checks if it's an item and runs on select, or checks if it's a folder and runs toggle.

<div onClick={handlers.onClick}>

We should prefix all the handlers with on so that you don't forget them.

jameskerr avatar Mar 16 '22 23:03 jameskerr

This is actually not the way to go, closing.

jameskerr avatar Mar 18 '22 17:03 jameskerr

This is actually not the way to go, closing.

Why not?

I am still trying to figure out how to trigger an event on node select. It's not obvious at all.

I have a prop that contains a function that I want to call. onSelect(({data}) => {}) feels the most natural.

vjpr avatar May 13 '22 12:05 vjpr

I'll reopen this then. Yeah, I can't think of an easy way to run a function when the node is selected. You'd have to put that code in the onClick handler of NodeRenderer probably, which is not very natural.

jameskerr avatar May 13 '22 22:05 jameskerr

+1 for this, first thing I was looking out was the onSelect handler from the Tree.

pgrodrigues avatar Aug 12 '22 20:08 pgrodrigues