hyper-tree
hyper-tree copied to clipboard
Listening for menu item click events
Hi, I really like your library. I couldn't find anything in the docs for this very basic use case - how can you handle the click of a menu item (and get the node id, etc. in the click event)?
@richjava Hi! You can overwrite the node renderer by renderNode
and do smth like
export const CustomNode = ({
node, // default prop (what you need)
onSelect, // default prop
onToggle, // default prop
onClick // custom prop
}) => {
const handleClick = useCallback(() => {
onSelect()
onClick(node)
}, [node, onSelect, onClick])
return (
<div onClick={handleClick}>
{...content}
</div>
)
}
Treeview:
<TreeView
{...otherOptions}
renderNode={(defaultProps) => <CustomNode {...defaultProps} onClick={yourCustomClickHandler} />}
/>
const yourCustomClickHandler = useCallback((node) => {
// your code
}, [])
Hey, I dont want to do custom rendering but just use the default rendering with a click handler. Is that possible?