Skip to content

Commit 57a29b9

Browse files
committed
fix(material/tree): add aria-expanded to ngOnInit
added aria-expanded to ngOnInit to get rid of ExpressionChangedAfterItHasBeenCheckedError Fixes #21922
1 parent af58191 commit 57a29b9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/cdk/tree/tree.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
321321
exportAs: 'cdkTreeNode',
322322
host: {
323323
'class': 'cdk-tree-node',
324-
'[attr.aria-expanded]': 'isExpanded',
324+
'[attr.aria-expanded]': 'isLeafNode ? null : isExpanded',
325325
},
326326
standalone: true,
327327
})
@@ -388,10 +388,7 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
388388
return false;
389389
}
390390

391-
get isExpanded(): boolean | null {
392-
if (this.isLeafNode) {
393-
return null;
394-
}
391+
get isExpanded(): boolean {
395392
return this._tree.treeControl.isExpanded(this._data);
396393
}
397394

@@ -415,6 +412,11 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
415412
ngOnInit(): void {
416413
this._parentNodeAriaLevel = getParentNodeAriaLevel(this._elementRef.nativeElement);
417414
this._elementRef.nativeElement.setAttribute('aria-level', `${this.level + 1}`);
415+
if (!this.isLeafNode) {
416+
this._elementRef.nativeElement.setAttribute('aria-expanded', this.isExpanded.toString());
417+
} else {
418+
this._elementRef.nativeElement.removeAttribute('aria-expanded');
419+
}
418420
}
419421

420422
ngOnDestroy() {

0 commit comments

Comments
 (0)