Skip to content

Commit 3bd160b

Browse files
crisbetojelbourn
authored andcommitted
fix(tree): unable to set zero as the padding of a tree node (#16345)
Fixes setting zero as the padding of a tree node being ignored by the `CkdTreeNodePadding` directive. Fixes #16338.
1 parent ad09bb2 commit 3bd160b

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
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: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
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 {
10+
Component,
11+
ViewChild,
12+
TrackByFunction,
13+
Type,
14+
EventEmitter,
15+
ViewChildren,
16+
QueryList,
17+
} from '@angular/core';
1018

1119
import {CollectionViewer, DataSource} from '@angular/cdk/collections';
1220
import {Directionality, Direction} from '@angular/cdk/bidi';
@@ -17,7 +25,7 @@ import {BaseTreeControl} from './control/base-tree-control';
1725
import {TreeControl} from './control/tree-control';
1826
import {FlatTreeControl} from './control/flat-tree-control';
1927
import {NestedTreeControl} from './control/nested-tree-control';
20-
import {CdkTreeModule} from './index';
28+
import {CdkTreeModule, CdkTreeNodePadding} from './index';
2129
import {CdkTree, CdkTreeNode} from './tree';
2230
import {getTreeControlFunctionsMissingError} from './tree-errors';
2331

@@ -137,6 +145,18 @@ describe('CdkTree', () => {
137145
[`${data[2].pizzaTopping} - ${data[2].pizzaCheese} + ${data[2].pizzaBase}`]);
138146
});
139147

148+
it('should be able to set zero as the indent level', () => {
149+
component.paddingNodes.forEach(node => node.level = 0);
150+
fixture.detectChanges();
151+
152+
const data = dataSource.data;
153+
154+
expectFlatTreeToMatch(treeElement, 0, 'px',
155+
[`${data[0].pizzaTopping} - ${data[0].pizzaCheese} + ${data[0].pizzaBase}`],
156+
[`${data[1].pizzaTopping} - ${data[1].pizzaCheese} + ${data[1].pizzaBase}`],
157+
[`${data[2].pizzaTopping} - ${data[2].pizzaCheese} + ${data[2].pizzaBase}`]);
158+
});
159+
140160
it('should reset the opposite direction padding if the direction changes', () => {
141161
const node = getNodes(treeElement)[0];
142162

@@ -1046,7 +1066,10 @@ function expectFlatTreeToMatch(treeElement: Element,
10461066
}
10471067

10481068
function checkLevel(node: Element, expectedNode: any[]) {
1049-
const actualLevel = (node as HTMLElement).style.paddingLeft;
1069+
const rawLevel = (node as HTMLElement).style.paddingLeft;
1070+
1071+
// Some browsers return 0, while others return 0px.
1072+
const actualLevel = rawLevel === '0' ? '0px' : rawLevel;
10501073
const expectedLevel = `${(expectedNode.length) * expectedPaddingIndent}${expectedPaddingUnits}`;
10511074
if (actualLevel != expectedLevel) {
10521075
missedExpectations.push(
@@ -1132,7 +1155,7 @@ class SimpleCdkTreeApp {
11321155
indent: number | string = 28;
11331156

11341157
@ViewChild(CdkTree, {static: false}) tree: CdkTree<TestData>;
1135-
1158+
@ViewChildren(CdkTreeNodePadding) paddingNodes: QueryList<CdkTreeNodePadding<TestData>>;
11361159
}
11371160

11381161
@Component({

src/material/tree/tree.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,14 @@ function expectFlatTreeToMatch(treeElement: Element, expectedPaddingIndent: numb
539539
}
540540

541541
function checkLevel(node: Element, expectedNode: any[]) {
542+
const rawLevel = (node as HTMLElement).style.paddingLeft;
542543

543-
const actualLevel = (node as HTMLElement).style.paddingLeft;
544+
// Some browsers return 0, while others return 0px.
545+
const actualLevel = rawLevel === '0' ? '0px' : rawLevel;
544546
if (expectedNode.length === 1) {
545-
if (actualLevel !== ``) {
547+
if (actualLevel !== `` && actualLevel !== '0px') {
546548
missedExpectations.push(
547-
`Expected node level to be 0 but was ${actualLevel}`);
549+
`Expected node level to be 0px but was ${actualLevel}`);
548550
}
549551
} else {
550552
const expectedLevel = `${(expectedNode.length - 1) * expectedPaddingIndent}px`;

0 commit comments

Comments
 (0)