Skip to content

Commit b775aaf

Browse files
committed
feat(cdk/tree): report an error when the API consumer tries to expand a non-expandable node.
1 parent 65ec31b commit b775aaf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/cdk/tree/tree-errors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,15 @@ export function getMultipleTreeControlsError() {
5454
export function getTreeControlNodeTypeUnspecifiedError() {
5555
return Error(`The nodeType was not specified for the tree.`);
5656
}
57+
58+
/**
59+
* Returns an error to be thrown when a node is attempted to be expanded or collapsed when
60+
* it's not expandable.
61+
* @docs-private
62+
*/
63+
export function getNodeNotExpandableError() {
64+
return Error(
65+
`The node that was attempted to be expanded or collapsed is not expandable; you might ` +
66+
`need to provide 'isExpandable'.`,
67+
);
68+
}

src/cdk/tree/tree.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {CdkTreeNodeDef, CdkTreeNodeOutletContext} from './node';
6161
import {CdkTreeNodeOutlet} from './outlet';
6262
import {
6363
getMultipleTreeControlsError,
64+
getNodeNotExpandableError,
6465
getTreeControlMissingError,
6566
getTreeControlNodeTypeUnspecifiedError,
6667
getTreeMissingMatchingNodeDefError,
@@ -1097,12 +1098,18 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
10971098

10981099
/** Collapses this data node. Implemented for TreeKeyManagerItem. */
10991100
collapse(): void {
1101+
if (typeof ngDevMode === 'undefined' || (ngDevMode && !this._isExpandable())) {
1102+
throw getNodeNotExpandableError();
1103+
}
11001104
this._tree.collapse(this._data);
11011105
this.expandedChange.emit(this.isExpanded);
11021106
}
11031107

11041108
/** Expands this data node. Implemented for TreeKeyManagerItem. */
11051109
expand(): void {
1110+
if (typeof ngDevMode === 'undefined' || (ngDevMode && !this._isExpandable())) {
1111+
throw getNodeNotExpandableError();
1112+
}
11061113
this._tree.expand(this._data);
11071114
this.expandedChange.emit(this.isExpanded);
11081115
}

0 commit comments

Comments
 (0)