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

Revert "fix(material/tree): aria-expanded attribute should not appear in the leaf node (#29096)" #29272

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/cdk/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,20 @@ describe('CdkTree', () => {
let data = dataSource.data;
dataSource.addChild(data[2]);
fixture.detectChanges();
let ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, 'false', null]);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'false';
}),
).toBe(true);

component.treeControl.expandAll();
fixture.detectChanges();

ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, 'true', null]);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'true';
}),
).toBe(true);
});

it('with the right data', () => {
Expand Down Expand Up @@ -799,8 +805,11 @@ describe('CdkTree', () => {
});

it('with the right aria-expanded attrs', () => {
let ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, null]);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'false';
}),
).toBe(true);

component.toggleRecursively = false;
fixture.changeDetectorRef.markForCheck();
Expand All @@ -813,7 +822,7 @@ describe('CdkTree', () => {
fixture.detectChanges();

const ariaExpanded = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpanded).toEqual([null, 'true', 'false', null]);
expect(ariaExpanded).toEqual(['false', 'true', 'false', 'false']);
});

it('should expand/collapse the node multiple times', () => {
Expand Down Expand Up @@ -877,7 +886,6 @@ describe('CdkTree', () => {
});

it('should expand/collapse the node recursively', () => {
fixture.changeDetectorRef.markForCheck();
let data = dataSource.data;
const child = dataSource.addChild(data[1], false);
dataSource.addChild(child, false);
Expand Down
22 changes: 1 addition & 21 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
exportAs: 'cdkTreeNode',
host: {
'class': 'cdk-tree-node',
'[attr.aria-expanded]': 'isLeafNode ? null : isExpanded',
'[attr.aria-expanded]': 'isExpanded',
},
standalone: true,
})
Expand Down Expand Up @@ -375,26 +375,6 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
return this._tree.treeControl.isExpanded(this._data);
}

/* If leaf node, return true to not assign aria-expanded attribute */
get isLeafNode(): boolean {
// If flat tree node data returns false for expandable property, it's a leaf node
if (
this._tree.treeControl.isExpandable !== undefined &&
!this._tree.treeControl.isExpandable(this._data)
) {
return true;

// If nested tree node data returns 0 descendants, it's a leaf node
} else if (
this._tree.treeControl.isExpandable === undefined &&
this._tree.treeControl.getDescendants(this._data).length === 0
) {
return true;
}

return false;
}

get level(): number {
// If the treeControl has a getLevel method, use it to get the level. Otherwise read the
// aria-level off the parent node and use it as the level for this node (note aria-level is
Expand Down
23 changes: 16 additions & 7 deletions src/material/tree/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,20 @@ describe('MatTree', () => {
const data = underlyingDataSource.data;
underlyingDataSource.addChild(data[2]);
fixture.detectChanges();
let ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, 'false']);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'false';
}),
).toBe(true);

component.treeControl.expandAll();
fixture.detectChanges();

ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, 'true', null]);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'true';
}),
).toBe(true);
});

it('with the right data', () => {
Expand Down Expand Up @@ -464,8 +470,11 @@ describe('MatTree', () => {
});

it('with the right aria-expanded attrs', () => {
let ariaExpandedStates = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpandedStates).toEqual([null, null, null]);
expect(
getNodes(treeElement).every(node => {
return node.getAttribute('aria-expanded') === 'false';
}),
).toBe(true);

component.toggleRecursively = false;
const data = underlyingDataSource.data;
Expand All @@ -477,7 +486,7 @@ describe('MatTree', () => {
fixture.detectChanges();

const ariaExpanded = getNodes(treeElement).map(n => n.getAttribute('aria-expanded'));
expect(ariaExpanded).toEqual([null, 'true', 'false', null]);
expect(ariaExpanded).toEqual(['false', 'true', 'false', 'false']);
});

it('should expand/collapse the node', () => {
Expand Down
2 changes: 0 additions & 2 deletions tools/public_api_guard/cdk/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ export class CdkTreeNode<T, K = T> implements FocusableOption, OnDestroy, OnInit
// (undocumented)
get isExpanded(): boolean;
// (undocumented)
get isLeafNode(): boolean;
// (undocumented)
get level(): number;
static mostRecentTreeNode: CdkTreeNode<any> | null;
// (undocumented)
Expand Down
Loading