diff --git a/packages/material-ui-lab/src/TreeItem/TreeItem.test.js b/packages/material-ui-lab/src/TreeItem/TreeItem.test.js index bb4404b731179e..b1e9b616bba70d 100644 --- a/packages/material-ui-lab/src/TreeItem/TreeItem.test.js +++ b/packages/material-ui-lab/src/TreeItem/TreeItem.test.js @@ -484,6 +484,45 @@ describe('', () => { 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 ( + + + + {!hide && ( + + + + )} + + + + ); + } + + const { queryByTestId, getByTestId } = render(); + + 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( diff --git a/packages/material-ui-lab/src/TreeView/TreeView.js b/packages/material-ui-lab/src/TreeView/TreeView.js index 3ae76f20824dbc..4dec3ac52f7cae 100644 --- a/packages/material-ui-lab/src/TreeView/TreeView.js +++ b/packages/material-ui-lab/src/TreeView/TreeView.js @@ -468,7 +468,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; @@ -493,7 +492,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]); const noopSelection = () => { return false;