Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(react-tree): ensure roving tab index when children changes #31595

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
bsunderhus marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "bugfix: ensure roving tab index when children content changes",
"packageName": "@fluentui/react-tree",
"email": "bernardo.sunderhus@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'cypress-real-events';
import * as React from 'react';
import { mount as mountBase } from '@cypress/react';
import { FluentProvider } from '@fluentui/react-provider';
Expand Down Expand Up @@ -395,6 +396,35 @@ describe('Tree', () => {
cy.get('#btn-after-tree').should('be.focused').realPress(['Shift', 'Tab']);
cy.get('[data-testid="item2__item1"]').should('be.focused');
});
it('should ensure roving tab indexes when children change', () => {
const RovingTreeTest = () => {
const [show, setShow] = React.useState(false);
return (
<>
<button onClick={() => setShow(true)} id="btn-before-tree">
show tree
</button>
<TreeTest>
{show && (
<>
<TreeItem itemType="leaf" value="item1" data-testid="item1">
<TreeItemLayout>level 1, item 1</TreeItemLayout>
</TreeItem>
<TreeItem itemType="leaf" value="item2" data-testid="item2">
<TreeItemLayout>level 1, item 2</TreeItemLayout>
</TreeItem>
</>
)}
</TreeTest>
</>
);
};

mount(<RovingTreeTest />);
cy.get('#btn-before-tree').focus().realClick();
cy.get('[data-testid="item1"]').should('have.attr', 'tabindex', '0').focus().realPress('ArrowDown');
cy.get('[data-testid="item2"]').should('be.focused');
});
});

declare global {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import { elementContains } from '@fluentui/react-utilities';
* https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/#kbd_roving_tabindex
*/
export function useRovingTabIndex() {
const currentElementRef = React.useRef<HTMLElement>();
const currentElementRef = React.useRef<HTMLElement | null>(null);
const walkerRef = React.useRef<HTMLElementWalker | null>(null);

React.useEffect(() => {
if (currentElementRef.current === null && walkerRef.current) {
initialize(walkerRef.current);
}
});

useFocusedElementChange(element => {
if (
element?.getAttribute('role') === 'treeitem' &&
Expand Down
Loading