Skip to content

Commit

Permalink
docs(material/tree): fix keyboard issues with "Load more flat tree"
Browse files Browse the repository at this point in the history
Fix keyboard navigation issues with the "Load more flat tree" demo.

 - "Load more" button will respond to Enter or Space keys
 - Expanding and collapsing using keyboard shortcuts will trigger
   loading.
  • Loading branch information
zarend committed Aug 29, 2023
1 parent e65f49a commit 10750e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</mat-tree-node>

<!-- expandable node -->
<mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding matTreeNodeToggle>
<mat-tree-node *matTreeNodeDef="let node; when: hasChild" matTreeNodePadding matTreeNodeToggle
(expandedChange)="loadChildren(node)">
<button mat-icon-button
[attr.aria-label]="'Toggle ' + node.item"
(click)="loadChildren(node)"
matTreeNodeToggle>
<mat-icon class="mat-icon-rtl-mirror">
{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
Expand All @@ -19,7 +19,8 @@
</mat-tree-node>

<mat-tree-node *matTreeNodeDef="let node; when: isLoadMore" matTreeNodeToggle>
<button mat-button (click)="loadMore(node.loadMoreParentItem)">
<button mat-button (click)="loadMore(node.loadMoreParentItem)"
(keydown)="loadMoreOnEnterOrSpace($event, node.loadMoreParentItem)">
Load more...
</button>
</mat-tree-node>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {MatTreeFlatDataSource, MatTreeFlattener, MatTreeModule} from '@angular/m
import {BehaviorSubject, Observable} from 'rxjs';
import {MatIconModule} from '@angular/material/icon';
import {MatButtonModule} from '@angular/material/button';
import {ENTER, SPACE} from '@angular/cdk/keycodes';

const LOAD_MORE = 'LOAD_MORE';

Expand Down Expand Up @@ -162,6 +163,16 @@ export class TreeLoadmoreExample {
this._database.loadMore(item);
}

loadMoreOnEnterOrSpace(event: KeyboardEvent, item: string) {
if (event.keyCode === ENTER || event.keyCode === SPACE) {
this._database.loadMore(item);

// Prevent default behavior so that the tree node doesn't handle the keypress instead of this
// button.
event.preventDefault();
}
}

loadChildren(node: LoadmoreFlatNode) {
this._database.loadMore(node.item, true);
}
Expand Down

0 comments on commit 10750e6

Please sign in to comment.