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

[TreeView] Fix focus steal #20232

Merged
merged 4 commits into from
Mar 27, 2020
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
42 changes: 41 additions & 1 deletion packages/material-ui-lab/src/TreeItem/TreeItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { spy } from 'sinon';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '@material-ui/core/test-utils/describeConformance';
import { createEvent, createClientRender, fireEvent } from 'test/utils/createClientRender';
import { act, createEvent, createClientRender, fireEvent } from 'test/utils/createClientRender';
import TreeItem from './TreeItem';
import TreeView from '../TreeView';

Expand Down Expand Up @@ -1005,4 +1005,44 @@ describe('<TreeItem />', () => {
fireEvent(input, keydownEvent);
expect(keydownEvent.preventDefault.callCount).to.equal(0);
});

it('should not focus steal', () => {
let setActiveItemMounted;
// a TreeItem whose mounted state we can control with `setActiveItemMounted`
function ControlledTreeItem(props) {
const [mounted, setMounted] = React.useState(true);
setActiveItemMounted = setMounted;

if (!mounted) {
return null;
}
return <TreeItem {...props} />;
}
const { getByText, getByTestId, getByRole } = render(
<React.Fragment>
<button type="button">Some focusable element</button>
<TreeView>
<TreeItem nodeId="one" label="one" data-testid="one" />
<ControlledTreeItem nodeId="two" label="two" data-testid="two" />
</TreeView>
</React.Fragment>,
);

fireEvent.click(getByText('two'));

expect(getByTestId('two')).to.have.focus;

getByRole('button').focus();

expect(getByRole('button')).to.have.focus;

act(() => {
setActiveItemMounted(false);
});
act(() => {
setActiveItemMounted(true);
});

expect(getByRole('button')).to.have.focus;
});
});
16 changes: 12 additions & 4 deletions packages/material-ui-lab/src/TreeView/TreeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
...other
} = props;
const [tabbable, setTabbable] = React.useState(null);
const [focused, setFocused] = React.useState(null);
const [focusedNodeId, setFocusedNodeId] = React.useState(null);
Copy link
Member

Choose a reason for hiding this comment

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

focused implies that this component is focused but it's actually for tracking other components. For example FormControl also has a focused state but that has a different meaning.


const nodeMap = React.useRef({});

const firstCharMap = React.useRef({});
const visibleNodes = React.useRef([]);

Expand Down Expand Up @@ -88,7 +89,7 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
);

const isTabbable = (id) => tabbable === id;
const isFocused = (id) => focused === id;
const isFocused = (id) => focusedNodeId === id;

/*
* Node Helpers
Expand Down Expand Up @@ -129,7 +130,7 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
const focus = (id) => {
if (id) {
setTabbable(id);
setFocused(id);
setFocusedNodeId(id);
}
};

Expand Down Expand Up @@ -181,7 +182,7 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
* Expansion Helpers
*/

const toggleExpansion = (event, value = focused) => {
const toggleExpansion = (event, value = focusedNodeId) => {
let newExpanded;
if (expanded.indexOf(value) !== -1) {
newExpanded = expanded.filter((id) => id !== value);
Expand Down Expand Up @@ -431,6 +432,13 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
}
});
nodeMap.current = newMap;

setFocusedNodeId((oldFocusedNodeId) => {
if (oldFocusedNodeId === id) {
return null;
}
return oldFocusedNodeId;
});
},
[getNodesToRemove],
);
Expand Down