Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions packages/mui-material/src/Unstable_TrapFocus/FocusTrap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,31 @@ describe('<FocusTrap />', () => {
expect(screen.getByRole('textbox')).toHaveFocus();
});

it('should keep focus inside when children have positive tabIndex', async () => {
render(
<div>
<input data-testid="outside-input" />
<FocusTrap open disableAutoFocus>
<div tabIndex={-1} data-testid="root">
<input data-testid="positive-tab" tabIndex={2} />
<button type="button" data-testid="normal-tab" />
</div>
</FocusTrap>
</div>,
);

await act(async () => {
screen.getByTestId('positive-tab').focus();
});
expect(screen.getByTestId('positive-tab')).toHaveFocus();

// Attempting to move focus outside should be trapped
await act(async () => {
screen.getByTestId('outside-input').focus();
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the fix works. The test is wrong since the input is focus()ed directly.

expect(screen.getByTestId('outside-input')).not.toHaveFocus();
});

it('should trap once the focus moves inside', async () => {
render(
<div>
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
return (
<React.Fragment>
<div
tabIndex={open ? 0 : -1}
tabIndex={open ? 1 : -1}
onFocus={handleFocusSentinel}
ref={sentinelStart}
data-testid="sentinelStart"
Expand Down
Loading