Skip to content

Commit

Permalink
[Breadcrumbs] Keep focus in the component after expanding (#20489)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerryy authored Apr 10, 2020
1 parent 89d7ad0 commit 991f6af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion packages/material-ui/src/Breadcrumbs/Breadcrumbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ const Breadcrumbs = React.forwardRef(function Breadcrumbs(props, ref) {
const [expanded, setExpanded] = React.useState(false);

const renderItemsBeforeAndAfter = (allItems) => {
const handleClickExpand = () => {
const handleClickExpand = (event) => {
setExpanded(true);

// The clicked element received the focus but gets removed from the DOM.
// Let's keep the focus in the component after expanding.
const focusable = event.currentTarget.parentNode.querySelector('a[href],button,[tabindex]');
if (focusable) {
focusable.focus();
}
};

// This defends against someone passing weird input, to ensure that if all
Expand Down
6 changes: 3 additions & 3 deletions packages/material-ui/src/Breadcrumbs/Breadcrumbs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('<Breadcrumbs />', () => {
});

it('should expand when `BreadcrumbCollapsed` is clicked', () => {
const { getAllByRole, getByRole } = render(
const { getAllByRole, getByRole, getByText } = render(
<Breadcrumbs>
<span>first</span>
<span tabIndex="-1">first</span>
<span>second</span>
<span>third</span>
<span>fourth</span>
Expand All @@ -79,7 +79,7 @@ describe('<Breadcrumbs />', () => {
);

getByRole('button').click();

expect(document.activeElement).to.equal(getByText('first'));
expect(getAllByRole('listitem', { hidden: false })).to.have.length(9);
});

Expand Down

0 comments on commit 991f6af

Please sign in to comment.