Skip to content

Commit 2d02f8c

Browse files
committed
fix(cdk/tree): remove duplicate detectChanges
1 parent 74222dd commit 2d02f8c

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
@@ -1278,7 +1278,7 @@ describe('CdkTree', () => {
12781278
expect(treeElement.hasAttribute('tabindex')).toBeFalse();
12791279
});
12801280

1281-
it('sets the tabindex to the first item by default', () => {
1281+
fit('sets the tabindex to the first item by default', () => {
12821282
expect(nodes.map(x => x.getAttribute('tabindex')).join(', ')).toEqual('0, -1, -1');
12831283
});
12841284

src/cdk/tree/tree.ts

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

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

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

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

426418
ngAfterContentInit() {
427419
this._initializeKeyManager();
@@ -512,22 +504,6 @@ export class CdkTree<T, K = T>
512504
);
513505
}
514506

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

1196-
private _updateKeyManagerItems(flattenedNodes: readonly T[]) {
1197-
this._keyManagerNodes.next(flattenedNodes);
1198-
}
1199-
12001172
/** Traverse the flattened node data and compute parents, levels, and group data. */
12011173
private _calculateParents(flattenedNodes: readonly T[]): void {
12021174
const levelAccessor = this._getLevelAccessor();
@@ -1454,6 +1426,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
14541426

14551427
constructor() {
14561428
CdkTreeNode.mostRecentTreeNode = this as CdkTreeNode<T, K>;
1429+
console.log(inject(ViewContainerRef));
14571430
}
14581431

14591432
ngOnInit(): void {
@@ -1494,6 +1467,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
14941467
/** Focuses this data node. Implemented for TreeKeyManagerItem. */
14951468
focus(): void {
14961469
this._tabindex.set(0);
1470+
this._changeDetectorRef.detectChanges();
14971471
if (this._shouldFocus) {
14981472
this._elementRef.nativeElement.focus();
14991473
}
@@ -1502,6 +1476,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
15021476
/** Defocus this data node. */
15031477
unfocus(): void {
15041478
this._tabindex.set(-1);
1479+
this._changeDetectorRef.detectChanges();
15051480
}
15061481

15071482
/** Emits an activation event. Implemented for TreeKeyManagerItem. */
@@ -1529,6 +1504,7 @@ export class CdkTreeNode<T, K = T> implements OnDestroy, OnInit, TreeKeyManagerI
15291504
/** Makes the node focusable. Implemented for TreeKeyManagerItem. */
15301505
makeFocusable(): void {
15311506
this._tabindex.set(0);
1507+
this._changeDetectorRef.detectChanges();
15321508
}
15331509

15341510
_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)