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(cdk/tree): react properly to expansion changes #29751

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1773629
fix(cdk/tree): add test that demonstrates issue
BobobUnicorn Sep 16, 2024
37f75e9
fix(cdk/tree): signalize render pipeline
BobobUnicorn Sep 16, 2024
c0406a8
fix(cdk/tree): fix lint errors
BobobUnicorn Sep 17, 2024
9dcfa35
fix(cdk/tree): update API goldens
BobobUnicorn Sep 17, 2024
7b14b85
fix(cdk/tree): formatting
BobobUnicorn Sep 17, 2024
a31f246
fix(cdk/tree): update goldens
BobobUnicorn Sep 17, 2024
dda9c79
refactor(cdk/tree): make tests compiled, gets rid of one `detectChanges`
BobobUnicorn Sep 19, 2024
78cc5c0
refactor(cdk/tree): fold more data computation into signals
BobobUnicorn Sep 20, 2024
a9c989e
fix(cdk/tree): remove unnecessary `async`
BobobUnicorn Sep 20, 2024
856fdd1
fix(cdk/tree): revert to ng_test_library; ng_module tests don't work …
BobobUnicorn Oct 3, 2024
b9dab16
fix(cdk/tree): revert signal changes for tests
BobobUnicorn Oct 3, 2024
8ca9455
refactor(cdk/tree): signalify `_flattenedNodes`
BobobUnicorn Oct 3, 2024
d58e9d9
refactor(cdk/tree): signalify the data rendering
BobobUnicorn Oct 3, 2024
312b578
refactor(cdk/tree): signalify the trackBy/expansionKey functions
BobobUnicorn Oct 3, 2024
327d6ad
fix(cdk/tree): remove duplicate `detectChanges`
BobobUnicorn Oct 3, 2024
7358786
fix(cdk/tree): fix lints
BobobUnicorn Oct 3, 2024
68b29d9
fix(cdk/tree): tests
BobobUnicorn Nov 6, 2024
ae9c2f5
fix(cdk/tree): change inputs back to regular @Input
BobobUnicorn Nov 6, 2024
93a959b
fix(cdk/tree): update goldens, fix lints
BobobUnicorn Nov 7, 2024
49330bb
fix(cdk/tree): formatting
BobobUnicorn Nov 7, 2024
cb4edf0
fix(cdk/tree): api goldens, again
BobobUnicorn Nov 7, 2024
be104c2
fix(cdk/tree): lint
BobobUnicorn Nov 18, 2024
ecc4070
fix(cdk/tree): format
BobobUnicorn Nov 18, 2024
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
6 changes: 6 additions & 0 deletions src/cdk/a11y/key-manager/tree-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ export class TreeKeyManager<T extends TreeKeyManagerItem> implements TreeKeyMana
if (newIndex > -1 && newIndex !== this._activeItemIndex) {
this._activeItemIndex = newIndex;
this._typeahead?.setCurrentSelectedItemIndex(newIndex);
} else if (newIndex === -1) {
// if there's no new matching element, then we reset the state of this
// key manager
this._activeItem = null;
this._activeItemIndex = -1;
this._hasInitialFocused = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/cdk/tree/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ng_test_library(
"//src/cdk/collections",
"//src/cdk/keycodes",
"//src/cdk/testing/testbed",
"@npm//@angular/common",
"@npm//rxjs",
],
)
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/tree/nested-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class CdkNestedTreeNode<T, K = T>
}

ngAfterContentInit() {
this._dataDiffer = this._differs.find([]).create(this._tree.trackBy);
this._dataDiffer = this._differs.find([]).create(this._tree._trackByFn());
this._tree
._getDirectChildren(this.data)
.pipe(takeUntil(this._destroyed))
Expand Down
11 changes: 5 additions & 6 deletions src/cdk/tree/tree-using-legacy-key-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ElementRef, QueryList, ViewChild, ViewChildren} from '@angular/core';
import {Component, QueryList, ViewChildren, ElementRef} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {of} from 'rxjs';
import {CdkTreeModule} from './tree-module';
Expand All @@ -9,8 +9,7 @@ describe('CdkTree when provided LegacyTreeKeyManager', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [CdkTreeModule],
declarations: [SimpleCdkTreeApp],
imports: [SimpleCdkTreeApp],
providers: [NOOP_TREE_KEY_MANAGER_FACTORY_PROVIDER],
});

Expand Down Expand Up @@ -74,20 +73,20 @@ class MinimalTestData {

@Component({
template: `
<cdk-tree #tree [dataSource]="dataSource" [childrenAccessor]="getChildren">
<cdk-tree [dataSource]="dataSource" [childrenAccessor]="getChildren">
<cdk-tree-node #node *cdkTreeNodeDef="let node">
{{node.name}}
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class SimpleCdkTreeApp {
isExpandable = (node: MinimalTestData) => node.children.length > 0;
getChildren = (node: MinimalTestData) => node.children;

dataSource = of([new MinimalTestData('apple'), new MinimalTestData('banana')]);

@ViewChild('tree', {read: ElementRef}) tree: ElementRef<HTMLElement>;
@ViewChildren('node') treeNodes: QueryList<ElementRef<HTMLElement>>;
}
56 changes: 36 additions & 20 deletions src/cdk/tree/tree-with-tree-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {FlatTreeControl} from './control/flat-tree-control';
import {NestedTreeControl} from './control/nested-tree-control';
import {CdkTreeModule, CdkTreeNodePadding} from './index';
import {CdkTree, CdkTreeNode} from './tree';
import {NgIf, NgSwitch} from '@angular/common';

describe('CdkTree with TreeControl', () => {
/** Represents an indent for expectNestedTreeToMatch */
Expand All @@ -37,9 +38,9 @@ describe('CdkTree with TreeControl', () => {
let tree: CdkTree<TestData>;
let dir: {value: Direction; readonly change: EventEmitter<Direction>};

function configureCdkTreeTestingModule(declarations: Type<any>[]) {
function configureCdkTreeTestingModule(imports: Type<any>[]) {
TestBed.configureTestingModule({
imports: [CdkTreeModule],
imports,
providers: [
{
provide: Directionality,
Expand All @@ -58,7 +59,6 @@ describe('CdkTree with TreeControl', () => {
},
},
],
declarations: declarations,
});
}

Expand Down Expand Up @@ -216,9 +216,8 @@ describe('CdkTree with TreeControl', () => {
});

it('should be able to set zero as the indent level', () => {
component.paddingNodes.forEach(node => (node.level = 0));
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
component.paddingNodes.forEach(node => (node.level = 0));

const data = dataSource.data;

Expand Down Expand Up @@ -532,7 +531,9 @@ describe('CdkTree with TreeControl', () => {

// Add new data
component.dataSource.data = copiedData;
fixture.detectChanges();
component.dataSource.addData();
fixture.detectChanges();
}

it('should add/remove/move nodes with reference-based trackBy', () => {
Expand Down Expand Up @@ -1462,7 +1463,8 @@ function expectNestedTreeToMatch(treeElement: Element, ...expectedTree: any[]) {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class SimpleCdkTreeApp {
getLevel = (node: TestData) => node.level;
Expand All @@ -1488,7 +1490,8 @@ class SimpleCdkTreeApp {
</ng-container>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule, NgSwitch],
})
class SimpleCdkTreeAppWithIndirectNodes extends SimpleCdkTreeApp {}

Expand All @@ -1501,7 +1504,8 @@ class SimpleCdkTreeAppWithIndirectNodes extends SimpleCdkTreeApp {}
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class NestedCdkTreeApp {
getChildren = (node: TestData) => node.observableChildren;
Expand All @@ -1525,7 +1529,8 @@ class NestedCdkTreeApp {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class StaticNestedCdkTreeApp {
getChildren = (node: TestData) => node.children;
Expand Down Expand Up @@ -1562,7 +1567,8 @@ class StaticNestedCdkTreeApp {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class WhenNodeNestedCdkTreeApp {
isSecondNode = (_: number, node: TestData) => node.pizzaBase.indexOf('2') > 0;
Expand All @@ -1586,7 +1592,8 @@ class WhenNodeNestedCdkTreeApp {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class CdkTreeAppWithToggle {
toggleRecursively: boolean = true;
Expand All @@ -1613,7 +1620,8 @@ class CdkTreeAppWithToggle {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule, NgIf],
})
class NestedCdkTreeAppWithToggle {
toggleRecursively: boolean = true;
Expand Down Expand Up @@ -1643,7 +1651,8 @@ class NestedCdkTreeAppWithToggle {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class WhenNodeCdkTreeApp {
isOddNode = (_: number, node: TestData) => node.level % 2 === 1;
Expand All @@ -1667,7 +1676,8 @@ class WhenNodeCdkTreeApp {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class ArrayDataSourceCdkTreeApp {
getLevel = (node: TestData) => node.level;
Expand All @@ -1694,7 +1704,8 @@ class ArrayDataSourceCdkTreeApp {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class ObservableDataSourceCdkTreeApp {
getLevel = (node: TestData) => node.level;
Expand All @@ -1720,7 +1731,8 @@ class ObservableDataSourceCdkTreeApp {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class ArrayDataSourceNestedCdkTreeApp {
getChildren = (node: TestData) => node.observableChildren;
Expand All @@ -1745,7 +1757,8 @@ class ArrayDataSourceNestedCdkTreeApp {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class ObservableDataSourceNestedCdkTreeApp {
getChildren = (node: TestData) => node.observableChildren;
Expand All @@ -1771,7 +1784,8 @@ class ObservableDataSourceNestedCdkTreeApp {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class DepthNestedCdkTreeApp {
getChildren = (node: TestData) => node.observableChildren;
Expand All @@ -1795,7 +1809,8 @@ class DepthNestedCdkTreeApp {
</cdk-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class CdkTreeAppWithTrackBy {
trackByStrategy: 'reference' | 'property' | 'index' = 'reference';
Expand Down Expand Up @@ -1829,7 +1844,8 @@ class CdkTreeAppWithTrackBy {
</cdk-nested-tree-node>
</cdk-tree>
`,
standalone: false,
standalone: true,
imports: [CdkTreeModule],
})
class NestedCdkTreeAppWithTrackBy {
trackByStrategy: 'reference' | 'property' | 'index' = 'reference';
Expand Down
Loading
Loading