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] Correct visibleNodes on re-render #20157

Merged
merged 2 commits into from
Mar 29, 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
39 changes: 39 additions & 0 deletions packages/material-ui-lab/src/TreeItem/TreeItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,45 @@ describe('<TreeItem />', () => {
expect(getByTestId('two')).to.have.focus;
});

it('moves focus to a child node works with a dynamic tree', () => {
function TestComponent() {
const [hide, setState] = React.useState(false);

return (
<React.Fragment>
<button
data-testid="button"
type="button"
onClick={() => setState(value => !value)}
>
Toggle Hide
</button>
<TreeView defaultExpanded={['one']}>
{!hide && (
<TreeItem nodeId="one" label="one" data-testid="one">
<TreeItem nodeId="two" label="two" data-testid="two" />
</TreeItem>
)}
<TreeItem nodeId="three" label="three" />
</TreeView>
</React.Fragment>
);
}

const { queryByTestId, getByTestId } = render(<TestComponent />);

expect(getByTestId('one')).to.not.be.null;
fireEvent.click(getByTestId('button'));
expect(queryByTestId('one')).to.be.null;
fireEvent.click(getByTestId('button'));
expect(getByTestId('one')).to.not.be.null;

getByTestId('one').focus();
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });

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

it("moves focus to a parent's sibling", () => {
const { getByTestId, getByText } = render(
<TreeView defaultExpanded={['one']}>
Expand Down
3 changes: 1 addition & 2 deletions packages/material-ui-lab/src/TreeView/TreeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
if (index === 0) {
setTabbable(id);
}
nodeMap.current[id] = { parent: null };
});
visibleNodes.current = nodeMap.current[-1].children;
prevChildIds.current = childIds;
Expand All @@ -480,7 +479,7 @@ const TreeView = React.forwardRef(function TreeView(props, ref) {
if (childrenCalculated) {
visibleNodes.current = buildVisible(nodeMap.current[-1].children);
}
}, [expanded, childrenCalculated, isExpanded]);
}, [expanded, childrenCalculated, isExpanded, children]);

return (
<TreeViewContext.Provider
Expand Down