react-component / react-component/tree-select

Focus and keyboard navigation do not work in search results when disabled parents

Open
#652 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
312
Forks
204
Avg merge
37m
Merged PRs (30d)
1

Description

What is expected?

When the tree select search is used, focus is given to the first element, users can press 'Enter' to select it or use 'up' and 'down' keys to navigate other search results

Disabled elements in the search should not be given focus, but if their children are enabled their children should still be focused in search results

What is actually happening?

Any element with a disabled parent somewhere in their hierarchy is not given focus when searching, 'Enter' key will select the first element in the tree instead

This is bad UX but also not good for accessibility

I am using master from this repo during testing

Steps to reproduce
I have a demo sandbox here made with AntD but exact behaviour comes from rc-tree-select, I can adjust the demo if needed: https://codesandbox.io/p/devbox/confident-resonance-8jv7cz

The demo shows two cases, one working (No disabled parents) and one broken (Disabled parent)

To show the component not working:

Click on the treeselect search
Search for '1' (Or '2', or '3')
The child element gets focus, pressing enter on keyboard does nothing

To show the component working as expected:

Click on the treeselect search
Search for '3' (Or '4', or '5')
The child element gets focus, pressing enter on keyboard selects element

Suggested code fix - even if a parent is disabled or not selectable, check children

// ========================== Get First Selectable Node ==========================
const getFirstMatchingNode = (nodes: EventDataNode<any>[]): EventDataNode<any> | null => {
  for (const node of nodes) {
-    if (node.disabled || node.selectable === false) {
-      continue;
-    }
-
-    if (searchValue) {
-      if (filterTreeNode(node)) {
-        return node;
-      }
-    } else {
-      return node;
-    }
+    const isNodeSelectable = !node.disabled && node.selectable !== false;
+
+    if (isNodeSelectable) {
+      if (searchValue) {
+        if (filterTreeNode(node)) {
+          return node;
+        }
+      } else {
+        return node;
+      }
+    }
 
     if (node[fieldNames.children]) {
       const matchInChildren = getFirstMatchingNode(node[fieldNames.children]);
       if (matchInChildren) {
         return matchInChildren;
       }
     }
   }
   return null;
};

Demo before;

Image

Demo after;

Image

Initially created as an antD issue

Please let me know if I can provide any more information

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the getFirstMatchingNode entry point described in the issue and reproduce the behavior using the linked CodeSandbox, comparing disabled-parent and enabled-parent cases. Verify that matching enabled children can receive focus and be selected with Enter, then add or update regression coverage if the repository has relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
accessibility, frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.