Skip to content

Commit

Permalink
[TreeView] Set focus on the focused Tree Item instead of the Tree View (
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Mar 19, 2024
1 parent 1eec2ea commit eeb9603
Show file tree
Hide file tree
Showing 17 changed files with 827 additions and 856 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ you can use the new `onItemSelectionToggle` prop which is called whenever an ite

:::

### Focus the Tree Item instead of the Tree View

The focus is now applied to the Tree Item root element instead of the Tree View root element.

This change will allow new features that require the focus to be on the Tree Item,
like the drag and drop reordering of items.
It also solves several issues with focus management,
like the inability to scroll to the focused item when a lot of items are rendered.

This will mostly impact how you write tests to interact with the Tree View:

For example, if you were writing a test with `react-testing-library`, here is what the changes could look like:

```diff
it('test example on first item', () => {
const { getByRole } = render(
<SimpleTreeView>
<TreeItem itemId="one">One</TreeItem>
<TreeItem itemId="two">Two</TreeItem>
</SimpleTreeView>
);
- const tree = getByRole('tree');
+ const treeItem = getByRole('treeitem', { name: 'One' });
act(() => {
- tree.focus();
+ treeItem.focus();
});
- fireEvent.keyDown(tree, { key: 'ArrowDown' });
+ fireEvent.keyDown(treeItem, { key: 'ArrowDown' });
})
```

### ✅ Use `useTreeItemState` instead of `useTreeItem`

The `useTreeItem` hook has been renamed `useTreeItemState`.
Expand Down

0 comments on commit eeb9603

Please sign in to comment.