hyper-tree icon indicating copy to clipboard operation
hyper-tree copied to clipboard

Listening for menu item click events

Open richjava opened this issue 2 years ago • 2 comments

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 avatar Oct 07 '22 09:10 richjava

@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 
}, [])

alekseymakhankov avatar Oct 09 '22 19:10 alekseymakhankov

Hey, I dont want to do custom rendering but just use the default rendering with a click handler. Is that possible?

kmodexc avatar Dec 21 '23 20:12 kmodexc