Skip to content

Commit 3d7b796

Browse files
committed
fix(cdk/tree): remove duplicate detectChanges
1 parent 64406c1 commit 3d7b796

File tree

4 files changed

+13
-39
lines changed

4 files changed

+13
-39
lines changed

src/cdk/tree/tree-using-legacy-key-manager.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe('CdkTree when provided LegacyTreeKeyManager', () => {
1515

1616
fixture = TestBed.createComponent(SimpleCdkTreeApp);
1717
fixture.detectChanges();
18-
fixture.detectChanges();
1918
});
2019

2120
describe('with default node options', () => {

src/cdk/tree/tree.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ describe('CdkTree', () => {
12561256
expect(treeElement.hasAttribute('tabindex')).toBeFalse();
12571257
});
12581258

1259-
it('sets the tabindex to the first item by default', () => {
1259+
fit('sets the tabindex to the first item by default', () => {
12601260
expect(nodes.map(x => x.getAttribute('tabindex')).join(', ')).toEqual('0, -1, -1');
12611261
});
12621262

src/cdk/tree/tree.ts

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,6 @@ export class CdkTree<T, K = T>
335335
new Map<K, CdkTreeNode<T, K>>(),
336336
);
337337

338-
/**
339-
* Synchronous cache of nodes for the `TreeKeyManager`. This is separate
340-
* from `_flattenedNodes` so they can be independently updated at different
341-
* times.
342-
*/
343-
private _keyManagerNodes: BehaviorSubject<readonly T[]> = new BehaviorSubject<readonly T[]>([]);
344-
345338
private _keyManagerFactory = inject(TREE_KEY_MANAGER) as TreeKeyManagerFactory<CdkTreeNode<T, K>>;
346339

347340
/** The key manager for this tree. Handles focus and activation based on user keyboard input. */
@@ -356,7 +349,7 @@ export class CdkTree<T, K = T>
356349
// - if an expansionKey is provided, TS will infer the type of K to be
357350
// the return type.
358351
// - if it's not, then K will be defaulted to T.
359-
return this.expansionKey() ?? ((item: T) => (item as unknown as K));
352+
return this.expansionKey() ?? ((item: T) => item as unknown as K);
360353
});
361354
readonly _trackByFn = computed(() => {
362355
const trackBy = this.trackBy();
@@ -411,16 +404,15 @@ export class CdkTree<T, K = T>
411404
};
412405
});
413406
});
407+
/**
408+
* Synchronous cache of nodes for the `TreeKeyManager`. This is separate
409+
* from `_flattenedNodes` so they can be independently updated at different
410+
* times.
411+
*/
412+
private readonly _keyManagerNodes = toObservable(this._flattenedNodes);
414413

415414
constructor(...args: unknown[]);
416-
constructor() {
417-
effect(
418-
() => {
419-
this._renderDataChanges(this._renderData());
420-
},
421-
{allowSignalWrites: true},
422-
);
423-
}
415+
constructor() {}
424416

425417
ngAfterContentInit() {
426418
this._initializeKeyManager();
@@ -511,22 +503,6 @@ export class CdkTree<T, K = T>
511503
);
512504
}
513505

514-
private _renderDataChanges(data: RenderingData<T> | null) {
515-
if (!data) {
516-
return;
517-
}
518-
519-
if (data.nodeType === null) {
520-
return;
521-
}
522-
523-
// If we're here, then we know what our node type is, and therefore can
524-
// perform our usual rendering pipeline.
525-
this._updateKeyManagerItems(data.flattenedNodes);
526-
// Explicitly detect the initial set of changes to this component subtree
527-
this._changeDetectorRef.detectChanges();
528-
}
529-
530506
private _emitExpansionChanges(expansionChanges: SelectionChange<K> | null) {
531507
if (!expansionChanges) {
532508
return;
@@ -1180,10 +1156,6 @@ export class CdkTree<T, K = T>
11801156
}
11811157
}
11821158

1183-
private _updateKeyManagerItems(flattenedNodes: readonly T[]) {
1184-
this._keyManagerNodes.next(flattenedNodes);
1185-
}
1186-
11871159
/** Traverse the flattened node data and compute parents, levels, and group data. */
11881160
private _calculateParents(flattenedNodes: readonly T[]): void {
11891161
const levelAccessor = this._getLevelAccessor();
@@ -1442,6 +1414,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
14421414

14431415
constructor() {
14441416
CdkTreeNode.mostRecentTreeNode = this as CdkTreeNode<T, K>;
1417+
console.log(inject(ViewContainerRef));
14451418
}
14461419

14471420
ngOnInit(): void {
@@ -1482,6 +1455,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
14821455
/** Focuses this data node. Implemented for TreeKeyManagerItem. */
14831456
focus(): void {
14841457
this._tabindex.set(0);
1458+
this._changeDetectorRef.detectChanges();
14851459
if (this._shouldFocus) {
14861460
this._elementRef.nativeElement.focus();
14871461
}
@@ -1490,6 +1464,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
14901464
/** Defocus this data node. */
14911465
unfocus(): void {
14921466
this._tabindex.set(-1);
1467+
this._changeDetectorRef.detectChanges();
14931468
}
14941469

14951470
/** Emits an activation event. Implemented for TreeKeyManagerItem. */
@@ -1517,6 +1492,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
15171492
/** Makes the node focusable. Implemented for TreeKeyManagerItem. */
15181493
makeFocusable(): void {
15191494
this._tabindex.set(0);
1495+
this._changeDetectorRef.detectChanges();
15201496
}
15211497

15221498
_focusItem() {

src/material/tree/tree-using-legacy-key-manager.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ describe('MatTree when provided LegacyTreeKeyManager', () => {
1818

1919
fixture = TestBed.createComponent(SimpleMatTreeApp);
2020
fixture.detectChanges();
21-
fixture.detectChanges();
2221
});
2322

2423
describe('when nodes have default options', () => {

0 commit comments

Comments
 (0)