Skip to content

Commit 48c3ec6

Browse files
committed
fix(tree): unable to set zero as the padding of a tree node
Fixes setting zero as the padding of a tree node being ignored by the `CkdTreeNodePadding` directive. Fixes #16338.
1 parent 6a7fc81 commit 48c3ec6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/cdk/tree/padding.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export class CdkTreeNodePadding<T> implements OnDestroy {
3737
@Input('cdkTreeNodePadding')
3838
get level(): number { return this._level; }
3939
set level(value: number) {
40-
this._level = coerceNumberProperty(value);
40+
// Set to null as the fallback value so that _setPadding can fall back to the node level if the
41+
// consumer set the directive as `cdkTreeNodePadding=""`. We still want to take this value if
42+
// they set 0 explicitly.
43+
this._level = coerceNumberProperty(value, null)!;
4144
this._setPadding();
4245
}
4346
_level: number;
@@ -90,8 +93,8 @@ export class CdkTreeNodePadding<T> implements OnDestroy {
9093
const nodeLevel = (this._treeNode.data && this._tree.treeControl.getLevel)
9194
? this._tree.treeControl.getLevel(this._treeNode.data)
9295
: null;
93-
const level = this._level || nodeLevel;
94-
return level ? `${level * this._indent}${this.indentUnits}` : null;
96+
const level = this._level == null ? nodeLevel : this._level;
97+
return typeof level === 'number' ? `${level * this._indent}${this.indentUnits}` : null;
9598
}
9699

97100
_setPadding(forceChange = false) {

src/cdk/tree/tree.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
9-
import {Component, ViewChild, TrackByFunction, Type, EventEmitter} from '@angular/core';
9+
import {Component, ViewChild, TrackByFunction, Type, EventEmitter, ViewChildren, QueryList} from '@angular/core';
1010

1111
import {CollectionViewer, DataSource} from '@angular/cdk/collections';
1212
import {Directionality, Direction} from '@angular/cdk/bidi';
@@ -17,7 +17,7 @@ import {BaseTreeControl} from './control/base-tree-control';
1717
import {TreeControl} from './control/tree-control';
1818
import {FlatTreeControl} from './control/flat-tree-control';
1919
import {NestedTreeControl} from './control/nested-tree-control';
20-
import {CdkTreeModule} from './index';
20+
import {CdkTreeModule, CdkTreeNodePadding} from './index';
2121
import {CdkTree, CdkTreeNode} from './tree';
2222
import {getTreeControlFunctionsMissingError} from './tree-errors';
2323

@@ -137,6 +137,18 @@ describe('CdkTree', () => {
137137
[`${data[2].pizzaTopping} - ${data[2].pizzaCheese} + ${data[2].pizzaBase}`]);
138138
});
139139

140+
it('should be able to set zero as the indent level', () => {
141+
component.paddingNodes.forEach(node => node.level = 0);
142+
fixture.detectChanges();
143+
144+
const data = dataSource.data;
145+
146+
expectFlatTreeToMatch(treeElement, 0, 'px',
147+
[`${data[0].pizzaTopping} - ${data[0].pizzaCheese} + ${data[0].pizzaBase}`],
148+
[`${data[1].pizzaTopping} - ${data[1].pizzaCheese} + ${data[1].pizzaBase}`],
149+
[`${data[2].pizzaTopping} - ${data[2].pizzaCheese} + ${data[2].pizzaBase}`]);
150+
});
151+
140152
it('should reset the opposite direction padding if the direction changes', () => {
141153
const node = getNodes(treeElement)[0];
142154

@@ -1132,7 +1144,7 @@ class SimpleCdkTreeApp {
11321144
indent: number | string = 28;
11331145

11341146
@ViewChild(CdkTree, {static: false}) tree: CdkTree<TestData>;
1135-
1147+
@ViewChildren(CdkTreeNodePadding) paddingNodes: QueryList<CdkTreeNodePadding<TestData>>;
11361148
}
11371149

11381150
@Component({

0 commit comments

Comments
 (0)