Skip to content

Commit

Permalink
Merge pull request #88 from jamesplease/root-fixes
Browse files Browse the repository at this point in the history
Fix issue when mounting node tree with disabled node
  • Loading branch information
jamesplease committed Feb 11, 2022
2 parents 703755b + c8547b4 commit 91c96e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/tests/disabled-nodes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ describe('disabled FocusNodes', () => {

expect(warning).toHaveBeenCalledTimes(0);
});

it('does not warn when a focus tree mounts with a disabled child (gh-78)', () => {
render(
<FocusRoot>
<FocusNode focusId="parent">
<FocusNode focusId="A" disabled />
<FocusNode focusId="B" />
</FocusNode>
</FocusRoot>
);

expect(console.error).toHaveBeenCalledTimes(0);
});
});
7 changes: 4 additions & 3 deletions src/utils/enforce-state-structure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FocusState, Node, NodeMap, Id } from '../types';
import nodeCanReceiveIndirectFocus from './node-can-receive-indirect-focus';

function validateNode(node: Node, nodes: NodeMap) {
function validateNode(node: Node, nodes: NodeMap, focusState: FocusState) {
const enabledNodeChildren = node.children.filter((childId) => {
const node = nodes[childId];
return node && !node.disabled && !node.isExiting;
return nodeCanReceiveIndirectFocus(focusState, node);
});

if (enabledNodeChildren.length > 0 && node.isFocusedLeaf) {
Expand Down Expand Up @@ -74,7 +75,7 @@ export default function enforceStateStructure(focusState: FocusState) {

Object.values(focusState.nodes).forEach((node) => {
if (node) {
validateNode(node, focusState.nodes);
validateNode(node, focusState.nodes, focusState);
}
});
}

0 comments on commit 91c96e6

Please sign in to comment.