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

Is it possible to override the default 'Backspace' delete functionality?

Open connerkennedy32 opened this issue 1 year ago • 3 comments

I want to be able to rename the different nodes I'm using, but anytime a node is selected and I press backspace, it runs the onDelete function and wipes the node from the tree. Anyway to override this so backspace doesn't delete a node?

connerkennedy32 avatar Feb 17 '25 21:02 connerkennedy32

That's no fun.

There is no way to override it through a configuration (yet). It is coming in version 4.

For now, add an event listener for onKeyDown on your Node Renderer or Row Renderer, and call e.stopPropagation() if the key is the backspace key.

jameskerr avatar Feb 18 '25 17:02 jameskerr

Perfect. Thanks!

connerkennedy32 avatar Mar 18 '25 21:03 connerkennedy32

I also am trying to delete a node with a button press instead/in addition to the default backspace press. I ended up hacking it like this:

const triggerBackspace = (node) => {
    const event = new KeyboardEvent("keydown", {
        key: "Backspace",
        code: "Backspace",
        keyCode: 8,     
        charCode: 8, 
        which: 8,
        bubbles: true,
        cancelable: true,
    });

    node.dispatchEvent(event);
}

node.focus()
triggerBackspace(document.activeElement)

Let me know if there is a better way to do this.

smark-1 avatar Mar 19 '25 00:03 smark-1