Skip to content

Commit

Permalink
fix(TreeNode): preserve nested node expand state (#17630)
Browse files Browse the repository at this point in the history
* fix(TreeNode): preserve nested node expand state

* fix(TreeView):  prevent focus on hidden child nodes
  • Loading branch information
emyarod authored Oct 10, 2024
1 parent 8abaf92 commit af1cd1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
12 changes: 7 additions & 5 deletions packages/react/src/components/TreeView/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ const TreeNode = React.forwardRef<HTMLLIElement, TreeNodeProps>(
{label}
</span>
</div>
{expanded && (
<ul role="group" className={`${prefix}--tree-node__children`}>
{nodesWithProps}
</ul>
)}
<ul
role="group"
className={classNames(`${prefix}--tree-node__children`, {
[`${prefix}--tree-node--hidden`]: !expanded,
})}>
{nodesWithProps}
</ul>
</li>
);
}
Expand Down
18 changes: 14 additions & 4 deletions packages/react/src/components/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ const TreeView: TreeViewComponent = ({
event.shiftKey &&
event.ctrlKey &&
treeWalker.current.currentNode instanceof Element &&
!treeWalker.current.currentNode.getAttribute('aria-disabled')
!treeWalker.current.currentNode.getAttribute('aria-disabled') &&
!treeWalker.current.currentNode.classList.contains(
`${prefix}--tree-node--hidden`
)
) {
nodeIds.push(treeWalker.current.currentNode?.id);
}
Expand All @@ -244,7 +247,8 @@ const TreeView: TreeViewComponent = ({
event.shiftKey &&
event.ctrlKey &&
nextFocusNode instanceof Element &&
!nextFocusNode.getAttribute('aria-disabled')
!nextFocusNode.getAttribute('aria-disabled') &&
!nextFocusNode.classList.contains(`${prefix}--tree-node--hidden`)
) {
nodeIds.push(nextFocusNode?.id);
}
Expand All @@ -257,7 +261,10 @@ const TreeView: TreeViewComponent = ({
while (treeWalker.current.nextNode()) {
if (
treeWalker.current.currentNode instanceof Element &&
!treeWalker.current.currentNode.getAttribute('aria-disabled')
!treeWalker.current.currentNode.getAttribute('aria-disabled') &&
!treeWalker.current.currentNode.classList.contains(
`${prefix}--tree-node--hidden`
)
) {
nodeIds.push(treeWalker.current.currentNode?.id);
}
Expand Down Expand Up @@ -286,7 +293,10 @@ const TreeView: TreeViewComponent = ({
if (!(node instanceof Element)) {
return NodeFilter.FILTER_SKIP;
}
if (node.classList.contains(`${prefix}--tree-node--disabled`)) {
if (
node.classList.contains(`${prefix}--tree-node--disabled`) ||
node.classList.contains(`${prefix}--tree-node--hidden`)
) {
return NodeFilter.FILTER_REJECT;
}
if (node.matches(`li.${prefix}--tree-node`)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/styles/scss/components/treeview/_treeview.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
}
}

.#{$prefix}--tree-node--hidden {
display: none;
}

.#{$prefix}--tree-node__children {
@include component-reset.reset;

Expand Down

0 comments on commit af1cd1b

Please sign in to comment.