Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/tree): Apply aria-level to all nodes #17818

Merged
merged 6 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cdk/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ describe('CdkTree', () => {
})).toBe(true);
});

it('with the right aria-levels', () => {
// add a child to the first node
let data = dataSource.data;
dataSource.addChild(data[0], true);

const ariaLevels = getNodes(treeElement).map(n => n.getAttribute('aria-level'));
expect(ariaLevels).toEqual(['2', '3', '2', '2']);
});

it('with the right data', () => {
expect(dataSource.data.length).toBe(3);

Expand Down
13 changes: 6 additions & 7 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class CdkTree<T> implements AfterContentChecked, CollectionViewer, OnDest
exportAs: 'cdkTreeNode',
host: {
'[attr.aria-expanded]': 'isExpanded',
'[attr.aria-level]': 'role === "treeitem" ? level : null',
'[attr.aria-level]': 'level + 1',
'[attr.role]': 'role',
'class': 'cdk-tree-node',
},
Expand Down Expand Up @@ -337,8 +337,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
}

/**
* The role of the node should be 'group' if it's an internal node,
* and 'treeitem' if it's a leaf node.
* The role of the node should always be 'treeitem'.
*/
@Input() role: 'treeitem' | 'group' = 'treeitem';

Expand All @@ -363,10 +362,10 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
focus(): void {
this._elementRef.nativeElement.focus();
}

protected _setRoleFromData(): void {
if (this._tree.treeControl.isExpandable) {
this.role = this._tree.treeControl.isExpandable(this._data) ? 'group' : 'treeitem';
this.role = this._tree.treeControl.isExpandable(this._data) ? 'treeitem' : 'treeitem';
annieyw marked this conversation as resolved.
Show resolved Hide resolved
} else {
if (!this._tree.treeControl.getChildren) {
throw getTreeControlFunctionsMissingError();
Expand All @@ -376,12 +375,12 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
this._setRoleFromChildren(childrenNodes as T[]);
} else if (isObservable(childrenNodes)) {
childrenNodes.pipe(takeUntil(this._destroyed))
.subscribe(children => this._setRoleFromChildren(children));
.subscribe(children => this._setRoleFromChildren(children));
}
}
}

protected _setRoleFromChildren(children: T[]) {
annieyw marked this conversation as resolved.
Show resolved Hide resolved
this.role = children && children.length ? 'group' : 'treeitem';
this.role = 'treeitem';
}
}
2 changes: 1 addition & 1 deletion src/material/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const _MatTreeNodeMixinBase: HasTabIndexCtor & CanDisableCtor & typeof CdkTreeNo
inputs: ['disabled', 'tabIndex'],
host: {
'[attr.aria-expanded]': 'isExpanded',
'[attr.aria-level]': 'role === "treeitem" ? level : null',
'[attr.aria-level]': 'level + 1',
'[attr.role]': 'role',
'class': 'mat-tree-node'
},
Expand Down
11 changes: 11 additions & 0 deletions src/material/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe('MatTree', () => {
});
});

it('with the right aria-level attrs', () => {
// add a child to the first node
let data = underlyingDataSource.data;
underlyingDataSource.addChild(data[2]);
component.treeControl.expandAll();
fixture.detectChanges();

const ariaLevels = getNodes(treeElement).map(n => n.getAttribute('aria-level'));
expect(ariaLevels).toEqual(['1', '1', '1', '2']);
});

it('with the right data', () => {
expect(underlyingDataSource.data.length).toBe(3);

Expand Down