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

[Menu][Divider] Do not allow focus on Divider when inside Menu list #38102

Merged
merged 7 commits into from
Jul 28, 2023
6 changes: 6 additions & 0 deletions packages/mui-material/src/Divider/Divider.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ const Divider = React.forwardRef(function Divider(inProps, ref) {
);
});

/**
* The following flag is used to ensure that this component isn't tabbable i.e.
* does not get highlight/focus inside of MUI List.
*/
Divider.muiSkipListHighlight = true;

Divider.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/MenuList/MenuList.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ const MenuList = React.forwardRef(function MenuList(props, ref) {
if (autoFocusItem) {
newChildProps.autoFocus = true;
}

if (child.props.tabIndex === undefined && variant === 'selectedMenu') {
newChildProps.tabIndex = 0;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/mui-material/src/MenuList/MenuList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as React from 'react';
import { expect } from 'chai';
import { stub } from 'sinon';
import { describeConformance, createRenderer } from 'test/utils';
import Divider from '@mui/material/Divider';
import MenuList from '@mui/material/MenuList';
import MenuItem from '@mui/material/MenuItem';
import List from '@mui/material/List';
import getScrollbarSize from '../utils/getScrollbarSize';

Expand Down Expand Up @@ -44,6 +46,18 @@ describe('<MenuList />', () => {

expect(getAllByRole('menuitem')).to.have.length(2);
});

it('should not add tabIndex to presentation elements like Divider when all Menu Items are disabled', () => {
const { getByRole } = render(
<MenuList>
<MenuItem disabled>one</MenuItem>
<Divider />
<MenuItem disabled>two</MenuItem>
</MenuList>,
);

expect(getByRole('separator')).not.to.have.attribute('tabIndex');
});
});

describe('actions: adjustStyleForScrollbar', () => {
Expand Down