Skip to content

Fix focus fallback on Backspace deletion of trailing nodes - #402

Closed
ccombe wants to merge 1 commit into
jameskerr:mainfrom
ccombe:fix/backspace-selection-fallback
Closed

ccombe wants to merge 1 commit into
jameskerr:mainfrom
ccombe:fix/backspace-selection-fallback

Conversation

@ccombe

@ccombe ccombe commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the focus fallback logic when deleting a multi-selection with Backspace, specifically when the selection extends to the end of the tree.

Follow-up to the review note on #399.

Problem

When multiple nodes were deleted using Backspace (ids.length > 1), handleBackspace searched for the next focus target among later siblings of mostRecentNode:

let nextFocus = tree.mostRecentNode;
while (nextFocus && nextFocus.isSelected) {
  nextFocus = nextFocus.nextSibling;
}
if (!nextFocus) nextFocus = tree.lastNode;
tree.focus(nextFocus, { scroll: false });
tree.delete(Array.from(ids));

If the selection included the end of the tree (for example, selecting the last two nodes), nextFocus.nextSibling reached the end and evaluated to null. The fallback then assigned nextFocus = tree.lastNode without checking whether lastNode was also selected. As a result, focus was placed directly on a node that was immediately destroyed by tree.delete(ids).

Similarly, in single-node deletion at the root level, sib || parent fell back to root (ROOT_ID), which is an internal virtual node not present in visibleNodes.

Fix

  • If searching nextSibling yields null or an already-selected node, findUnselectedFallback searches tree.visibleNodes:
    1. Searches forward after the highest selected index for the next unselected node.
    2. If the selection reaches the bottom of the tree, searches backward before the lowest selected index for the nearest preceding unselected node.
    3. If all nodes in the tree are selected, returns null (avoiding placing focus on deleted nodes).
  • In single-node deletion, guards parent && !parent.isRoot before falling back to node.prev if deleting the last root-level node.

Testing

  • Unit test added: "Backspace on multi-selection at the end of the tree falls back to an unselected node", asserting that deleting the last two nodes focuses the nearest preceding unselected node.
  • Unit test added: "Backspace on single node at end of root falls back to previous node rather than root".
  • All 137 unit tests and 20 e2e tests passing warning-clean.

When multiple nodes at the end of the tree were selected and deleted via
Backspace, the focus fallback logic checked tree.lastNode without
verifying whether it was also part of the selection. This caused focus
to land on a node that was immediately deleted.

When later siblings are unavailable or selected, findUnselectedFallback
now finds the nearest unselected node preceding or following the
selection. Also prevents single-node deletion from focusing the
invisible root node when deleting the last root-level node.
@netlify

netlify Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for react-arborist ready!

Name Link
🔨 Latest commit a94255c
🔍 Latest deploy log https://app.netlify.com/projects/react-arborist/deploys/6ae849a6a2a7a7b22968c965
😎 Deploy Preview https://deploy-preview-402--react-arborist.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

ccombe added a commit to ccombe/react-arborist that referenced this pull request Nov 7, 2026
…election deletion

When multiple nodes at the end of the tree are selected and deleted via
Backspace, the focus fallback logic checked tree.lastNode without
verifying whether it was also part of the selection. This caused focus
to land on a node that was immediately deleted.

When later siblings are unavailable or selected, findUnselectedFallback
now finds the nearest unselected node preceding or following the
selection. Also prevents single-node deletion from focusing the
invisible root node when deleting the last root-level node.

This folds jameskerr#402 into jameskerr#399.
@ccombe

ccombe commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #399 to reduce the number of open PRs under review.

@ccombe ccombe closed this Nov 7, 2026
@ccombe
ccombe deleted the fix/backspace-selection-fallback branch November 9, 2026 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant